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

时间:2015-08-23 09:50:26

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

回收商视图布局

<?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>

主要活动:

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

}}

3 个答案:

答案 0 :(得分:5)

正如Rohan所指出的那样for (int i = 0; i<myarray.GetLengh(0);i++) { //get personnal number as integer, this is simple int personnal = Convert.ToInt32(myarray[i][0]); //get industry number as integer, this is the difficult part string strIndustry = myarray[i][1]; strIndustry = strIndustry.SubString(0,1);//remove the first char int industry = Convert.ToInt32(strindustry);//I assume that you only have numbers after the letter if not you may need to test it with tryParse... //then you can now compare integers if(personnal==industry) { //your code to execute if true } else{//...} } 作为一个高度不适用于WRAP_CONTENT的简单解决方案尝试设置

RecyclerView上的

RecyclerView

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

android:minHeight="...dp"

现在,您的构造函数中没有多个参数,而是有这样的构造函数。

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

答案 1 :(得分:2)

您可能需要参考以下主题

How do I make WRAP_CONTENT work on a RecyclerView

Wrap_content通常不适用于RecyclerView。您需要一个自定义LayoutManager并覆盖onMeasure。

答案 2 :(得分:1)

wrap_content无法使用默认LinearLayoutManager,请改用此库 https://github.com/serso/android-linear-layout-manager

编辑:

这是怎么做的 将以下依赖项添加到模块build.gradle

compile 'org.solovyev.android.views:linear-layout-manager:0.5@aar'

然后在你的代码中

LinearLayoutManager layoutManager = new org.solovyev.android.views.llm.LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);