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

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

回收站 View 布局
<?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查看项目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>

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

}}

最佳答案

正如罗汉(Rohan)所指出的,作为一种简单的解决方案,WRAP_CONTENT不能与RecyclerView一起使用,因为高度不能设置

RecyclerView上的android:minHeight="...dp"
如果您不介意我的建议,那么RecyclerView适配器的实现看起来有些复杂。也许将考试名称列表,价格列表和图像ID组织到一个普通的类中。例如

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

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

现在,您将拥有一个像这样的构造函数,而不是在构造函数中包含多个参数。
List<Exam> examList;

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

您的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();
                }
            });
    }
}

关于android - 水平回收站 View 不显示任何内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32165321/

10-11 14:25