自定义可扩展列表视图中 ToggleButton 的状态
在我的应用程序中,我使用自定义 ExpandableListView
。在 ExpandableListView
中,每个子项都有一个 ToggleButton
。当我展开和折叠其他组时,ToggleButton
的状态不会保持不变。
我的适配器代码是:
public class SettingsExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private Activity mActivity;
private LayoutInflater mInflater;
private String[] mGroups = null;
private Typeface font;
private String mChildren[][] ;
private boolean mStates[] [];
public SettingsExpandableListAdapter(Activity activity, String[] groups, String children[][],boolean [][] states)
{
mActivity = activity;
mGroups = groups;
mChildren= children;
mContext = activity;
mStates = states;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mChildren[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
final String children= (String) getChild(groupPosition, childPosition);
final boolean state = mStates[groupPosition][childPosition];
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_child_row, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.childName);
ToggleButton tb = (ToggleButton)convertView.findViewById(R.id.togbtn);
if(state)
{
tb.setChecked(true);
}else
tb.setChecked(false);
tv.setText(children);
tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
if(isChecked)
{
Settings.states[groupPosition][childPosition]=false;
mStates[groupPosition][childPosition] = false;
}else
{
Settings.states[groupPosition][childPosition]=true;
mStates[groupPosition][childPosition] = true;
}
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return mChildren[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return mGroups[groupPosition];
}
@Override
public int getGroupCount() {
return mGroups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_group_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
tv.setText(group);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
In my application I am using custom ExpandableListView
. In the ExpandableListView
there is a ToggleButton
in every child. states of the ToggleButton
are not remain unchanged when I expand and collapse other groups.
My adapter code is:
public class SettingsExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private Activity mActivity;
private LayoutInflater mInflater;
private String[] mGroups = null;
private Typeface font;
private String mChildren[][] ;
private boolean mStates[] [];
public SettingsExpandableListAdapter(Activity activity, String[] groups, String children[][],boolean [][] states)
{
mActivity = activity;
mGroups = groups;
mChildren= children;
mContext = activity;
mStates = states;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mChildren[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
final String children= (String) getChild(groupPosition, childPosition);
final boolean state = mStates[groupPosition][childPosition];
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_child_row, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.childName);
ToggleButton tb = (ToggleButton)convertView.findViewById(R.id.togbtn);
if(state)
{
tb.setChecked(true);
}else
tb.setChecked(false);
tv.setText(children);
tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
if(isChecked)
{
Settings.states[groupPosition][childPosition]=false;
mStates[groupPosition][childPosition] = false;
}else
{
Settings.states[groupPosition][childPosition]=true;
mStates[groupPosition][childPosition] = true;
}
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return mChildren[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return mGroups[groupPosition];
}
@Override
public int getGroupCount() {
return mGroups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_group_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
tv.setText(group);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 getChildView 中删除条件
if (convertView == null)
Remove condition
if (convertView == null)
from getChildView