以下是一个示例代码,展示了如何在Android中嵌套使用RecyclerView,并根据不同的项目进行计数。
activity_main.xml
:
item_outer.xml
:
item_inner.xml
:
OuterAdapter.java
:public class OuterAdapter extends RecyclerView.Adapter {
private List outerItems;
public OuterAdapter(List outerItems) {
this.outerItems = outerItems;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_outer, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
OuterItem outerItem = outerItems.get(position);
holder.outerTextView.setText(outerItem.getTitle());
InnerAdapter innerAdapter = new InnerAdapter(outerItem.getInnerItems());
holder.innerRecyclerView.setAdapter(innerAdapter);
}
@Override
public int getItemCount() {
return outerItems.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView outerTextView;
RecyclerView innerRecyclerView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
outerTextView = itemView.findViewById(R.id.outerTextView);
innerRecyclerView = itemView.findViewById(R.id.innerRecyclerView);
}
}
}
InnerAdapter.java
:public class InnerAdapter extends RecyclerView.Adapter {
private List innerItems;
public InnerAdapter(List innerItems) {
this.innerItems = innerItems;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_inner, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
InnerItem innerItem = innerItems.get(position);
holder.innerTextView.setText(innerItem.getName());
}
@Override
public int getItemCount() {
return innerItems.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView innerTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
innerTextView = itemView.findViewById(R.id.innerTextView);
}
}
}
OuterItem.java
和InnerItem.java
,并在OuterItem
中添加计数值:public class OuterItem {
private String title;
private List innerItems;
private int count;
public OuterItem(String title, List innerItems) {
this.title = title;
this.innerItems = innerItems;
this.count = innerItems.size();
}
public String getTitle() {
return title;
}
public List getInnerItems() {
return innerItems;
}
public int getCount() {
return count;
}
}
public class InnerItem {
private String name;
public InnerItem(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class MainActivity extends AppCompatActivity {
private RecyclerView outerRecyclerView;
private OuterAdapter outerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outerRecyclerView = findViewById(R.id.outerRecyclerView);
outerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
List outerItems = generateOuterItems();
outerAdapter = new OuterAdapter(outerItems);
outer