如何在片段中使用CardView运行RecyclerView? [英] How to run my RecyclerView with CardView in fragment?

查看:66
本文介绍了如何在片段中使用CardView运行RecyclerView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,每当我尝试运行它时..应用程序没有运行就没有错误,但是没有运行,有人可以帮助我确定导致这种问题的原因是什么.

I have this code and everytime I tried to run it ..the application is not running not error but not running can anyone please help me to determine what is the reason for getting this kind of problem.

因为我正在用CardView做RecyclerView片段,所以我将近一个星期遇到了这个问题:(

because I'm doing RecyclerView with CardView in fragment I have this problem almost a week :(

这是我的代码:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext ;
    private List<Book> mData ;


    public RecyclerViewAdapter(Context mContext, List<Book> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    public RecyclerViewAdapter(TimeFragment timeFragment, List<Book> lstBook) {
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view ;
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        view = mInflater.inflate(R.layout.cardveiw_item_book,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.tv_book_title.setText(mData.get(position).getTitle());
        holder.img_book_thumbnail.setImageResource(mData.get(position).getThumbnail());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_book_title;
        public ImageView img_book_thumbnail;
        CardView cardView ;

        public MyViewHolder(View itemView) {
            super(itemView);

            tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id) ;
            img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_img_id);
            cardView = (CardView) itemView.findViewById(R.id.cardview_id);


        }
    }


}

这是相关的POJO类.

Here's is the related POJO class.

public class Book {

    private String Title;
    private String Category ;
    private String Description ;
    private int Thumbnail ;

    public Book() {
    }

    public Book(String title, String category, String description, int thumbnail) {
        Title = title;
        Category = category;
        Description = description;
        Thumbnail = thumbnail;
    }


    public String getTitle() {
        return Title;
    }

    public String getCategory() {
        return Category;
    }

    public String getDescription() {
        return Description;
    }

    public int getThumbnail() {
        return Thumbnail;
    }


    public void setTitle(String title) {
        Title = title;
    }

    public void setCategory(String category) {
        Category = category;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public void setThumbnail(int thumbnail) {
        Thumbnail = thumbnail;
    }
}

将适配器设置为RecyclerView的片段的代码.

Code for the fragment that sets the adapter to RecyclerView.

public class TimeFragment extends Fragment {
    List<Book> lstBook ;

    public TimeFragment() {
    }

    @Nullable

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lstBook = new ArrayList<>();
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.ic_in));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.ic_out));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.ic_in));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.ic_out));
    }


    @Nullable
    @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;

        view = inflater.inflate( R.layout.recyclerview, container, false);
        RecyclerView myrv = (RecyclerView) view.findViewById(R.id.recyclerview_id);
        RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,lstBook);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        myrv.setLayoutManager(gridLayoutManager);
        myrv.setAdapter(myAdapter);
        return view;
    }
}

推荐答案

实际上有两种方法.

1.从RecyclerViewAdapter中删除下面提到的行.

公共RecyclerViewAdapter(TimeFragment timeFragment,List lstBook){ }

public RecyclerViewAdapter(TimeFragment timeFragment, List lstBook) { }

在片段代码中,还有一个小的变化,请看下面的代码,而且我没有可绘制对象,所以我使用了默认对象.

and in the fragment code, there is also a minor change kindly look the code below and also I didn't have the drawable so I used the default one.

public class TimeFragment extends Fragment {

    List<Book> lstBook ;

    public TimeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lstBook = new ArrayList<>();
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.ic_launcher_background));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.ic_launcher_background));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.ic_launcher_background));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.ic_launcher_background));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_time, container, false);

        RecyclerView myrv = (RecyclerView) view.findViewById(R.id.recyclerview_id);
        RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(getContext(),lstBook);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        myrv.setLayoutManager(gridLayoutManager);
        myrv.setAdapter(myAdapter);
        return view;
    }

}

如果您不删除上述行,它将仍然有效.只需修改您的TimeFragment的代码即可.

And if you don't remove the above-mentioned line still it is going to work. Just modify the code of your TimeFragment.

  1. 发生的事情是,当您在适配器构造函数中拥有上下文时,您实际上已经使用了适配器的第二个构造函数,该构造函数接受如下所示的参数

  1. What has happened is when you have the context in the adapter constructor you have actually used the second constructor of the adapter which takes the argument as shown below

公共RecyclerViewAdapter(TimeFragment timeFragment,列表lstBook){
}

public RecyclerViewAdapter(TimeFragment timeFragment, List lstBook) {
}

实际上,您实际上并未填充列表,这是在下面显示的第一个构造函数中完成的.

In this, you have actually not populated the list, which you have done in the first constructor shown below.

public RecyclerViewAdapter(Context mContext, List<Book> mData) {
    this.mContext = mContext;
    this.mData = mData;
}

现在,应用程序崩溃是因为当它在适配器中找到列表时,它始终为空.所以它崩溃了.我已经在适配器部分和片段部分的代码中更改了几行.请在下面找到更改.

Now the app crashes because when it finds the list in the adapter it is always empty. so it crashes. I have changed a few lines in the code in both the adapter part and the fragment part. Kindly find the changes below.

RecyclerViewAdapter:

RecyclerViewAdapter:

   public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

        private Context mContext ;
        private List<Book> mData ;
        private TimeFragment timeFragment;


        public RecyclerViewAdapter(Context mContext, List<Book> mData) {
            this.mContext = mContext;
            this.mData = mData;
        }

        public RecyclerViewAdapter(TimeFragment timeFragment, List<Book> lstBook) {
            this.timeFragment = timeFragment;
            this.mData = lstBook;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view ;
            LayoutInflater mInflater = LayoutInflater.from(timeFragment.getContext());
            view = mInflater.inflate(R.layout.cardveiw_item_book,parent,false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {

            holder.tv_book_title.setText(mData.get(position).getTitle());
            holder.img_book_thumbnail.setImageResource(mData.get(position).getThumbnail());
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public static class MyViewHolder extends RecyclerView.ViewHolder {

            public TextView tv_book_title;
            public ImageView img_book_thumbnail;
            CardView cardView ;

            public MyViewHolder(View itemView) {
                super(itemView);
                tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id) ;
                img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_img_id);
                cardView = (CardView) itemView.findViewById(R.id.cardview_id);
            }
        }


    }

时间片段:

public class TimeFragment extends Fragment {

    List<Book> lstBook ;

    public TimeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("TestFailed","OnCreate");
        lstBook = new ArrayList<>();
        lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.ic_launcher_background));
        lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.ic_launcher_background));
        lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.ic_launcher_background));
        lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.ic_launcher_background));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.d("TestFailed","OnCreateView");
        View view = inflater.inflate(R.layout.fragment_time, container, false);

        RecyclerView myrv = (RecyclerView) view.findViewById(R.id.recyclerview_id);
        RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,lstBook);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        myrv.setLayoutManager(gridLayoutManager);
        myrv.setAdapter(myAdapter);
        return view;
    }

}

希望可以帮助消除疑虑.

Hope that helps and clears the doubt.

这篇关于如何在片段中使用CardView运行RecyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆