具有很多视图持有者的Recyclerview

时间:2019-04-18 22:47:56

标签: android android-recyclerview view

帮助,请修复用于recyclerview的适配器。它必须仅将列表中的所有对象添加到recyclerview(作为列表)。

代码:

适配器:

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

    private Context context;
    private List<Model> listItems;

    TestAdapter(Context context, List<Model> listItems){
        this.context = context;
        this.listItems = listItems;
    }

    @Override
    public int getItemViewType(int position) {
        switch (listItems.get(position).type) {
            case 0:
                return Model.YOUR_MESSAGE_TYPE;
            case 1:
                return Model.INTERLOCUTOR_MESSAGE_TYPE;
            case 2:
                return Model.ADVERTISING_INTEGRATION_TYPE;
            default:
                return -1;
        }
    }

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


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view;
        switch (i) {
            case Model.YOUR_MESSAGE_TYPE:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.your_message_item, viewGroup, false);
                return new YourMessageVH(view);
            case Model.INTERLOCUTOR_MESSAGE_TYPE:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.interlocutor_message_item, viewGroup, false);
                return new InterlocutorMessageVH(view);
            case Model.ADVERTISING_INTEGRATION_TYPE:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.advertising_integration_item, viewGroup, false);
                return new AdvertisingIntegrationVH(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        Model object = listItems.get(i);
        switch (object.type) {
            case Model.YOUR_MESSAGE_TYPE:
                Picasso.get().load(object.getImage()).into(((YourMessageVH) viewHolder).image);
                ((YourMessageVH) viewHolder).message.setText(object.getMessage());
                ((YourMessageVH) viewHolder).date.setText(object.getDate());
                ((YourMessageVH) viewHolder).message.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "CALLBACK TO Activity", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case Model.INTERLOCUTOR_MESSAGE_TYPE:
                Picasso.get().load(object.getImage()).into(((InterlocutorMessageVH) viewHolder).image);
                ((InterlocutorMessageVH) viewHolder).message.setText(object.getMessage());
                ((InterlocutorMessageVH) viewHolder).date.setText(object.getDate());
                break;
        }
    }


    private class YourMessageVH extends RecyclerView.ViewHolder{
        ImageView image;
        TextView message;
        TextView date;

        private YourMessageVH(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.iv_your_image);
            message = itemView.findViewById(R.id.tv_your_message);
            date = itemView.findViewById(R.id.tv_your_message_date);
        }
    }

    private class InterlocutorMessageVH extends RecyclerView.ViewHolder{
        ImageView image;
        TextView message;
        TextView date;

        private InterlocutorMessageVH(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.iv_interlocutor_image);
            message = itemView.findViewById(R.id.tv_interlocutor_message);
            date = itemView.findViewById(R.id.tv_interlocutor_message_date);
        }
    }

    private class AdvertisingIntegrationVH extends RecyclerView.ViewHolder{
        Button button;

        private AdvertisingIntegrationVH(@NonNull View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.b_show_reward);
        }
    }
}

列出数据:

public class Data {

    public static ArrayList<Model> getModelList() {
        ArrayList<Model> list = new ArrayList<>();

        String date = "18.04.2019";
        String johnSnowImage = "https://pbs.twimg.com/profile_images/901947348699545601/hqRMHITj_400x400.jpg";
        String ygritteImage = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c4/Ygritte-Rose_Leslie.jpg/220px-Ygritte-Rose_Leslie.jpg";

        Model yourFirstMessage = new Model();
        yourFirstMessage.setType(Model.YOUR_MESSAGE_TYPE);
        yourFirstMessage.setImage(johnSnowImage);
        yourFirstMessage.setMessage("Игритт, подожди!");
        yourFirstMessage.setDate(date);
        list.add(yourFirstMessage);

        Model interlocutorFirstMessage = new Model();
        interlocutorFirstMessage.setType(Model.INTERLOCUTOR_MESSAGE_TYPE);
        interlocutorFirstMessage.setImage(ygritteImage);
        interlocutorFirstMessage.setMessage("Ничего ты не знаешь, Джон Сноу!");
        interlocutorFirstMessage.setDate(date);
        list.add(interlocutorFirstMessage);

        Model yourSecondMessage = new Model();
        yourFirstMessage.setType(Model.YOUR_MESSAGE_TYPE);
        yourFirstMessage.setImage(johnSnowImage);
        yourFirstMessage.setMessage("Кое-что я знаю. Знаю что люблю тебя. Знаю что ты меня любишь. Но сейчас мне нужно домой!");
        yourFirstMessage.setDate(date);
        list.add(yourSecondMessage);

        Model firstAdvertisingIntegration = new Model();
        firstAdvertisingIntegration.setType(Model.ADVERTISING_INTEGRATION_TYPE);
        list.add(firstAdvertisingIntegration);

        return list;
    }

}

示例:

Recyclerview必须显示“ yourFirstMessage”,然后显示“ interlocutorFirstMessage”,然后显示“ yourSecondMessage”,然后显示“ firstAdvertisingIntegration”(所有这些对象均来自Data)

但是其显示方式类似于“ yourSecondMessage”,“ interlocutorFirstMessage”,然后为空的cardview块,然后是“ firstAdvertisingIntegration”。

对不起,英语。 希望您能解决它。

1 个答案:

答案 0 :(得分:0)

您不必自己做。 这是一个适合您的开源库,它功能强大且易于使用。

https://github.com/CymChad/BaseRecyclerViewAdapterHelper