Android Studio 将 "Switch" 添加到 ExpandableListView 的子组
Android Studio adding "Switch" to child group of ExpandableListView
我用 textView 制作了 expandablelistview,并在子组中切换。
我想使用开关的状态(开或关)来控制连接到 ESP8266 WiFi 模块的设备。
如何存储开关数据以在其他地方使用它?
适配器java文件:
public class AdpMain extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> arrayGroup;
private HashMap<String, ArrayList<String>> arrayChild;
public AdpMain(Context context, ArrayList<String> arrayGroup, HashMap<String, ArrayList<String>> arrayChild)
{
super();
this.context = context;
this.arrayGroup = arrayGroup;
this.arrayChild = arrayChild;
}
...
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
String childName = arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);
View v = convertView;
if(v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (RelativeLayout)inflater.inflate(R.layout.activity_listview_child, null);
}
TextView textChild = (TextView)v.findViewById(R.id.textChild);
textChild.setText(childName);
return v;
}
}
您必须先从 activity_listview_child
xml
文件中在 getChildView()
中注册 switch
id。假设您在 xml
文件中的 switch
id 是 switchChild
然后使用以下代码:
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
ChildHolder childHolder;
String childName = arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_listview_child, null);
childHolder = new ChildHolder();
childHolder.textChild = (TextView)convertCiew..findViewById(R.id.textChild);
childHolder.switchChild = (Switch)convertCiew..findViewById(R.id.switchChild);
converView.setTag(childHolder)
}
else
{
childHolder = (ChildHolder)convertView.getTag();
}
childHolder.textChild.setText(childName);
childHolder.switchChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
//whatever you want do for switch code here
mListener.OnSwitchClick(groupPosition, childPosition,childHolder.switchChild.isChecked());
}
});
return convertView;
}
private static class ChildHolder
{
TextView textChild;
Switch switchChild;
}
希望对你有帮助
我用 textView 制作了 expandablelistview,并在子组中切换。 我想使用开关的状态(开或关)来控制连接到 ESP8266 WiFi 模块的设备。 如何存储开关数据以在其他地方使用它?
适配器java文件:
public class AdpMain extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> arrayGroup;
private HashMap<String, ArrayList<String>> arrayChild;
public AdpMain(Context context, ArrayList<String> arrayGroup, HashMap<String, ArrayList<String>> arrayChild)
{
super();
this.context = context;
this.arrayGroup = arrayGroup;
this.arrayChild = arrayChild;
}
...
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
String childName = arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);
View v = convertView;
if(v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (RelativeLayout)inflater.inflate(R.layout.activity_listview_child, null);
}
TextView textChild = (TextView)v.findViewById(R.id.textChild);
textChild.setText(childName);
return v;
}
}
您必须先从 activity_listview_child
xml
文件中在 getChildView()
中注册 switch
id。假设您在 xml
文件中的 switch
id 是 switchChild
然后使用以下代码:
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
ChildHolder childHolder;
String childName = arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_listview_child, null);
childHolder = new ChildHolder();
childHolder.textChild = (TextView)convertCiew..findViewById(R.id.textChild);
childHolder.switchChild = (Switch)convertCiew..findViewById(R.id.switchChild);
converView.setTag(childHolder)
}
else
{
childHolder = (ChildHolder)convertView.getTag();
}
childHolder.textChild.setText(childName);
childHolder.switchChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
//whatever you want do for switch code here
mListener.OnSwitchClick(groupPosition, childPosition,childHolder.switchChild.isChecked());
}
});
return convertView;
}
private static class ChildHolder
{
TextView textChild;
Switch switchChild;
}
希望对你有帮助