我有一个ExpandableListView;列表的单个项目(组项)具有包含ToggleButton的FrameLayout。我这样做是为了增加按钮的触摸区域。我使用按钮展开它所属的组项的子视图
一次不能有两个选中的按钮,因为当检查一个按钮时,检查的前一个按钮将被取消选中。
现在,我第一次点击某个项目的按钮时,它切换正常;之后,当我点击另一个项目之一时,切换的按钮是位于"点击按钮的位置" +1。
但是,如果我点击了最后一个项目的按钮,那么将被切换的按钮是位于当前切换按钮的位置"的按钮" +1(这意味着,如果我首先切换项目1,然后单击最后一项的按钮,项目2将切换;再次单击最后一项将切换项目3,然后项目4,项目5等等直到最后,最后一项将切换。)
这里是适配器的代码:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public List<Item> names;//Item is a custom object
public boolean[] buttons;
public int lastChecked;
private final int imageId = R.drawable.item1;
private ExpandableListView list;
public CustomList(Activity context, List<Item> names, ExpandableListView theListView) {
this.context = context;
this.names = names;
this.buttons = new boolean[names.size()];
this.lastChecked = -1;
this.list = theListView;
}
private static class GroupHolder {
TextView txtTitle;
TextView txtData;
ImageView imageView;
ToggleButton toggle;
FrameLayout frame;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_single, null);
holder = new GroupHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.text);
holder.txtData = (TextView) convertView.findViewById(R.id.numberOfFiles);
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);
holder.frame.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(lastChecked != -1) {//if there's a checked button
buttons[lastChecked] = !buttons[lastChecked];//uncheck it
if(position == lastChecked)//if I clicked on the last checked button
lastChecked = -1;//there's no checked button
else {
buttons[position] = !buttons[position];//check the button
lastChecked = position;//and set it as the last checked one
}
notifyList();
}
return false;
}
});
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.txtTitle.setText(names.get(position).getName());
holder.txtData.setText(names.get(position).getData());
holder.imageView.setImageResource(imageId);
holder.toggle.setChecked(buttons[position]);
if(holder.toggle.isChecked()) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
list.expandGroup(position);
else
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
return convertView;
}
private static class ChildHolder {
TextView details;
TextView send;
TextView other;
}
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
holder = new ChildHolder();
holder.details = (TextView) convertView.findViewById(R.id.details);
holder.send = (TextView) convertView.findViewById(R.id.send);
holder.other = (TextView) convertView.findViewById(R.id.other);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
holder.details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//*call some method in the main activity*
}
});
holder.send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//*call some method in the main activity*
}
});
holder.other.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//*call some method in the main activity*
}
});
return convertView;
}
public void notifyList() {
this.notifyDataSetChanged();
}
如您所见,在getGroupView中,子项目会根据布尔数组中与列表中其视图位置对应的项的值进行展开或折叠。我调用notifyDataSetChanged()来更新列表 在主活动中,我将onGroupClickListener设置为ExpandableListView并在onGroupClick中返回true,以便在单击组项时不展开/折叠子视图(因为我使用了ToggleButtons)。
答案 0 :(得分:2)
好吧,这是一个非常蹩脚的错误。每次调用getGroupView时,我都必须为FrameLayout设置OnTouchListener,而不仅仅是第一次。所以该方法的正确代码是:
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_single, null);
holder = new GroupHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.text);
holder.txtData = (TextView) convertView.findViewById(R.id.numberOfFiles);
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.txtTitle.setText(names.get(position).getName());
holder.txtData.setText(names.get(position).getData());
holder.imageView.setImageResource(imageId);
holder.frame.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(lastChecked != -1) {//if there's a checked button
buttons[lastChecked] = !buttons[lastChecked];//uncheck it
if(position == lastChecked)//if I clicked on the last checked button
lastChecked = -1;//there's no checked button
else {
buttons[position] = !buttons[position];//check the button
lastChecked = position;//and set it as the last checked one
}
notifyList();
}
return false;
}
});
holder.toggle.setChecked(buttons[position]);
if(holder.toggle.isChecked()) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
list.expandGroup(position);
else
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
return convertView;
}