Similar request, when I had similar needs before, if the content was not too much, I would nest a list/recycler View and throw away all touch events to avoid sliding conflicts, which is really not very elegant.
In my opinion, this is not a recyclerView nested recyclerView. A recyclerView can handle it. It just needs to display different list data and different items. My idea is to create a new baseModel. The beans of the two data sources inherit from the baseModel. Let’s look at the code. I can’t explain clearly
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);
}
}
}
Why the first impression I saw was
流式布局 FlowLayout
, GitHub flow layout, from GoogleYour dynamic
addView()
can also be doneSimilar request, when I had similar needs before, if the content was not too much, I would nest a list/recycler View and throw away all touch events to avoid sliding conflicts, which is really not very elegant.
In my opinion, this is not a recyclerView nested recyclerView. A recyclerView can handle it. It just needs to display different list data and different items. My idea is to create a new baseModel. The beans of the two data sources inherit from the baseModel. Let’s look at the code. I can’t explain clearly
public class PersonModel extends ModelBase {
private long id;
private String name;
private int age;
}
Why do you have to use recyclerView when using expandListView