How to use RecyclerView like GridView

Asked 2 years ago, Updated 2 years ago, 83 views

The only views the adapter references are nameText and dataText.

DB is like this.

When you search your name on SearchView, I hope it comes out like GridView.

JOHN1 JOHN1 JOHN1 JOHN1
10    20    30    40

But I don't know how to import data into the adapter.

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

    List<Data> dataList;
    Context context;

    public Adapter(List<Data> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//        //        holder.nameText.setText(dataList.get(position).getName());
//        //        holder.dataText.setText(dataList.get(position).getData1());
//        //        holder.dataText.setText(dataList.get(position).getData2());
//        //        holder.dataText.setText(dataList.get(position).getData3());
//        //        holder.dataText.setText(dataList.get(position).getData4());
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView nameText, dataText;

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

            nameText = itemView.findViewById(R.id.nameText);
            dataText = itemView.findViewById(R.id.dataText);
        }
    }
}

If I comment on the onBindViewHolder, only one data is included, what should I do?

recyclerview retrofit2

2022-09-20 19:55

1 Answers

Since there are only two TextViews that the adapter refers to, if you want to print out four names and data in one set per line, you can make four sets of TextViews in the xml of the holder item in advance.

Otherwise, you might want to use grid views, or you might want to use horizontal list views in vertical list views. As it stands, it seems difficult to use magic to draw the desired result.


    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.bindView(dataList.get(position).getName(), 
                dataList.get(position).getData1(), 
                dataList.get(position).getData2(), 
                dataList.get(position).getData3(), 
                dataList.get(position).getData4());
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView nameText, data1Text, data2Text, data3Text, data4Text;

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

            nameText = itemView.findViewById(R.id.nameText);
            data1Text = itemView.findViewById(R.id.data1Text);
            data2Text = itemView.findViewById(R.id.data2Text);
            data3Text = itemView.findViewById(R.id.data3Text);
            data4Text = itemView.findViewById(R.id.data4Text);
        }

        public view bindView(String name, String data1, String data2, String data3, String data4) {
            nameText.setText(name);
            data1Text.setText(data1);
            data2Text.setText(data2);
            data3Text.setText(data3);
            data4Text.setText(data4);

        }
    }





2022-09-20 19:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.