如何将 OnClickListener 设置为 Expandable RecycleView

How to set OnClickListener to Expandable RecycleView

我想将 OnClickListeners 设置为我的 Expandable Recycleview 中的项目。 Recycleview 中的每个项目都应该有一个按钮(像这样 https://imgur.com/qlEJCkk :一个用于添加任务的 + 按钮和一个用于每个任务的 "x" 按钮用于删除它)

我已经尝试从其他一些 onClickListeners 示例中实现它,但到目前为止没有任何效果

这是适配器:

public class ExpandableAdapter extends ExpandableRecyclerViewAdapter<RoutineViewHolder, TaskViewHolder> {
    public ExpandableAdapter(List<? extends ExpandableGroup> groups) {
        super(groups);
    }

    @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(tasks);

    }

    @Override
    public void onBindGroupViewHolder(RoutineViewHolder holder, int flatPosition, ExpandableGroup group) {
        final Routine routine = (Routine) group;
        holder.bind(routine);

    }

RoutineViewHolder:

import com.thoughtbot.expandablerecyclerview.viewholders.GroupViewHolder;

public class RoutineViewHolder extends GroupViewHolder implements View.OnClickListener {
    private TextView mTextView;



    public RoutineViewHolder(View itemView) {
        super(itemView);

        mTextView = itemView.findViewById(R.id.exp_routine);
        itemView.setOnClickListener(this);
    }


    public void bind(Routine routine){
        mTextView.setText(routine.getTitle());
    }


}

任务视图持有者:

public class TaskViewHolder extends ChildViewHolder {
    private TextView mTextView;
    private CheckBox mCheckBox;
    private Boolean checkVal;

    public TaskViewHolder(View itemView) {
        super(itemView);
        mTextView = itemView.findViewById(R.id.exp_task);
        mCheckBox=itemView.findViewById(R.id.exp_task_checkbox);
    }

    public void bind(Tasks tasks) {
        mTextView.setText(tasks.name);
        checkVal=((tasks.checkBox==1)?Boolean.TRUE:Boolean.FALSE);
        mCheckBox.setChecked(checkVal);
    }
}

如您所见,我有 2 个 ViewHolder:RoutineViewHolder 和 TaskViewHolder。我很困惑我应该在哪里以及如何设置 OnClickListener 因为我希望它对 "Routines" 和 "Tasks" 表现不同,因为它们会有不同的按钮。

"Tasks" 应该有 + 按钮来在它下面添加任务 每个任务都应该有一个 X 按钮来删除该特定任务

可扩展的回收视图由另外 2 个 "Tasks" 分类组成。

您是否尝试过从 activity 调用 ExpandableAdapter 对象的 setChildClickListener?

看看这个:

ExpandableAdapter adapter=new ExpandableAdapter(myExpandableGroupList);
adapter.setChildClickListener(new OnCheckChildClickListener() {
@Override
public void onCheckChildCLick(View v, boolean checked, CheckedExpandableGroup group,int childIndex) {

    }
 });

希望对您有所帮助。

我更喜欢这样一种解决方案,我可以从更高级别(如 Activity)收听所有这些回调,我可以在其中更改数据对象并刷新 RecyclerView,以根据回调保持同步。 (如果你缩放它,这最终是你需要的。)

我实现了你的代码并做了一些修改以获得预期的结果。

对于此解决方案:

  • 我创建了一个接口来获取回调
    • 在“添加”上单击“例程”
    • 在删除时单击了任务
    • 任务检查状态已更改
  • 让我的 activity 实现该接口并将其传递给适配器。
  • 适配器将其传递给 ViewHolder
  • ViewHolder 将在点击时调用所需的功能。
  • 在回调中:
    • 对于ADD:你可以知道点击了哪个Routine
    • 对于删除:可以知道ParentRoutine,Task的Child Index和Task
    • Check Change: 可以知道ParentRoutine, Child Index of Task, Task and New check status

代码

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

这是您在屏幕截图中看到的我的示例 activity。

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。更新适配器以接受 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);
            }
        });
    }
}

宾果.... 运行 密码... :)