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

android– 水平的Recycler视图没有显示任何东西,第1张

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

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

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;}@OverrIDepublic int getItemCount() {    return examnameList.length;}@OverrIDepublic 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);    }}@OverrIDepublic 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视图布局

<?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/@R_912_4048@_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"                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 arrayInteger[] 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;@OverrIDeprotected 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看起来像这样:

@OverrIDepublic 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 – 水平的Recycler视图没有显示任何东西全部内容,希望文章能够帮你解决android – 水平的Recycler视图没有显示任何东西所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存