搜尋

首頁  >  問答  >  主體

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

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

高洛峰高洛峰2767 天前965

全部回覆(4)我來回復

  • 迷茫

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

    為何我看到的第一印像是流式布局 FlowLayout,GitHub 串流佈局,Google出來的

    你動態的 addView() 也是可以做到的

    回覆
    0
  • 巴扎黑

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

    同求,之前我有類似需求的時候,如果內容不是太多,會在內嵌套一個list/recycler View並拋掉所有觸摸事件避免滑動衝突,著實是不太優雅。

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

    回覆
    0
  • 大家讲道理

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

    用expandListView 幹嘛非要用recyclerView

    回覆
    0
  • 取消回覆