cari

Rumah  >  Soal Jawab  >  teks badan

android - recyclerview嵌套recyclerview比较好的解决方案

像这种需要recyclerview嵌套recyclerview的情况有没有比较好的解决方案

高洛峰高洛峰2767 hari yang lalu966

membalas semua(4)saya akan balas

  • 迷茫

    迷茫2017-04-18 09:20:45

    Sebab kesan pertama yang saya lihat ialah 流式布局 FlowLayout, reka letak aliran GitHub, dihasilkan oleh Google

    Dinamik anda addView() juga boleh dilakukan

    balas
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:20:45

    Permintaan yang sama, apabila saya mempunyai keperluan yang sama sebelum ini, jika kandungannya tidak terlalu banyak, saya akan meletakkan senarai/recycler View dan membuang semua peristiwa sentuhan untuk mengelakkan konflik gelongsor. Ia benar-benar tidak begitu elegan.

    balas
    0
  • 怪我咯

    怪我咯2017-04-18 09:20:45

    Pada pendapat saya, ini bukan recyclerView bersarang RecyclerView boleh mengendalikannya. Ia hanya perlu memaparkan data senarai yang berbeza dan item yang berbeza daripada baseModel, atau lihat Kod tidak jelas

    public class ModelBase {
    
        public int BEAN_SORT = -1;
        public int BEAN_TYPE = -1;
    }
    
    
      public class AnimalModel extends ModelBase {
    
        private long id;
        private String name;
        private int age;
        private String address;
        }

    personModel kelas awam memanjangkan ModelBase {

    id panjang peribadi;
    nama Rentetan peribadi;
    umur int peribadi;
    }

    public class MainActivity extends AppCompatActivity {
    
        RecyclerView rvModel;
        private ArrayList<ModelBase> list;
    
        private enum BASE_TYPE {
            TYPE_PERSON, TYPE_ANIMAL
        }
    
        private ModelAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            rvModel = (RecyclerView) findViewById(R.id.recycler_view);
            list = new ArrayList<ModelBase>();
            rvModel.setLayoutManager(new LinearLayoutManager(this));
            bindData();
            adapter = new ModelAdapter();
            rvModel.setAdapter(adapter);
        }
    
        private void bindData() {
            PersonModel person1 = new PersonModel(1, "大白", 25, "2017-02-13");
            list.add(person1);
            PersonModel person2 = new PersonModel(2, "二白", 25, "2018-06-21");
            list.add(person2);
            AnimalModel animal1 = new AnimalModel(1, "兔子", 2, "", "2016-03-15");
            list.add(animal1);
            AnimalModel animal2 = new AnimalModel(1, "兔子2", 2, "森林", "2019-02-14");
            list.add(animal2);
            PersonModel person3 = new PersonModel(3, "三白", 25, "2017-03-18");
            list.add(person3);
            AnimalModel animal3 = new AnimalModel(1, "兔子3", 2, "森林", "2015-03-15");
            list.add(animal3);
            PersonModel person4 = new PersonModel(4, "四白", 25, "2015-03-28");
            list.add(person4);
            PersonModel person5 = new PersonModel(5, "小白", 25, "2016-05-15");
            list.add(person5);
            AnimalModel animal4 = new AnimalModel(1, "兔子4", 2, "森林", "2014-03-14");
            list.add(animal4);
            Collections.sort(list, new Comparator<ModelBase>() {
                @Override
                public int compare(ModelBase model1, ModelBase model2) {
                    return model2.data.compareTo(model1.data);
                }
            });  // 排序
        }
    
        class ModelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                if (viewType == BASE_TYPE.TYPE_PERSON.ordinal()) {
                    return new PersonViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_person, parent, false));
                } else if (viewType == BASE_TYPE.TYPE_ANIMAL.ordinal()) {
                    return new AnimalViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_animal, parent, false));
                }
                return null;
            }
    
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                if (holder instanceof PersonViewHolder) {
                    ((PersonViewHolder) holder).tvPerson.setText(((PersonModel) list.get(position)).getName());
                }
                if (holder instanceof AnimalViewHolder) {
                    ((AnimalViewHolder) holder).tvAnimal.setText(((AnimalModel) list.get(position)).getName());
                }
            }
    
            @Override
            public int getItemCount() {
                return list.size();
            }
    
            @Override
            public int getItemViewType(int position) {
                if (list.get(position).BEAN_TYPE == 1) {
                    return BASE_TYPE.TYPE_PERSON.ordinal();
                } else if (list.get(position).BEAN_TYPE == 2) {
                    return BASE_TYPE.TYPE_ANIMAL.ordinal();
                }
                return 0;
            }
    
            class PersonViewHolder extends RecyclerView.ViewHolder {
                TextView tvPerson;
    
                public PersonViewHolder(View itemView) {
                    super(itemView);
                    tvPerson = (TextView) itemView.findViewById(R.id.tv_person);
                }
            }
    
            class AnimalViewHolder extends RecyclerView.ViewHolder {
                TextView tvAnimal;
    
                public AnimalViewHolder(View itemView) {
                    super(itemView);
                    tvAnimal = (TextView) itemView.findViewById(R.id.tv_animal);
                }
            }
        }
    
    你可以照着列子敲一下 bean省去了set和get方法 布局文件也是很简单
    
    

    balas
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:20:45

    Mengapa anda perlu menggunakan recyclerView apabila menggunakan expandListView

    balas
    0
  • Batalbalas