我正在尝试使用ExpendableListView实现布局。有4个组元素,每组2个孩子。第一组元素确实有0个孩子。
G
G
C
C
G
C
C
G
C
C
每个组元素有2个TextView,1个ImageView和1个Switch。 每个子元素都有2个TextView和1个SeekBar。
适配器包含2个支架。一个用于Groupviews,一个用于Childviews。
在群组图层上,一切都按预期运行。
但是在Child Layer上,不同的ChildView是相互依赖的。例如,设置Group Element3 / Child Element1的SeekBar也设置Group Element1 / Child Element0的SeekBar。
我确实理解这个问题与Android的视图缺失和视图处理(优化)有关。
这是getChildView方法:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int tempgroupPos = groupPosition;
final int tempchildPos = childPosition;
ChildViewHolder cviewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.interfaces_listrow_details_intervall, null);
cviewHolder = new ChildViewHolder();
cviewHolder.tvb = (TextView) convertView.findViewById(R.id.interfaces_child_intervall_textbig);
cviewHolder.tvs = (TextView) convertView.findViewById(R.id.interfaces_child_intervall_textexplanation);
cviewHolder.sb = (SeekBar) convertView.findViewById(R.id.interfacesSeekBar);
cviewHolder.sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int getPosition = (Integer) seekBar.getTag();
clist.get(tempgroupPos).get(tempchildPos).setProgress(seekBar.getProgress());
// do stuff
notifyDataSetChanged();
}
});
convertView.setTag(cviewHolder);
convertView.setTag(R.id.interfaces_child_intervall_textbig, cviewHolder.tvb);
convertView.setTag(R.id.interfaces_child_intervall_textexplanation, cviewHolder.tvs);
convertView.setTag(R.id.interfacesSeekBar, cviewHolder.sb);
} else {
cviewHolder = (ChildViewHolder) convertView.getTag();
}
cviewHolder.sb.setTag(groupPosition);
cviewHolder.sb.setProgress(clist.get(groupPosition).get(childPosition).getProgress());
cviewHolder.tvb.setText(clist.get(groupPosition).get(childPosition).getBigText());
cviewHolder.tvs.setText(clist.get(groupPosition).get(childPosition).getSmallTextActivated());
return convertView;
}
如果您需要代码的其他任何部分或其他信息,请询问。
我调查了这些问题/答案,但他们并没有真正帮助我个人。
答案 0 :(得分:1)
行。此外,我不确定为什么这种方法不起作用我再次查看发布到Q / A的链接。我选择了这个
Android - Expandable ListView - using ViewHolder for optimization
并完全遵循一切。现在它以这种方式工作。我专注于我的第一个选择的例子。谢谢。我的代码现在正在运行。以下是其他人可以参考的代码。
ChildViewHolder:
// https://stackoverflow.com/questions/22583689/android-expandable-listview-using-viewholder-for-optimization
static class ChildViewHolder {
private HashMap<Integer, View> storedViews = new HashMap<Integer, View>();
public ChildViewHolder addView(View view)
{
int id = view.getId();
storedViews.put(id, view);
return this;
}
public View getView(int id)
{
return storedViews.get(id);
}
}
getChildView方法:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int tempgroupPos = groupPosition;
final int tempchildPos = childPosition;
View row = convertView;
if (row == null)
{
row = inflater.inflate(R.layout.interfaces_listrow_details_intervall, parent, false);
TextView tvb = (TextView) row.findViewById(R.id.interfaces_child_intervall_textbig);
TextView tvs = (TextView) row.findViewById(R.id.interfaces_child_intervall_textexplanation);
SeekBar sb = (SeekBar) row.findViewById(R.id.interfacesSeekBar);
ChildViewHolder cholder = new ChildViewHolder();
cholder.addView(tvb);
cholder.addView(tvs);
cholder.addView(sb);
row.setTag(cholder);
}
ChildViewHolder cholder = (ChildViewHolder) row.getTag();
TextView tvb = (TextView) cholder.getView(R.id.interfaces_child_intervall_textbig);
TextView tvs = (TextView) cholder.getView(R.id.interfaces_child_intervall_textexplanation);
SeekBar sb = (SeekBar) cholder.getView(R.id.interfacesSeekBar);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
Globals g = Globals.getInstance();
clist.get(tempgroupPos-1).get(tempchildPos).setProgress(seekBar.getProgress());
// do something
notifyDataSetChanged();
}
});
tvb.setText(clist.get(groupPosition-1).get(childPosition).getBigText());
tvs.setText(clist.get(groupPosition-1).get(childPosition).getSmallTextActivated());
sb.setProgress(clist.get(groupPosition-1).get(childPosition).getProgress());
return row;
}
答案 1 :(得分:-1)
本教程是关于android中的可扩展列表视图,它有助于在组中显示数据。您可以在活动中创建多个listview,可以在click事件上展开。 Android Expandable listview的主要思想是它有组,每个组都有一些Child。可以通过单击来扩展和推卸组。
可扩展列表视图主要用于显示组中的数据。普通列表视图和列表视图之间的主要区别在于,它允许两个级别:组和子组(组是单独扩展的)。 更多信息结帐http://techlovejump.com/android-expandable-listview-tutorial/