suchen

Heim  >  Fragen und Antworten  >  Hauptteil

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

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

高洛峰高洛峰2767 Tage vor964

Antworte allen(4)Ich werde antworten

  • 迷茫

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

    为何我看到的第一印象是流式布局 FlowLayout,GitHub 流式布局,Google出来的

    你动态的 addView() 也是可以做到的

    Antwort
    0
  • 巴扎黑

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

    同求,之前我有类似需求的时候,如果内容不是太多,会在内嵌套一个list/recycler View并抛掉所有触摸事件避免滑动冲突,着实是不太优雅。

    Antwort
    0
  • 怪我咯

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

    在我看来这不是recyclerView嵌套recyclerView 一个recyclerView就可以搞定 只是要显示不同的list的数据和不同的item,我的思路是新建一个baseModel 两个数据源的bean 继承于baseModel,还是看代码 说是说不清楚

    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;
        }

    public class PersonModel extends ModelBase {

    private long id;
    private String name;
    private int age;
    }

    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方法 布局文件也是很简单
    
    

    Antwort
    0
  • 大家讲道理

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

    用expandListView 干嘛非要用recyclerView

    Antwort
    0
  • StornierenAntwort