点击子列表改变父列表
MainActivity
public class MainActivity extends AppCompatActivity { List<String> group; //组列表 List<List<String>> child; //子列表 private ExpandableListView expandistView; private SortExpandableAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expandistView = (ExpandableListView) findViewById(R.id.expandlistview); initializeData(); } private void initializeData() { group = new ArrayList<>(); child = new ArrayList<>(); addInfo("汉朝", new String[]{"刘邦", "刘彻", "刘秀"}); addInfo("唐朝", new String[]{"李渊", "李世民", "李那个啥"}); addInfo("宋朝", new String[]{"赵匡胤", "赵构", "赵那个什么"}); addInfo("明朝", new String[]{"朱元璋", "朱棣", "那个谁"}); adapter = new SortExpandableAdapter(child, this, group); expandistView.setAdapter(adapter); expandistView.setGroupIndicator(null); expandistView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { for (int i = 0, count = expandistView .getExpandableListAdapter().getGroupCount(); i < count; i++) { if (groupPosition != i) {// 关闭其他分组 expandistView.collapseGroup(i); } } } }); expandistView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView expandableListViews, View view, int groupPosition, int i1, long l) { for (int j = 0; j < expandableListViews.getExpandableListAdapter().getGroupCount(); j++) { if (groupPosition == j) {// 关闭分组 expandableListViews.collapseGroup(j); } } String text = expandableListViews.getExpandableListAdapter().getChild(groupPosition, i1).toString(); group.set(groupPosition, text); adapter.notifyDataSetChanged(); return false; } }); } /** * 模拟给组、子列表添加数据 * * @param g-group * @param c-child */ private void addInfo(String g, String[] c) { group.add(g); List<String> childitem = new ArrayList<String>(); for (int i = 0; i < c.length; i++) { childitem.add(c[i]); } child.add(childitem); } }
adapter
public class SortExpandableAdapter extends BaseExpandableListAdapter { Context context; List<String> group; //组列表 List<List<String>> child; //子列表 public SortExpandableAdapter(List<List<String>> child, Context context, List<String> group) { this.child = child; this.context = context; this.group = group; } //-----------------Child----------------// @Override public Object getChild(int groupPosition, int childPosition) { return child.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return child.get(groupPosition).size(); } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String string = child.get(groupPosition).get(childPosition); ViewHoldChild viewHoldChild = null; if (convertView==null){ viewHoldChild = new ViewHoldChild(); convertView = LayoutInflater.from(context).inflate(R.layout.view_childsort,null); viewHoldChild.textView_ch = (TextView) convertView.findViewById(R.id.text_a); convertView.setTag(viewHoldChild); }else { viewHoldChild= (ViewHoldChild) convertView.getTag(); } viewHoldChild.textView_ch.setText(string); viewHoldChild.textView_ch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Toast.makeText(context,string,Toast.LENGTH_SHORT).show(); } }); return convertView; } //----------------Group----------------// @Override public Object getGroup(int groupPosition) { return group.get(groupPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public int getGroupCount() { return group.size(); } @Override//父view public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final String string = group.get(groupPosition); ViewHoldChild viewHoldChild = null; if (convertView==null){ viewHoldChild = new ViewHoldChild(); convertView = LayoutInflater.from(context).inflate(R.layout.view_parentsort,null); viewHoldChild.textView_ch = (TextView) convertView.findViewById(R.id.text_b); convertView.setTag(viewHoldChild); }else { viewHoldChild= (ViewHoldChild) convertView.getTag(); } viewHoldChild.textView_ch.setText(string); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class ViewHoldChild { TextView textView_ch; } }