Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal Recycler View Not Showing Anything

I have a horizontal recycler view in a relative layout, everything looks OK but in the layout it doesn't show any item of the recycler view.There are some same questions on the Website but I can't find the problem.Here's the code:

RecyclerView Adapter:

public class ExamDetailRecyclerViewAdapter extends
    RecyclerView.Adapter<ExamDetailRecyclerViewAdapter.DetailViewHolder> {

private String[] examNameList;
private Integer[] examImageIds;
private String[] examPriceList;

private static Activity activity;

public ExamDetailRecyclerViewAdapter(Activity activity,
        String[] examNameList, Integer[] examImageIds,
        String[] examPriceList) {
    this.examNameList = examNameList;
    this.activity = activity;
    this.examImageIds = examImageIds;
    this.examPriceList = examPriceList;

}

@Override
public int getItemCount() {
    return examNameList.length;
}

@Override
public long getItemId(int position) {
    return position;
}

public DetailViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
            R.layout.exam_detail_hlistview_item, viewGroup, false);

    return new DetailViewHolder(itemView);
}

public static class DetailViewHolder extends RecyclerView.ViewHolder
        implements OnClickListener {

    TextView examNameTextView;
    ImageView examImageView;
    TextView examPriceTextView;
    RatingBar examRatingBar;

    public DetailViewHolder(View v) {
        super(v);

        examNameTextView = (TextView) v
                .findViewById(R.id.exDetail_hListView_exam_name);
        examImageView = (ImageView) v
                .findViewById(R.id.exDetail_hListView_image);
        examPriceTextView = (TextView) v
                .findViewById(R.id.exDetail_hListView_exam_price);
        examRatingBar = (RatingBar) v
                .findViewById(R.id.exDetail_hListView_exam_rating_bar);
         v.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i = new Intent(v.getContext(), ExamDetailActivity.class);
        activity.startActivity(i);
    }
}

@Override
public void onBindViewHolder(DetailViewHolder detailViewHolder, int i) {
    // TODO Auto-generated method stub
    detailViewHolder.examNameTextView.setText(examNameList[i]);
    String num = displayNumbertoFarsi(examPriceList[i]);
    detailViewHolder.examPriceTextView.setText(num);
    detailViewHolder.examImageView.setImageResource(examImageIds[i]);
    detailViewHolder.examRatingBar
            .setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

                @Override
                public void onRatingChanged(RatingBar ratingBar,
                        float rating, boolean fromUser) {

                    Toast.makeText(activity,
                            "Rating Value: " + String.valueOf(rating),
                            Toast.LENGTH_SHORT).show();
                }
            });
}
}

Recycler view layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/ir.elenoon.bazmoon"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<include
    android:id="@+id/toolbar_exam"
    layout="@layout/toolbar" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/about_exam_bottom_layout"
        android:background="@color/chromatic_grey"
        android:scaleType="fitXY"
        android:scrollbars="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg" >

            <RelativeLayout
                android:id="@+id/fourth_partition"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/third_partition"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/button_border"
                android:padding="12dp" >

                <app.textView.custemFont.TextViewPlus
                    android:id="@+id/about_exam_same_exams_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_margin="5dp"
                    android:text="@string/about_exam_same_exams_title"
                    android:textColor="#000000"
                    android:textStyle="bold"
                    app:customFont1="DroidNaskhRegularSystemUI.ttf" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/exam_detail_hListView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/about_exam_same_exams_title"
                    android:background="#000000"
                    android:focusable="false"
                    android:paddingBottom="20dp"
                    android:paddingTop="20dp"
                    android:scrollbars="horizontal" />
            </RelativeLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

</LinearLayout>

Recyler view item xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/ir.elenoon.bazmoon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
    android:id="@+id/exDetail_hListView_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:contentDescription="@string/image_button_contentDescription"
    android:scaleType="fitXY"
    android:src="@drawable/icon_luncher" />

<app.textView.custemFont.TextViewPlus
    android:id="@+id/exDetail_hListView_exam_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:text="exam_name"
    android:textColor="@color/black"
    app:customFont1="DroidNaskhRegularSystemUI.ttf" />

<RelativeLayout
    android:id="@+id/bottom_grid_view_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <app.textView.custemFont.TextViewPlus
        android:id="@+id/exDetail_hListView_exam_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="right"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/exDetail_hListView_exam_rating_bar"
        android:text="exam_price"
        android:textColor="@color/black"
        app:customFont1="Byekan.ttf" />

    <RatingBar
        android:id="@+id/exDetail_hListView_exam_rating_bar"
        style="@style/foodRatingBarSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:isIndicator="true"
        android:numStars="5"
        android:rating="3"
        android:stepSize="0.1" />
</RelativeLayout>

</LinearLayout>

Main Activity:

public class ExamDetailActivity extends AppCompatActivity {

String[] examNameList = { "Java", "C", "C++", "C#", "Jsp",
        "Microsoft .Net", "Android", "PHP", "Jquery", "JavaScript" };

String[] examPriceList = { "1000 ت", "2000 ت", "3000 ت", "4000 ت",
        "5000 ت", "6000 ت", "7000 ت", "8000 ت", "9000 ت", "10 000 ت" };
// Keep all Images in array
Integer[] examImageIds = { R.drawable.s1, R.drawable.s2, R.drawable.s3,
        R.drawable.s4, R.drawable.s5, R.drawable.s6, R.drawable.s7,
        R.drawable.s8, R.drawable.s9, R.drawable.s10 };

ExamDetailRecyclerViewAdapter mDetailHListViewAdapter;

private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.exam_detail);

    recyclerView = (RecyclerView) findViewById(R.id.exam_detail_hListView);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.HORIZONTAL);
    recyclerView.setLayoutManager(llm);

    ExamDetailRecyclerViewAdapter examDetailHListViewAdapter = new ExamDetailRecyclerViewAdapter(
            this, examNameList, examImageIds, examPriceList);

    recyclerView.setAdapter(examDetailHListViewAdapter);

}}
like image 408
farzin Avatar asked Aug 23 '15 09:08

farzin


1 Answers

As pointed out by Rohan the WRAP_CONTENT as a height does not work with the RecyclerView for a simple solution try setting a

android:minHeight="...dp" on the RecyclerView

If you don't mind me suggesting, your implementation for RecyclerView's adapter looks a little complex. Maybe organize exam namelists, pricelists and image ids into a common class. For e.g.

public class Exam {
    String examName;
    String price;
    int imageId;

    // getter and setters for exame name, price, and image id
}

Now rather than having numerous parameters in your constructor you'd have a constructor like this.

List<Exam> examList;

public ExamDetailRecyclerViewAdapter(Activity activity, List<Exam> examList) {
    this.examList = examList;
    this.activity = activity;
}

Your onBindViewHolder would look something like this:

@Override
public void onBindViewHolder(DetailViewHolder detailViewHolder, int position) {
    // TODO Auto-generated method stub
    Exam exam = examList.get(position);
    detailViewHolder.examNameTextView.setText(exam.getExamName());
    String num = displayNumbertoFarsi(exam.getPrice();
    detailViewHolder.examPriceTextView.setText(num);
    detailViewHolder.examImageView.setImageResource(exam.getImageId());
    detailViewHolder.examRatingBar
            .setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

                @Override
                public void onRatingChanged(RatingBar ratingBar,
                        float rating, boolean fromUser) {

                    Toast.makeText(activity,
                            "Rating Value: " + String.valueOf(rating),
                            Toast.LENGTH_SHORT).show();
                }
            });
    }
}
like image 71
Rakeeb Rajbhandari Avatar answered Oct 07 '22 01:10

Rakeeb Rajbhandari