如何为列表视图适配器中的开关添加点击监听器
Posted
技术标签:
【中文标题】如何为列表视图适配器中的开关添加点击监听器【英文标题】:how to add click listener for a switch in listview adapter 【发布时间】:2021-07-09 08:25:44 【问题描述】:我有一个带有适配器的列表视图,列表视图项目是线性布局,带有包含随机数的开关,如何为开关编写点击监听器?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical">
<ListView
android:id="@+id/lster"
android:layout_
android:layout_
android:layout_margin="10dp">
</ListView>
</LinearLayout>
MainActivity.java
package com.kattah.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class MainActivity extends AppCompatActivity
ListView lv;
myadapter adp;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> RandomNumbberList = new ArrayList<>();
int i;
i = 0;
while (i < 10)
HashMap<String, String> RandomElement = new HashMap<>();
int rnd = new Random().nextInt(100000);
String num = String.valueOf(rnd);
RandomElement.put("number", num);
RandomNumbberList.add(RandomElement);
i = i + 1;
ArrayList<String> items;
items = new ArrayList<String>();
String currentrow;
for (int u = 0; u < RandomNumbberList.size(); u++)
currentrow = RandomNumbberList.get(u).toString();
items.add(currentrow);
lv = findViewById(R.id.lster);
adp = new myadapter(this, R.layout.list_row2, items, lv);
lv.setAdapter(adp);
list_row2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:padding="5dip">
<Switch
android:id="@+id/switch1"
android:layout_
android:layout_
android:text="Switch"
android:textSize="16sp" />
</LinearLayout>
myadapter.java
package com.kattah.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;
import java.util.List;
/**
* Created by Vikram Diwakar on 1/23/2018.
*/
public class myadapter extends ArrayAdapter<String>
//the list values in the List of type hero
List<String> items;
//activity context
Context context;
//the layout resource file for the list items
int resource;
ListView lsst;
//constructor initializing the values
public myadapter(Context context, int resource, List<String> items, ListView lss)
super(context, resource, items);
this.context = context;
this.resource = resource;
this.items = items;
lsst = lss;
//this will return the ListView Item as a View
@Override
public View getView(final int position, View convertView, ViewGroup parent)
//we need to get the view of the xml for our list item
//And for this we need a layoutinflater
LayoutInflater layoutInflater = LayoutInflater.from(context);
//getting the view
View view = layoutInflater.inflate(resource, null, false);
Switch sw = (Switch) view.findViewById(R.id.switch1);
// Get the text from the list and split it to get the random number
String s = items.get(position);
s = s.replace("", "");
s = s.replace("", "");
String str[] = s.split(",");
String s1[] = str[0].split("=");
sw.setText(s1[1]);
sw.setTag(position);
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
if (isChecked == true)
TerminalAdapter adpr = (TerminalAdapter) lsst.getAdapter();
final int firstListItemPosition = lsst.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + lsst.getChildCount() - 1;
for (int i = firstListItemPosition; i <= lastListItemPosition; i++)
LinearLayout ln;
ln = (LinearLayout) getViewByPosition(i, lsst);
Switch swth;
swth = (Switch) ln.getChildAt(0);
if (swth != buttonView)
swth.setChecked(false);
;
);
return view;
public View getViewByPosition(int pos, ListView listView)
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition)
return listView.getAdapter().getView(pos, null, listView);
else
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
我想知道将代码放在哪里以及如何访问列表视图中的所有开关。
【问题讨论】:
您已经有代码来响应适配器中的onCheckedChanged()
回调。究竟是什么问题?
无法访问开关按钮。
对不起,问题不是很清楚,我已经编辑了帖子。我的问题是在选择一个开关并打开时关闭所有开关。如何到达其余的开关??
数据在适配器中(在items
中)。在适配器中,您可以访问这些。
现在我再次编辑了帖子,我在适配器中添加了一个公共视图 getViewByPosition,它可以通过它的位置获取视图,我尝试检查所有开关以关闭它们。当所选的开关打开时,也不起作用。 span>
【参考方案1】:
已通过上次编辑解决!检查上面的完整代码。
【讨论】:
以上是关于如何为列表视图适配器中的开关添加点击监听器的主要内容,如果未能解决你的问题,请参考以下文章
如何为我在回收站视图中的卡片视图中的每个项目创建一个点击监听器