场景
场景1
Item中包含若干个Button,且Button需要响应点击事件。
此时实现思路可以是通过接口回调或者抽象类的形式来实现Button的点击事件。
场景2
Item和Item中的Button均需要响应点击事件。
此时需要在item.xml中根目录设置属性android:descendantFocusability=”blocksDescendants”,并且设置Button按钮
android:focusable=”false”属性,这样可以同时响应item的点击事件和item中Button的点击事件。
实现方式
接口回调
通过接口回调实现Item中Button的点击事件,思路是在适配器中定义一个接口,接口中定义一个抽象方法,并在适配器中实现View.OnClickListener,在重载的onClick方法中调用接口中的方法,在Activity层实现适配器中定义的接口,通过接口回调的方式实现按钮点击事件。
源码
适配器
package com.example.a002034.listviewitemclickdemo.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.example.a002034.listviewitemclickdemo.R;
import java.util.ArrayList;
import java.util.List;
/**
* 使用接口回调的方式实现button点击事件.
*
* @author xuzhuyun
*/
@SuppressWarnings("unused")
public class MyAdapter extends BaseAdapter implements View.OnClickListener {
List<String> mStringList = new ArrayList();
private Context mContext;
private Callback mCallback;
public MyAdapter(Context context, Callback callback) {
mContext = context;
mCallback = callback;
}
public void setStringList(List<String> stringList) {
mStringList = stringList;
}
public List<String> getStringList() {
return mStringList;
}
@Override
public int getCount() {
return mStringList.size();
}
@Override
public Object getItem(int i) {
return getCount() == 0 ? null : mStringList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.lv_test_item, null);
viewHolder = new ViewHolder();
viewHolder.mTvItemName = view.findViewById(R.id.tv_name);
viewHolder.mBtnClick = view.findViewById(R.id.btn_click);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.mTvItemName.setText(mStringList.get(i));
viewHolder.mBtnClick.setOnClickListener(this);
viewHolder.mBtnClick.setTag(i);//保存当前点击按钮的位置
return view;
}
class ViewHolder {
TextView mTvItemName;
Button mBtnClick;
}
@Override
public void onClick(View view) {
mCallback.click(view);
}
/**
* 回调接口.
*/
public interface Callback {
void click(View v);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Activity
package com.example.a002034.listviewitemclickdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.a002034.listviewitemclickdemo.adapter.MyAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements MyAdapter.Callback, AdapterView.OnItemClickListener {
private ListView mListView;
private MyAdapter mMyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.lv_test);
List<String> mList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
mList.add("测试item" + i);
}
mMyAdapter = new MyAdapter(this, this);
mMyAdapter.setStringList(mList);
mListView.setAdapter(mMyAdapter);
mListView.setOnItemClickListener(this);
}
@Override
public void click(View v) {
int position = (int) v.getTag();
String clickItemText = mMyAdapter.getStringList().get(position);
Toast.makeText(this, "button click -->"+clickItemText, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this,"listview item click -- >"
+ mMyAdapter.getStringList().get(i), Toast.LENGTH_SHORT).show();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
使用抽象类回调
定义一个抽象类继承自View.OnClickListener,在Activity中实例化该抽象类,并在Adapter实例化的时候传入adapter,然后Button绑定点击事件时,即可传入该抽象类,通过抽象类完成点击事件的回调。
源码
适配器
package com.example.a002034.listviewitemclickdemo.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.example.a002034.listviewitemclickdemo.R;
import java.util.ArrayList;
import java.util.List;
/**
* 使用接口回调的方式实现button点击事件.
*
* @author xuzhuyun
*/
@SuppressWarnings("unused")
public class MyAdapter2 extends BaseAdapter {
List<String> mStringList = new ArrayList();
private Context mContext;
private MyClickListener mMyClickListener;
public MyAdapter2(Context context, MyClickListener myClickListener) {
mContext = context;
mMyClickListener = myClickListener;
}
public void setStringList(List<String> stringList) {
mStringList = stringList;
}
public List<String> getStringList() {
return mStringList;
}
@Override
public int getCount() {
return mStringList.size();
}
@Override
public Object getItem(int i) {
return getCount() == 0 ? null : mStringList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.lv_test_item, null);
viewHolder = new ViewHolder();
viewHolder.mTvItemName = view.findViewById(R.id.tv_name);
viewHolder.mBtnClick = view.findViewById(R.id.btn_click);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.mTvItemName.setText(mStringList.get(i));
viewHolder.mBtnClick.setTag(i);
viewHolder.mBtnClick.setOnClickListener(mMyClickListener);
return view;
}
class ViewHolder {
TextView mTvItemName;
Button mBtnClick;
}
public static abstract class MyClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
myOnClick((Integer) view.getTag(), view);
}
public abstract void myOnClick(int position, View v);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Activity
package com.example.a002034.listviewitemclickdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.a002034.listviewitemclickdemo.adapter.MyAdapter;
import com.example.a002034.listviewitemclickdemo.adapter.MyAdapter2;
import java.util.ArrayList;
import java.util.List;
public class MainActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener {
private ListView mListView;
private MyAdapter2 mMyAdapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.lv_test);
List<String> mList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
mList.add("测试item" + i);
}
mMyAdapter2 = new MyAdapter2(this, mMyClickListener);
mMyAdapter2.setStringList(mList);
mListView.setAdapter(mMyAdapter2);
mListView.setOnItemClickListener(this);
}
private MyAdapter2.MyClickListener mMyClickListener = new MyAdapter2.MyClickListener() {
@Override
public void myOnClick(int position, View v) {
Toast.makeText(MainActivity2.this, "button click -- >" + mMyAdapter2.getStringList().get(position), Toast.LENGTH_SHORT).show();
}
};
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this, "listview item click -- >" + mMyAdapter2.getStringList().get(i), Toast.LENGTH_SHORT).show();
}
}
---------------------
作者:hgncxzy_
来源:CSDN
原文:https://blog.csdn.net/jdfkldjlkjdl/article/details/80868784
版权声明:本文为博主原创文章,转载请附上博文链接!