我更喜欢一个解决方案,我可以从更高级别(如 Activity)监听所有这些回调,我可以在其中更改数据对象并刷新 RecyclerView 根据回调保持同步。(如果你扩展它,这最终是你需要的。)
我实现了您的代码并进行了一些修改以获得预期的结果。
对于此解决方案:
- 我创建了一个接口来获取回调
- 在添加单击例程
- 在删除单击任务
- 在任务上更改检查状态
- 使我的活动实现该接口并将其传递给适配器。
- 适配器将其传递给 ViewHolder
- ViewHolder 将在单击时调用所需的函数。
- 在回调中:
- 对于添加:您可以知道单击了哪个例程
- 对于删除:可以知道ParentRoutine、Task的Child Index和Task
- 对于检查更改:您可以了解 ParentRoutine、Task 的 Child Index、Task 和 New 检查状态。
代码
1.添加新文件ListActionListener.java
这是界面。
public interface ListActionListener {
// Know add was clicked on given routine
void onAddTaskClicked(Routine routine);
// Know delete was clicked on given task.
void onDeleteTaskClicked(Routine routine, Tasks task, int index);
// Know checkbox clicked on given task (with new checked status)
void onTaskCheckChanged(Routine routine, Tasks task, int index, boolean checked);
}
2.让你的activity实现这个接口。ExpandableListActivity.java
这是您在屏幕截图中看到的示例活动。
public class ExpandableListActivity extends AppCompatActivity implements ListActionListener{
ExpandableAdapter adapter;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_list);
recyclerView = findViewById(R.id.recyclerView);
loadList();
}
private void loadList() {
List<Routine> routines = getDummyRoutineList();
adapter = new ExpandableAdapter(routines, this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
private List<Routine> getDummyRoutineList() {
List<Routine> list = new ArrayList<Routine>();
Tasks rt1 = new Tasks("R1 Tasks1", 1);
Tasks rt2 = new Tasks("R1 Tasks2", 0);
Tasks rt3 = new Tasks("R1 Tasks3", 1);
Tasks rt4 = new Tasks("R1 Tasks4", 0);
Tasks rt5 = new Tasks("R1 Tasks5", 0);
List<Tasks> r1Tasks = new ArrayList<>();
r1Tasks.add(rt1);
r1Tasks.add(rt2);
r1Tasks.add(rt3);
r1Tasks.add(rt4);
r1Tasks.add(rt5);
Routine r1 = new Routine("Routine 1", r1Tasks);
Tasks r2t1 = new Tasks("R2 Tasks1", 1);
Tasks r2t2 = new Tasks("R2 Tasks2", 0);
Tasks r2t3 = new Tasks("R2 Tasks3", 1);
Tasks r2t4 = new Tasks("R2 Tasks4", 0);
Tasks r2t5 = new Tasks("R2 Tasks5", 1);
List<Tasks> r2Tasks = new ArrayList<>();
r2Tasks.add(r2t1);
r2Tasks.add(r2t2);
r2Tasks.add(r2t3);
r2Tasks.add(r2t4);
r2Tasks.add(r2t5);
Routine r2 = new Routine("Routine 2", r2Tasks);
list.add(r1);
list.add(r2);
return list;
}
@Override
public void onAddTaskClicked(Routine routine) {
Toast.makeText(this, "On Add Clicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onDeleteTaskClicked(Routine routine, Tasks task, int index) {
Toast.makeText(this, "On Delete Clicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onTaskCheckChanged(Routine routine, Tasks task, int index, boolean checked) {
Toast.makeText(this, "On Check changed:"+checked, Toast.LENGTH_SHORT).show();
}
}
3.在任务行布局中添加“X”按钮
这是我的示例 XML 文件,您的 XML 可能看起来不同。主要是添加删除按钮。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="8dp"
>
<Button
android:id="@+id/btn_delete"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="X"
/>
<CheckBox
android:id="@+id/exp_task_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/exp_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btn_delete"
android:layout_toRightOf="@+id/exp_task_checkbox"
/>
</RelativeLayout>
4.在Routine Layout文件中添加“+”按钮
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="8dp"
>
<Button
android:id="@+id/btn_add"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="+"
/>
<TextView
android:id="@+id/exp_routine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btn_delete"
android:layout_toRightOf="@+id/exp_task_checkbox"
/>
</RelativeLayout>
5. 更新 Adapter 以接受 ListActionListener
public class ExpandableAdapter extends ExpandableRecyclerViewAdapter<RoutineViewHolder, TaskViewHolder> {
ListActionListener listActionListener;
public ExpandableAdapter(List<? extends ExpandableGroup> groups, ListActionListener listActionListener) {
super(groups);
this.listActionListener = listActionListener;
}
@Override
public RoutineViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_recyclerview_routine, parent, false);
return new RoutineViewHolder(v);
}
@Override
public TaskViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_recyclerview_task, parent, false);
return new TaskViewHolder(v);
}
@Override
public void onBindChildViewHolder(TaskViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
final Tasks tasks = (Tasks) group.getItems().get(childIndex);
holder.bind((Routine)group, childIndex, tasks, listActionListener);
}
@Override
public void onBindGroupViewHolder(RoutineViewHolder holder, int flatPosition, ExpandableGroup group) {
final Routine routine = (Routine) group;
holder.bind(routine, listActionListener);
}
}
6.更新TaskViewHolder.java
接受侦听器并调用回调
public class TaskViewHolder extends ChildViewHolder {
private TextView mTextView;
private CheckBox mCheckBox;
private Boolean checkVal;
private Button btnDelete;
public TaskViewHolder(View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.exp_task);
mCheckBox=itemView.findViewById(R.id.exp_task_checkbox);
btnDelete = itemView.findViewById(R.id.btn_delete);
}
public void bind(final Routine parentRoutine, final int childIndex, final Tasks tasks, final ListActionListener listActionListener) {
mTextView.setText(tasks.name);
checkVal=((tasks.checkBox==1)?Boolean.TRUE:Boolean.FALSE);
mCheckBox.setChecked(checkVal);
//add delete button click
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listActionListener.onDeleteTaskClicked(parentRoutine, tasks, childIndex);
}
});
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
//to avoid initial call back
if(checked != checkVal) {
listActionListener.onTaskCheckChanged(parentRoutine, tasks, childIndex, checked);
checkVal = checked;
}
}
});
}
}
7.更新RoutineViewHolder.java
接受侦听器并调用回调。
public class RoutineViewHolder extends GroupViewHolder implements View.OnClickListener {
private TextView mTextView;
private Button btnAdd;
public RoutineViewHolder(View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.exp_routine);
btnAdd = itemView.findViewById(R.id.btn_add);
itemView.setOnClickListener(this);
}
public void bind(final Routine routine, final ListActionListener listActionListener) {
mTextView.setText(routine.getTitle());
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listActionListener.onAddTaskClicked(routine);
}
});
}
}
宾果游戏......运行代码...... :)