没有显示任何东西的水平的Recycler视图

far*_*zin 4 android android-layout android-recyclerview

我有一个相对布局的水平回收视图,一切看起来还不错,但在布局中它没有显示回收者视图的任何项目.网站上有一些相同的问题,但我找不到问题.这是代码:

RecyclerView适配器:

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();
                }
            });
}
}
Run Code Online (Sandbox Code Playgroud)

Recycler视图布局

<?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>
Run Code Online (Sandbox Code Playgroud)

Recyler视图项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>
Run Code Online (Sandbox Code Playgroud)

主要活动:

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);

}}
Run Code Online (Sandbox Code Playgroud)

Rak*_*ari 5

正如Rohan所指出的WRAP_CONTENT那样,一个高度不适RecyclerView用于一个简单的解决方案尝试设置一个

android:minHeight="...dp" 在RecyclerView上

如果您不介意我建议,您对RecyclerView适配器的实现看起来有点复杂.也许将考试名单,价格表和图像ID组织成一个共同的类.例如

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

    // getter and setters for exame name, price, and image id
}
Run Code Online (Sandbox Code Playgroud)

现在不是在构造函数中有很多参数,而是有一个像这样的构造函数.

List<Exam> examList;

public ExamDetailRecyclerViewAdapter(Activity activity, List<Exam> examList) {
    this.examList = examList;
    this.activity = activity;
}
Run Code Online (Sandbox Code Playgroud)

onBindViewHolder会看起来像这样:

@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();
                }
            });
    }
}
Run Code Online (Sandbox Code Playgroud)