Android ExpandableListView children 相互依赖也没有 ViewHolder
Android ExpandableListView children interdepending also without ViewHolder
我实现了一个小应用程序并检查了一些 ExpandableListView
我在一个更大的应用程序中存在的错误。我阅读并完成了 this and that post。但是后来我决定在第一个 运行.
中实现没有 ViewHolder 模式的 ExpandableListView
适配器
但我在打开和关闭 parent 视图时仍然存在错误。这是我的 MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.exp_list_view);
List<Parent> parentList = new ArrayList<>();
Parent parent;
List<Child> childList;
childList = new ArrayList<>();
childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three"));
parent = new Parent("First", childList);
parentList.add(parent);
childList = new ArrayList<>();
childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three"));
parent = new Parent("Second", childList);
parentList.add(parent);
childList = new ArrayList<>();
childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three"));
parent = new Parent("Third", childList);
parentList.add(parent);
childList = new ArrayList<>();
childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three"));
parent = new Parent("Forth", childList);
parentList.add(parent);
final MyAdapter adapter = new MyAdapter(this, parentList);
expandableListView.setAdapter(adapter);
}
}
我的适配器class:
class MyAdapter extends BaseExpandableListAdapter {
private final List<Parent> mParentList;
private final Context mContext;
public MyAdapter(final Context context, final List<Parent> parentList) {
mContext = context;
mParentList = parentList;
}
@Override
public int getGroupCount() {
return mParentList.size();
}
@Override
public int getChildrenCount(final int groupPosition) {
return mParentList.get(groupPosition).getChildList().size();
}
@Override
public Object getGroup(final int groupPosition) {
return mParentList.get(groupPosition);
}
@Override
public Object getChild(final int groupPosition, final int childPosition) {
return mParentList.get(groupPosition).getChildList().get(childPosition);
}
@Override
public long getGroupId(final int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(final int groupPosition, final int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.row_parent, parent, false);
}
TextView tvn = (TextView) v.findViewById(R.id.tv_parent_name);
tvn.setText(mParentList.get(groupPosition).getName());
return v;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, final View convertView, final ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(R.layout.row_child, parent, false);
}
TextView tvn = (TextView) row.findViewById(R.id.tv_child_name);
Switch sw = (Switch) row.findViewById(R.id.sw_child);
tvn.setText(mParentList.get(groupPosition).getChildList().get(childPosition).getName());
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
Log.v(getClass().getName(), "onCheckedChanged() (" + groupPosition + ", " + childPosition + ")");
}
});
return row;
}
@Override
public boolean isChildSelectable(final int groupPosition, final int childPosition) {
return false;
}
还有我的两个模型 classes。 Parent:
public class Parent {
private String name;
private List<Child> childList = new ArrayList<>();
public Parent(final String name, List<Child> childList) {
this.name = name;
this.childList = childList;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public List<Child> getChildList() {
return childList;
}
public void setChildList(final List<Child> childList) {
this.childList = childList;
}
}
Child:
public class Child {
private String name;
public Child(final String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
现在我在 运行 宁 activity 上观察到以下问题。请关注:
- 展开所有 parent(从第一个到第四个)
- 单击开关从 parent 第二和第四(从上到下)
- 展开所有 parent(从第一个到第四个)
- 展开所有 parent(从第一个到第四个)
观察:
- 第二个parent: 只选了一个和两个
- 第三个parent:一、二、三被选中
- 第四parent: 只选了两个
这里出了什么问题?同样实现 ViewHolder 模式在这里也不起作用。
我必须做两件事:
- 用状态扩展 child 并跟踪它
- 去掉
getChildView()
和getGroupView
中的if-Statements
我的新 child class 现在是:
public class Child {
private String name;
private boolean state;
public Child(final String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public boolean isState() {
return state;
}
public void setState(final boolean state) {
this.state = state;
}
}
我的新getChildView()
方法:
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, final View convertView, final ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.child_row, parent, false);
final Switch sw = (Switch) view.findViewById(R.id.sw);
TextView textViewChildName = (TextView) view.findViewById(R.id.childname);
String name = ((Child) getChild(groupPosition, childPosition)).getName();
sw.setChecked(list.get(groupPosition).getChildList().get(childPosition).isState());
textViewChildName.setText(name);
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
list.get(groupPosition).getChildList().get(childPosition).setState(isChecked);
sw.setChecked(isChecked);
}
});
return view;
}
最后 getGroupView()
:
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.group_row, parent, false);
TextView textView = (TextView) view.findViewById(R.id.groupname);
String name = ((Parent) getGroup(groupPosition)).getName();
textView.setText(name);
return view;
}
我不得不放弃 ViewHolder 模式。它对我不起作用,因为我正在使用开关并想保持状态。
我实现了一个小应用程序并检查了一些 ExpandableListView
我在一个更大的应用程序中存在的错误。我阅读并完成了 this and that post。但是后来我决定在第一个 运行.
ExpandableListView
适配器
但我在打开和关闭 parent 视图时仍然存在错误。这是我的 MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.exp_list_view);
List<Parent> parentList = new ArrayList<>();
Parent parent;
List<Child> childList;
childList = new ArrayList<>();
childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three"));
parent = new Parent("First", childList);
parentList.add(parent);
childList = new ArrayList<>();
childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three"));
parent = new Parent("Second", childList);
parentList.add(parent);
childList = new ArrayList<>();
childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three"));
parent = new Parent("Third", childList);
parentList.add(parent);
childList = new ArrayList<>();
childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three"));
parent = new Parent("Forth", childList);
parentList.add(parent);
final MyAdapter adapter = new MyAdapter(this, parentList);
expandableListView.setAdapter(adapter);
}
}
我的适配器class:
class MyAdapter extends BaseExpandableListAdapter {
private final List<Parent> mParentList;
private final Context mContext;
public MyAdapter(final Context context, final List<Parent> parentList) {
mContext = context;
mParentList = parentList;
}
@Override
public int getGroupCount() {
return mParentList.size();
}
@Override
public int getChildrenCount(final int groupPosition) {
return mParentList.get(groupPosition).getChildList().size();
}
@Override
public Object getGroup(final int groupPosition) {
return mParentList.get(groupPosition);
}
@Override
public Object getChild(final int groupPosition, final int childPosition) {
return mParentList.get(groupPosition).getChildList().get(childPosition);
}
@Override
public long getGroupId(final int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(final int groupPosition, final int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.row_parent, parent, false);
}
TextView tvn = (TextView) v.findViewById(R.id.tv_parent_name);
tvn.setText(mParentList.get(groupPosition).getName());
return v;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, final View convertView, final ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(R.layout.row_child, parent, false);
}
TextView tvn = (TextView) row.findViewById(R.id.tv_child_name);
Switch sw = (Switch) row.findViewById(R.id.sw_child);
tvn.setText(mParentList.get(groupPosition).getChildList().get(childPosition).getName());
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
Log.v(getClass().getName(), "onCheckedChanged() (" + groupPosition + ", " + childPosition + ")");
}
});
return row;
}
@Override
public boolean isChildSelectable(final int groupPosition, final int childPosition) {
return false;
}
还有我的两个模型 classes。 Parent:
public class Parent {
private String name;
private List<Child> childList = new ArrayList<>();
public Parent(final String name, List<Child> childList) {
this.name = name;
this.childList = childList;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public List<Child> getChildList() {
return childList;
}
public void setChildList(final List<Child> childList) {
this.childList = childList;
}
}
Child:
public class Child {
private String name;
public Child(final String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
现在我在 运行 宁 activity 上观察到以下问题。请关注:
- 展开所有 parent(从第一个到第四个)
- 单击开关从 parent 第二和第四(从上到下)
- 展开所有 parent(从第一个到第四个)
- 展开所有 parent(从第一个到第四个)
观察:
- 第二个parent: 只选了一个和两个
- 第三个parent:一、二、三被选中
- 第四parent: 只选了两个
这里出了什么问题?同样实现 ViewHolder 模式在这里也不起作用。
我必须做两件事:
- 用状态扩展 child 并跟踪它
- 去掉
getChildView()
和getGroupView
中的if-Statements
我的新 child class 现在是:
public class Child {
private String name;
private boolean state;
public Child(final String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public boolean isState() {
return state;
}
public void setState(final boolean state) {
this.state = state;
}
}
我的新getChildView()
方法:
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, final View convertView, final ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.child_row, parent, false);
final Switch sw = (Switch) view.findViewById(R.id.sw);
TextView textViewChildName = (TextView) view.findViewById(R.id.childname);
String name = ((Child) getChild(groupPosition, childPosition)).getName();
sw.setChecked(list.get(groupPosition).getChildList().get(childPosition).isState());
textViewChildName.setText(name);
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
list.get(groupPosition).getChildList().get(childPosition).setState(isChecked);
sw.setChecked(isChecked);
}
});
return view;
}
最后 getGroupView()
:
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.group_row, parent, false);
TextView textView = (TextView) view.findViewById(R.id.groupname);
String name = ((Parent) getGroup(groupPosition)).getName();
textView.setText(name);
return view;
}
我不得不放弃 ViewHolder 模式。它对我不起作用,因为我正在使用开关并想保持状态。