如何为多个视图持有者创建一个与所选视图持有人匹配的视图持有者视图适配器,而不是在同一回收者视图列表中 [英] how to create one recycler view adapter for multiple viewholders that matches a viewholder of choice not in the same recycler view list

查看:54
本文介绍了如何为多个视图持有者创建一个与所选视图持有人匹配的视图持有者视图适配器,而不是在同一回收者视图列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建一个可容纳多个视图持有者的回收站视图适配器,并且每次用该适配器实例化RecyclerView时,我只想查看一种特定类型,而不是混合类型.

I would like to know how I can create ONE recycler view adapter that holds MULTIPLE view-holders and each time the RecyclerView is instantiated with that adapter,I want to see only a specific type rather than a mixed type.

我在Android活动中有2个不同的屏幕,每个屏幕都显示一个项目/卡片列表,一个是文本和数字,另一个是照片.现在,我为每个对象有2个不同的自定义适配器,而我想要实现的是一个适配器的灵活性,该适配器可以根据其显示的屏幕在对象类型之间进行延迟,并仅列出我需要的行.

I have 2 different screens in the Android activity and each of them shows a list of items/cards.One is of text and number and the other is a photo. Right now I have 2 different custom adapters for each object and what I want to achieve is the flexibility of one adapter that can defer between the types of objects according to the screen its displaying and list only the rows I need.

我所看到的所有示例都是混合类型的对象,而这并不是我所追求的.

All the examples I've seen are of mixed type objects and it is not what I am after.

适配器代码附在下面.

    public class MultiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<MYDataProvider> arrayList;
    private final static int VIEW0 = 0;
    private final static int VIEW1 = 1;

    public MultiAdapter(ArrayList<MyDataProvider> _arrayList) {
        this.arrayList = new ArrayList<>();
        this.arrayList = _arrayList;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return VIEW0;

            case 1:
            default:
                return VIEW1;
        }//switch
    }//get item view type

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        int layoutRes;
        View view;
        RecyclerView.ViewHolder viewholder;

        switch (viewType) {
            case VIEW0:
                layoutRes = R.layout.card_item;
                view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutRes, viewGroup, false);
                viewholder =  new E1(view);
                break;

            case VIEW1:
            default:
                layoutRes = R.layout.item_layout;
                view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutRes, viewGroup, false);
                viewholder =  new E2(view);
                break;
        }//switch

        return viewholder;
    }//on create

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case VIEW0: {
                MyDataProvider mydata = arrayList.get(position);
                ((E1)holder).name1.setText(mydata.getD_name() + "this is item " + position);
                ((E1)holder).imageView1.setImageResource(arrayList.get(position).getImg_res());
            }
            break;

            case VIEW1: {
                MyDataProvider mydata = arrayList.get(position);
                ((E2)holder).name2.setText(mydata.getD_name() + "this is item " + position);
                ((E2)holder).imageView2.setImageResource(arrayList.get(position).getImg_res());
            }
            break;
        }//switch
    }//on bind


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

    public class E1 extends mainViewHolder {
        ImageView imageView1;
        TextView name1;

        public E1(View itemView) {
            super(itemView);
            imageView1 = itemView.findViewById(R.id.image);
            name1 = itemView.findViewById(R.id.d_name);
        }
    }//e1 class

        public class E2 extends mainViewHolder {
        ImageView imageView2;
        TextView name2;

        public E2(View itemView) {
            super(itemView);
            imageView2 = itemView.findViewById(R.id.person_photo);
            name2 = itemView.findViewById(R.id.person_name);
        }
    }//e2 class

    public class mainViewHolder extends RecyclerView.ViewHolder {
        public mainViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}//adapter

推荐答案

我不明白您的问题在哪里?您能否提供更多说明以实现您的目标?

I don't get it where is your problem? Can you provide more explanation what you want to achieve?

//编辑

在您的代码中,您确定只有第一个项目将是VIEW0类型的项目,而其他项目将是VIEW1类型.这在getItemViewType(int position)功能中.

In your code you determine that only first item will be item of type VIEW0 and others will be VIEW1. This is in getItemViewType(int position) function.

如果要确定选择哪种ViewHolder,则必须先选择它,然后再填充列表.在适配器中创建枚举:

If you want determine what ViewHolder to pick you must to choose it before you populate list. Create enum in your adapter:

enum TYPE {
    VIEW0,
    VIEW1
}

然后创建枚举变量:

private TYPE itemType;

并将其传递给您的构造函数:

and pass it in your constructor:

public Test(ArrayList<MyDataProvider> _arrayList, TYPE itemType) {
    this.arrayList = new ArrayList<>();
    this.arrayList = _arrayList;
    this.itemType = itemType;
}

,然后在onCreateViewHolder函数中检查您的类型是什么(基于itemType变量).请注意,switch中的变量是"itemType",而不是覆盖方法中的viewType.

and check what is your type (based on itemType variable) in your switch in onCreateViewHolder function like that. Note that variable in switch is "itemType", not viewType from override method.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (itemType) {
    case VIEW0:
        return new NotificationsAdapter.ReferralTriggerViewHolder(inflater.inflate(R.layout.row_referral_trigger, parent, false));
    case VIEW1:
        return new NotificationsAdapter.UserNotificationViewHolder(inflater.inflate(R.layout.row_layout_user_notification, parent, false));
    default:
        return new NotificationsAdapter.UserNotificationViewHolder(inflater.inflate(R.layout.row_layout_user_notification, parent, false));
}

}

这篇关于如何为多个视图持有者创建一个与所选视图持有人匹配的视图持有者视图适配器,而不是在同一回收者视图列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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