处理 Fragment class 的 ListView 的按钮事件?

Handle button event of ListView of Fragment class?

我在我的代码中使用了两个片段。我必须在每个片段中显示一个自定义 ListView。但是我想处理这个 ListView 中的 Button 的 onClickListener 事件。 我在网上找不到如何在 Fragment class 中使用这些 Button(实际上是 ImageButton)组件。 这是我的代码:

我的片段class:

public class tabtodo extends Fragment implements View.OnClickListener {

public tabtodo() {}

private View view, view2;
private ListView lstTask;
private ImageButton btndelete;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_tabtodo, container, false);

    lstTask = (ListView) view.findViewById(R.id.listView);


    AlUpdater alUpdater = new AlUpdater(getContext());
    lstTask.setAdapter(alUpdater.getAdapterTodo());

    view2 = inflater.inflate(R.layout.row, container, false);
    btndelete = (ImageButton) view2.findViewById(R.id.btntest);
    btnTest.setOnClickListener(this);

    return mView;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btntest:
            Log.i("prova", "prova ");
            break;
        default:
            Log.i("prova", "def");
            break;
    }
}

alUpdater.getAdapterTodo() 是:

public ArrayAdapter < String > getAdapterTodo() {
    ArrayList < String > taskList = (dG).getListTodo("todo");
    if (adptodo == null) {
        adptodo = new ArrayAdapter < > (context, R.layout.row, R.id.task_title, taskList);
        adptodo.notifyDataSetChanged();
    } else {
        adptodo.clear();
        adptodo.addAll(taskList);
        adptodo.notifyDataSetChanged();
    }
    return adptodo;
}

如您所见,我正在为数组适配器使用自定义布局。 R.layout.fragment_tabtodo: 是一个只有列表视图的片段:


    <?xml version="1.0" encoding="utf-8"?>`
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".tabtodo"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:background="@color/white">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@color/white"
            android:dividerHeight="0dp"
            android:footerDividersEnabled="true"
            android:headerDividersEnabled="true">
        </ListView>
    </FrameLayout>

and finally, the XML of the row of the adapter (**R.layout.row**):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingVertical="8dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/themeselector">

        <TextView
            android:id="@+id/task_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="10dp"
            android:text="TASK"
            android:textColor="@color/white"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/btnGoToDone"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints" />

        <ImageButton
            android:id="@+id/btnGoToDone"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:backgroundTint="@android:color/transparent"
            android:onClick="switchTodoDone"
            android:src="@drawable/ic_done_icon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/btndelete"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@+id/btndelete"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="4dp"
            android:backgroundTint="@android:color/transparent"
            android:src="@drawable/ic_delete_icon"
            android:onClick="deleteTask"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

您需要为您的 ListView 创建自定义适配器。并点击那里的按钮。 查看本教程 here

我建议您使用 RecyclerView 而不是 ListView,因为它强制执行可提高性能的 ViewHolder 模式。