我无法在expandablelistview中使用自定义布局,只需点击一个孩子就可以刷新。
在示例中,您有方法MyExpandableListAdapter - > getChildView,其中孩子膨胀自定义布局R.layout.list_out_item(什么是文本视图和按钮)。
现在我希望如果我点击一个按钮,该子视图的子节点和父节点就会更新。我怎样才能做到这一点?我正在考虑刷新父母和孩子,因为崩溃迫使更新,但也欢迎其他解决方案。
例如
public class Sample extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
}
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Map<Ability, Integer> abilityMods;
private List<Skill> skills = SkillList();
private List<Skill> SkillList() {
List<Skill> skills = new Vector<Skill>();
abilityMods = Player.getAbilityModifier();
skills.add(new Skill(abilityMods, "Appraise", 2, Ability.Dex, 2));
skills.add(new Skill(abilityMods, "Autohypnosis", 0, Ability.Int,
-7));
skills.add(new Skill(abilityMods, "Craft", 0, Ability.Dex, 0));
return skills;
}
public MyExpandableListAdapter() {
super();
abilityMods = Player.getAbilityModifier();
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return 1;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, final ViewGroup parent) {
LayoutInflater infalInflater = getLayoutInflater();
convertView = infalInflater.inflate(R.layout.list_out_item, null);
final Skill skill = getChild(groupPosition, childPosition);
TextView tv = (TextView) convertView.findViewById(R.id.abilityMod);
tv.setText(toString(skill.abillityMod()));
Button rankMod = (Button) convertView.findViewById(R.id.rankMod);
rankMod.setText(toString(skill.Ranks));
rankMod.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
skill.Ranks++;
}
});
return convertView;
}
private String toString(int value) {
if (value >= 0)
return ("+" + String.valueOf(value));
else
return (String.valueOf(value));
}
public Skill getGroup(int groupPosition) {
return skills.get(groupPosition);
}
@Override
public Skill getChild(int groupPosition, int childPosition) {
return getGroup(groupPosition);
}
public int getGroupCount() {
return skills.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater infalInflater = getLayoutInflater();
convertView = infalInflater
.inflate(R.layout.list_header_item, null);
TextView skillName = (TextView) convertView
.findViewById(R.id.skillName);
Skill skill = getGroup(groupPosition);
TextView skillPower = (TextView) convertView
.findViewById(R.id.skillPower);
int TotalMod = skill.TotalModifier();
if (TotalMod >= 0)
skillPower.setText("+" + String.valueOf(TotalMod));
else
skillPower.setText(String.valueOf(TotalMod));
return convertView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
答案 0 :(得分:0)
查看自定义BaseExpandableListViewAdapter
的{{3}}链接。使用List<View>
存储父视图,单击btn时,获取父视图并操作视图。