java - 如何从嵌套的 Recycler View 中获取多个 Checkbox 状态?
问题描述
在嵌套的回收器视图中,我有多个复选框(在我的情况下为开关)。它如下所示:
我希望能够在用户返回时检索此屏幕上的所有开关状态,并能够识别哪些设置为真。例如,我正在寻找与此类似格式的东西: [Item 0, Sub Item 1, TRUE], [Item 0, Sub Item 2, TRUE], [Item 1, Sub Item 0, TRUE] ,等等
我的问题是我不确定如何存储这些开关状态。当它们被放置在嵌套的回收器视图中时,我不确定如何在此处获取特定的开关值。
这是我正在关注的代码:
主要活动
package com.example.recycledviewpoolexample;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rvItem = findViewById(R.id.rv_item);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
ItemAdapter itemAdapter = new ItemAdapter(buildItemList());
rvItem.setAdapter(itemAdapter);
rvItem.setLayoutManager(layoutManager);
}
private List<Item> buildItemList() {
List<Item> itemList = new ArrayList<>();
for (int i=0; i<10; i++) {
Item item = new Item("Item "+i, buildSubItemList());
itemList.add(item);
}
return itemList;
}
private List<SubItem> buildSubItemList() {
List<SubItem> subItemList = new ArrayList<>();
for (int i=0; i<3; i++) {
SubItem subItem = new SubItem("Sub Item "+i, false);
subItemList.add(subItem);
}
return subItemList;
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
/*if Checkboxes are true, store/display them in the logs
EXAMPLE OUTPUT:
Item (String)
SubItem (String)
*/
}
}
项目.java
public class Item {
private String itemTitle;
private List<SubItem> subItemList;
public Item(String itemTitle, List<SubItem> subItemList) {
this.itemTitle = itemTitle;
this.subItemList = subItemList;
}
public String getItemTitle() {
return itemTitle;
}
public void setItemTitle(String itemTitle) {
this.itemTitle = itemTitle;
}
public List<SubItem> getSubItemList() {
return subItemList;
}
public void setSubItemList(List<SubItem> subItemList) {
this.subItemList = subItemList;
}
}
项适配器
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
private List<Item> itemList;
ItemAdapter(List<Item> itemList) {
this.itemList = itemList;
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, viewGroup, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, int i) {
Item item = itemList.get(i);
itemViewHolder.tvItemTitle.setText(item.getItemTitle());
// Create layout manager with initial prefetch item count
LinearLayoutManager layoutManager = new LinearLayoutManager(
itemViewHolder.rvSubItem.getContext(),
LinearLayoutManager.VERTICAL,
false
);
layoutManager.setInitialPrefetchItemCount(item.getSubItemList().size());
// Create sub item view adapter
SubItemAdapter subItemAdapter = new SubItemAdapter(item.getSubItemList());
itemViewHolder.rvSubItem.setLayoutManager(layoutManager);
itemViewHolder.rvSubItem.setAdapter(subItemAdapter);
itemViewHolder.rvSubItem.setRecycledViewPool(viewPool);
}
@Override
public int getItemCount() {
return itemList.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView tvItemTitle;
private RecyclerView rvSubItem;
ItemViewHolder(View itemView) {
super(itemView);
tvItemTitle = itemView.findViewById(R.id.tv_item_title);
rvSubItem = itemView.findViewById(R.id.rv_sub_item);
}
}
}
子项.java
package com.example.recycledviewpoolexample;
public class SubItem {
private int subItemImage;
private String subItemTitle;
private boolean checkbox;
public SubItem(String subItemTitle, boolean checkbox) {
this.subItemTitle = subItemTitle;
this.checkbox = checkbox;
}
public int getSubItemImage() {
return subItemImage;
}
public void setSubItemImage(int subItemImage) {
this.subItemImage = subItemImage;
}
public String getSubItemTitle() {
return subItemTitle;
}
public void setSubItemTitle(String subItemTitle) {
this.subItemTitle = subItemTitle;
}
public boolean isSelected() {
return checkbox;
}
public void setCheckbox(boolean checkbox) {
this.checkbox = checkbox;
}
}
子项适配器.java
public class SubItemAdapter extends RecyclerView.Adapter<SubItemAdapter.SubItemViewHolder> {
private List<SubItem> subItemList;
SubItemAdapter(List<SubItem> subItemList) {
this.subItemList = subItemList;
}
@NonNull
@Override
public SubItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_sub_item, viewGroup, false);
return new SubItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SubItemViewHolder subItemViewHolder, int i) {
final SubItem subItem = subItemList.get(i);
subItemViewHolder.tvSubItemTitle.setText(subItem.getSubItemTitle());
subItemViewHolder.checkBox.setOnCheckedChangeListener(null);
subItemViewHolder.checkBox.setChecked(subItem.isSelected());
subItemViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
subItem.setCheckbox(isChecked);
}
});
}
@Override
public int getItemCount() {
return subItemList.size();
}
class SubItemViewHolder extends RecyclerView.ViewHolder {
TextView tvSubItemTitle;
Switch checkBox;
SubItemViewHolder(View itemView) {
super(itemView);
tvSubItemTitle = itemView.findViewById(R.id.tv_sub_item_title);
checkBox = itemView.findViewById(R.id.switch1);
}
}
}
layout_sub_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img_sub_item"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@color/colorPrimaryDark"
android:src="@drawable/ic_launcher_foreground"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/img_sub_item"
android:padding="12dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_sub_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Sub item title"/>
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
layout_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#f3f3f3"
app:cardElevation="8dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12sp"
android:textSize="18sp"
android:text="Item Title"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_sub_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
任何帮助,将不胜感激。谢谢!
解决方案
推荐阅读
- python - 如何在使用 requests.get() 开始下载之前加载完整的网页?
- javascript - 如何在Javascript中调用祖父母类的setter
- c# - 添加自定义属性并从动态对象获取属性
- django - Template Parse Erros Angular "<" (" with Django REST framework
- python - 人类可读的大小到一个字节范围
- java - 未调用 RetryLoadBalancerInterceptor
- python - 从 Python 串行输入解码 £ 符号的问题
- typescript - 在 vuex 中设置 firebase.User 会导致永恒循环
- sql - 如何在 Oracle 中获取表关系/结构图/实体关系图 (ERD) 或依赖关系?数据库?
- python - Twilio 函数:在函数调用中添加参数