简体   繁体   中英

How to set multiple Recycleview to single adapter in Firebase (MultipleView Type)?

How to set multiple RecycleView to a single adapter in Firebase?

I am creating an app, there are a lot of categories in my app, and creating multiple adapters, models, and RecycleView makes an app complicated. It is hard to make different adapters for multiple RecycleView.

Can I set multiple RecycleView to one single adapter? By using if or else or switch cases how to do this?

Or is there any alternative?

First Model Class

public class FirstModel {
    private String Image, Text;

    FirstModel() {
    }

    public FirstModel(String image, String text) {
        Image = image;
        Text = text;
    }

    public String getImage() {
        return Image;
    }

    public void setImage(String image) {
        Image = image;
    }

    public String getText() {
        return Text;
    }

    public void setText(String text) {
        Text = text;
    }
}

Second Model Class

public class SecondModel  {
    private String Image, Text;

    SecondModel() {
    }

    public SecondModel(String image, String text) {
        Image = image;
        Text = text;
    }

    public String getImage() {
        return Image;
    }

    public void setImage(String image) {
        Image = image;
    }

    public String getText() {
        return Text;
    }

    public void setText(String text) {
        Text = text;
    }

Link To One Adapter

public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int FirstLayout = 0;
    private final int SecondLayout = 1;    

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == FirstLayout) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_categories, null);
            return new GiftsForHerViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_categories, null);
            return new GiftsForHimViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        ????
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        switch (holder.getItemViewType()) {
            case 1:
                ?????
        }
    }

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

        class FirstViewHolder extends RecyclerView.ViewHolder {

            private ImageView Image;
            
            
            private TextView Text;

            public FirstViewHolder(@NonNull View itemView) {
                super(itemView);

                Image = (ImageView) itemView.findViewById(R.id.Image);
                Text = (TextView) itemView.findViewById(R.id.Text);
            }
        }

        class SecondViewHolder extends RecyclerView.ViewHolder {

            public SecondViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }

When you want to present items in a list by using a Recycle View and an adapter, then these items usually have similar UI and are base on the same type of data (ex Car, Person, Building, etc.). It's not a good idea to mix different entities in a single adapter. The data sources can be of different lengths and the list items will have different UI. Thus, stick to the idea - one Recycle View with one adapter.

One of the most important ideas in programming is reusability , and I quote, "In computer science and software engineering, reusability is the use of existing assets in some form within the software product development process".

So if the only thing that differs is the model classes and the layout files, then you can create a single adapter class. The key to solving this issue is to use generics . Here is a working example:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM