ListView的点击事件
# item的点击事件
设置ListVie控件的监听item事件
ListView listview = findViewById(R.id.lv_main);
listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this);
1
2
3
2
3
item的点击事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), mDataName[position], Toast.LENGTH_LONG).show();
// 获得所点击行的数据
AppInfo itemInfo = (AppInfo) parent.getItemAtPosition(position);
itemInfo.setAppName("iqqcode");
itemInfo.setAppSize(randSize(100));
Log.i(TAG, "---------onItemClick: itemInfo" + itemInfo.toString());
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("itemInfo", itemInfo);
intent.putExtras(bundle);
startActivity(intent);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
item长按的点击事件
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), mDataName[position], Toast.LENGTH_LONG).show();
// 获得所点击行的数据
AppInfo itemInfo = (AppInfo) parent.getItemAtPosition(position);
itemInfo.setAppName("iqqcode");
itemInfo.setAppSize(randSize(100));
Log.i(TAG, "---------onItemClick: itemInfo" + itemInfo.toString());
Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("itemInfo", itemInfo);
intent.putExtras(bundle);
startActivity(intent);
return true; // 消化该点击事件
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# item上Button的点击事件(回调接口)
# 解决item点击反馈
问题:当点击某一行的时候,没有背景色的变化
- 原因:行中包含了按钮,Button的优先级较高,抢夺了焦点
- 解决方案:夺回焦点,在item布局的最外层标签设置
descendantFocusability=blocksDescendants
属性
# 设置Button的点击监听
- 首先定义点击监听的接口
- Adapter中声明接口类型的引用
- Adapter中设置item中Button的点击事件
- Activity实现接口方法,并且设置点击事件
1. Adapter中定义接口
public interface IClickButton {
void clickButton(int position);
}
1
2
3
2
3
2. Adapter中声明引用
// 接口类型的引用
IClickButton clickItemButton;
1
2
2
3. Adapter的getView方法设置点击事件
// 设置Button的点击,提供在Activity中调用
public void setClickButton(IClickButton clickItemButton) {
this.clickItemButton = clickItemButton;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// item中Button的点击事件
Button btn = convertView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickItemButton.clickButton(position);
}
});
// .....
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4. Activity中调用setButton方法
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, IClickButton {
private List<AppInfo> list;
private AppInfo info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// .
mListView = findViewById(R.id.list_view);
MyBaseAdapter adapter = new MyBaseAdapter(this, list);
adapter.setClickButton(this);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(this);
mListView.setOnItemLongClickListener(this);
}
@Override
public void clickButton(int position) {
startActivity(new Intent(MainActivity.this, UninstallActivity.class));
}
}
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
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
编辑 (opens new window)
上次更新: 2021/06/02, 10:01:48