如何为所有Recycler视图android创建通用适配器类

时间:2016-02-23 11:42:49

标签: listview android-recyclerview recycler-adapter

我必须为所有Recyclerview创建单独的适配器类。为所有Recycler视图实现单个适配器类的任何方式?

public void onBindViewHolder(ViewHolder holder, int position) {
        System.out.println("$$$bind");

        if(fragmentName.equalsIgnoreCase("")) {
            holder.text.setText("");
            holder.text.setBackgroundResource();
            holder.textValue.setBackgroundResource();
            holder.textValue.setCompoundDrawablesWithIntrinsicBounds(R.drawable.coupons_arrow, 0, 0, 0);
        }
        if(fragmentName.equalsIgnoreCase("")) {
            holder.text.setText("name");
            holder.text.setBackgroundResource(R.color.c_101035);
            holder.textValue.setBackgroundResource(R.color.c_7b7bff);
            holder.textValue.setCompoundDrawablesWithIntrinsicBounds(R.drawable.deals_arrow_img, 0, 0, 0);
        }
        if (fragmentName.equalsIgnoreCase("")) {
                holder.text.setText("");
                holder.text.setBackgroundResource(R.color.c_10331);
                holder.textValue.setBackgroundResource(R.color.c_alpha_7dfff2);
                holder.textValue.setCompoundDrawablesWithIntrinsicBounds(R.drawable.visiting_offer_arrow, 0, 0, 0);
            }
          //Model obj= new FragmentTodaytip();
          //obj.getTitle();
       // holder.servicename.setText("ccheckkk");

    }

这个create.it如何使大量的视图包含在每个回收者列表项的bind和view holder中,以实现这一目标的其他方式?

1 个答案:

答案 0 :(得分:0)

是的,你可以使用相同的适配器,你只需要在你的适配器中做一些额外的编码。

为两个不同的RecyclerViews创建两个不同的构造函数。

假设一个Recycler View包含1个textView,所以你需要传递一个String,对于包含2个textViews的RecyclerView,传递两个字符串。

例如,如果Adapter类是 MyAdapter ,请使用以下构造函数。

 public MyAdapter(String[] myDataset,String[] mySecondaryDataset, Context context) {
    mDataset = myDataset;
    mSecondaryDataset=mySecondaryDataset;
    this.context=context;

}

public MyAdapter(String[] myDataset,Context context)
{
    mDataset=myDataset;
    this.context=context;
}

并在您需要的任何地方对您的适配器代码进行此类更改。