Controls related to adapters in android ---- ExpandableListView and expandablelistview
ExpandableListView (foldable List)
1. ExpandableListView (foldable list) is similar to ListView in many places, but they use different adapters. ExpandableListView uses ExpandableAdapter, commonly used include BaseExpandableAdapter and SimpleExpandableAdapter. Common attributes:
Android: childDivider: Specifies the split line between table items of sub-classes in each group. The picture is not completely displayed. The split sub-list item is a straight line.
Android: childIndicator: The Drawable object displayed next to the sublist. It can be an image.
Android: childIndicatorEnd: end constraint position of the sub-list item indicator
Android: childIndicatorLeft: Left constraint position of the sub-list item indicator
Android: childIndicatorRight: The right constraint position of the sub-list indicator
Android: childIndicatorStart: the starting position of the sub-list indicator
Android: groupIndicator: Drawable object displayed next to the group list. It can be an image.
Android: indicatorEnd: end constraint position of the group list item indicator
Android: indicatorLeft: Left constraint position of the group list item indicator
Ii. Example
Adapter:
package com.example.test3;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ImageView;import android.widget.TextView;import java.util.ArrayList;import java.util.List;/** * Created by coder-tu on 2016/1/18. */public class MyExpandabedAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> parentData; private List<ArrayList<Item>> childData; public MyExpandabedAdapter(Context mContext, List<ArrayList<Item>> childData, List<String> parentData) { this.mContext = mContext; this.childData = childData; this.parentData = parentData; } @Override public int getGroupCount() { return parentData.size(); } @Override public int getChildrenCount(int i) { return childData.get(i).size(); } @Override public Object getGroup(int i) { return parentData.get(i); } @Override public Object getChild(int i, int i1) { return childData.get(i).get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { ViewHolder1 viewHolder1 = null; if (view == null){ viewHolder1 = new ViewHolder1(); view = LayoutInflater.from(mContext).inflate(R.layout.parent,viewGroup,false); viewHolder1.textView1 = (TextView) view.findViewById(R.id.text1); view.setTag(viewHolder1); }else{ viewHolder1 = (ViewHolder1) view.getTag(); } viewHolder1.textView1.setText(parentData.get(i)); return view; } @Override public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { ViewHolder2 viewHolder2 = null; if (view == null){ viewHolder2 = new ViewHolder2(); view = LayoutInflater.from(mContext).inflate(R.layout.item,viewGroup,false); viewHolder2.imageView2 = (ImageView) view.findViewById(R.id.image2); viewHolder2.textView2 = (TextView) view.findViewById(R.id.text2); view.setTag(viewHolder2); } else{ viewHolder2 = (ViewHolder2) view.getTag(); } viewHolder2.imageView2.setImageResource(childData.get(i).get(i1).getId()); viewHolder2.textView2.setText(childData.get(i).get(i1).getContent()); return view; } @Override public boolean isChildSelectable(int i, int i1) { return true; } class ViewHolder1{ public TextView textView1; } class ViewHolder2{ public ImageView imageView2; public TextView textView2; }}
Layout File
Java files
Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. expandableListView; import android. widget. toast; import java. util. arrayList; import java. util. list; public class MainActivity extends Activity {private ExpandableListView expandableListView; private List <String> parentData; private List <ArrayList <Item> childData; private MyExpandabedAdapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); expandableListView = (ExpandableListView) findViewById (R. id. expandableListView); parentData = new ArrayList <> (); parentData. add (new String ("Parents 1"); parentData. add (new String ("Parents 2"); parentData. add (new String ("parents 3"); // prepare ArrayList for parents 1 <Item> arrayList1 = new ArrayList <> (); arrayList1.add (new Item (R. mipmap. ic_launcher, "parent 1 -- child 1"); arrayList1.add (new Item (R. mipmap. ic_launcher, "parent 1 -- child 2"); arrayList1.add (new Item (R. mipmap. ic_launcher, "Parents 1 -- parents 3"); // prepare ArrayList for parents 2 <Item> arrayList2 = new ArrayList <> (); arrayList2.add (new Item (R. mipmap. ic_launcher, "parent 2 -- child 1"); arrayList2.add (new Item (R. mipmap. ic_launcher, "parent 2 -- child 2"); arrayList2.add (new Item (R. mipmap. ic_launcher, "parent 2 -- child 3"); // prepare ArrayList for parent 3 <Item> arrayList3 = new ArrayList <> (); arrayList3.add (new Item (R. mipmap. ic_launcher, "parent 3 -- child 1"); arrayList3.add (new Item (R. mipmap. ic_launcher, "parent 3 -- child 2"); arrayList3.add (new Item (R. mipmap. ic_launcher, "parent 3 -- child 3"); childData = new ArrayList <> (); childData. add (arrayList1); childData. add (arrayList2); childData. add (arrayList3); adapter = new MyExpandabedAdapter (MainActivity. this, childData, parentData); expandableListView. setAdapter (adapter); expandableListView. setOnChildClickListener (new ExpandableListView. onChildClickListener () {@ Override public boolean onChildClick (ExpandableListView parent, View children, int parentPosition, int childPosition, long id) {Toast. makeText (MainActivity. this, "hello", Toast. LENGTH_LONG ). show (); return false ;}});}}
: