是否可以在活动中为自定义listView项设置onClickListener?

时间:2017-05-25 20:27:05

标签: android listview onclicklistener

是否可以为活动中的listView项按钮绑定onClickListener?我的listView项目有名称,删除和编辑按钮。 listView项目存储在List中。

exercises_list_view_item.xml

"bundles": [
  {
    "name": "app-bundle.js",
    "source": {
      "include": [
        "**/src/main.js",
        "**/src/app.js",
        "**/src/environment.js",
        "**/src/app.{html,css}"
      ]
    }
  },
  ...
  {
    "name": "vendor-bundle.js",
    "dependencies": [
      ...
      {
        "name": "lodash",
        "path": "../node_modules/lodash",
        "main": "index"
      },
      {
        "name": "xmlbuilder",
        "path": "../node_modules/xmlbuilder",
        "main": "lib/index"
      },
      {
        "name": "aws-sdk",
        "path": "../node_modules/aws-sdk",
        "main": "dist/aws-sdk",
        "exports": "AWS"
      }
    ],
    "append": [
      "node_modules/amazon-cognito-identity-js/dist/aws-cognito-sdk.min.js",
      "node_modules/amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js"
    ]
  },
  ...
]

活动代码:

    <?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="40dp"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <Button
        android:id="@+id/deleteExerciseBtn"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:background="@android:drawable/ic_delete" />

    <Button
        android:id="@+id/editExerciseBtn"
        android:layout_width="26dp"
        android:layout_height="26dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="7dp"
        android:layout_toLeftOf="@id/deleteExerciseBtn"
        android:background="@drawable/edit_btn" />

    <TextView
        android:id="@+id/exerciseName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:layout_toLeftOf="@id/editExerciseBtn"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="This is a Large text with fill width"
        android:textColor="#000000"
        android:textSize="18sp" />

</RelativeLayout>

由于WorkoutExerciseListAdapted有很多代码,我在这里贴了它:https://pastebin.com/FYNVVQnS

我想做的是: 如果用户单击编辑按钮,我需要将Exercise对象传递给另一个活动进行编辑。 如果用户点击删除按钮,我需要从列表&lt;&gt;中删除该项目并更新listView(使用notifyDataSetChanged()?)

我试图通过stackoverflow和google寻找答案,但它要么无效,要么我的应用程序开始崩溃。 几个尝试过的例子: http://www.c-sharpcorner.com/UploadFile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/ Adding an onclicklistener to listview (android) 和其他人..

我很感激任何建议。谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

1。更新您的customButtonListener界面,如下所示:

public interface customButtonListener {
    public void onEditButtonClickListner(int position, String value);
    public void onDeleteButtonClickListner(int position);
}

2。在适配器getView()方法设置中,点击了editdelete按钮的监听器:

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {

    ............
    ...................

    // Edit
    viewHolder.editBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (customListner != null) {
                customListner.onEditButtonClickListner(position, getItem(position).getExerciseName());
            }
        }
    });

    // Delete
    viewHolder.deleteBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (customListner != null) {
                customListner.onDeleteButtonClickListner(position);
            }
        }
    });

    return convertView;
}

3。在您的活动中,将CustomButtonListener添加到您的ListView

A. Get item position from `onEditButtonClickListner()` and get `workoutExercise` object and pass it to another activity 
B. Get item position from `onDeleteButtonClickListner()` and delete item and upadte ListView.

在您的活动中添加以下代码:

    ..........
    .................

    listAdapter.setCustomButtonListener(create_workout.this);
    exercisesList.setAdapter(listAdapter);

    exercisesList.setCustomButtonListener(new WorkoutExerciseListAdapter.customButtonListener() {

        @Override
        public void onEditButtonClickListner(int position, String value) 
        {
            // Item
            WorkoutExercise workoutExercise = workoutExercises.get(position);

            // Do something with object workoutExercise 
        }

        @Override
        public void onDeleteButtonClickListner(int position) 
        {
            // Delete
            workoutExercises.remove(position);

            // Update ListView
            listAdapter.notifyDataSetChanged();
        }
    });

希望这会有所帮助〜

答案 1 :(得分:0)

如果您不想为列表视图中的每个列表项启动任何事件,那么您应该在 BaseAdapter 类中实现它。 在 WorkoutExerciseListAdapter 上,在 getView ()内部定义您的事件列表器。

如果您需要在活动上获取点击事件,请实施并使用界面,如果是,则在WorkoutExerciseListAdapter

MyNotifier listner;

public interface MyNotifier
{
  void OnButtonClicked;
}

public void setMyNotificationListner(MyNotifier listner)
{
  this.listner=listner;
}

然后按下按钮

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
     Button b=(Button)v.getViewByID(<id>);
     b.setOnClickListner(new OnClickListner...
     {
       listner.OnButtonClicked()
     }
    }

在MainActivity

WorkoutExerciseListAdapter listAdapter = new WorkoutExerciseListAdapter(this, R.layout.exercises_list_view_item, workoutExercises);
listAdapter.setMyNotificationListner(new MyNotifier {..//You get click calls here..};)