Use expandableListView to implement a three-level list,

Source: Internet
Author: User

Use expandableListView to implement a three-level list,

Time passes quickly, and in the twinkling of an eye, it is always inexplicable in the long river of time, maybe time is really the most cruel, whether you like or sad, happy and lacking, he keeps moving. Not to mention nonsense. On the day before the commencement of work (yesterday), Chen wrote a demo to implement a three-level list through expandableListView, and there was no time to finish the demo yesterday.

First, let's take a look:

We all know that two levels of list can be implemented through expandableListView. How can we implement a three-level list? In fact, it is also simple. We can implement it through expandableListView nesting expandableListView.

The use of ExpendableListView is almost the same as that of ListView:

Write layout files and obtainExpendableListView. Custom adapter inheritanceBaseExpandableListAdapter.SetAdapter: Set the adapter.

Implementation is started as follows:

Write layout files and search for controls

  

CustomThe Adapter inherits fromBaseExpandableListAdapter. Because the real expandableListView is nested, we also need to define two adapters, which are called OuterAdapter and InnerAdapter respectively.

Before writing this class, we must have our entity class. Let's take a look at the country, province, and city entities we define.And set the set and get methods respectively.

/*** Country entity class* Created by Administrator on 2018/2/21.*/Public class CountyEntity {// national name private String name; // National province collection private List  Provinces; public String getName () {return name;} public void setName (String name) {this. name = name;} public List  GetProvinces () {return provinces;} public void setProvinces (List  Provinces) {this. provinces = provinces ;} /** * Saves entity classes */  Static class Province {// Province name private String name; // Province's urban collection private List  Cities; public String getName () {return name;} public void setName (String name) {this. name = name;} public List  GetCities () {return cities;} public void setCities (List  Cities) {this. cities = cities ;}} /** * City entity */  Static class City {// City name private String name; public String getName () {return name;} public void setName (String name) {this. name = name ;}}}      

OK. After writing the object class, go directlyThe Code of OuterAdapter and InnerAdapter.

OuterAdaper:

Public class OutAdapter extends BaseExpandableListAdapter {private List  CountyEntities; // a collection of private activities in the country; // create a layout and use the // constructor public OutAdapter (List  CountyEntities, Activity activity) {if (countyEntities! = Null) {this. countyEntities = countyEntities;} else countyEntities = new ArrayList <> (); this. activity = activity;} // number of groups @ Override public int getGroupCount () {return countyEntities. size () ;}// number of sub-views @ Override public int getChildrenCount (int I) {return 1 ;}// obtain the Group Object @ Override public Object getGroup (int I) {return countyEntities. get (I);} // obtain the subview Object @ Override public Object getChild (int I, int i1) {return countyEntities. get (I ). getProvinces (). get (i1) ;}// group View subscript @ Override public long getGroupId (int I) {return I ;}// subview subscript @ Override public long getChildId (int I, int i1) {return i1 ;}@ Override public boolean hasStableIds () {return false ;}// obtain the group layout @ Override public View getGroupView (int I, boolean B, View view, viewGroup viewGroup) {TextView textView = new TextView (activity); AbsListView. layoutParams params = new AbsListView. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. LayoutParams. WRAP_CONTENT); TextView. setLayoutParams (params); textView. setLeft (20); textView. setPadding (90, 15, 10, 15); textView. setGravity (Gravity. CENTER_VERTICAL| Gravity. LEFT); TextView. setText (countyEntities. get (I ). getName (); return textView;} // obtain the subview @ Override public View getChildView (int I, int i1, boolean B, View, ViewGroup viewGroup) {AbsListView. layoutParams params = new AbsListView. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. LayoutParams. WRAP_CONTENT); InnerExpandableListView expandableListView = new InnerExpandableListView (activity); expandableListView. setLayoutParams (params); InnerAdapter adapter = new InnerAdapter (countyEntities. get (I ). getProvinces (), activity); expandableListView. setAdapter (adapter); expandableListView. setPadding (90, 15, 5, 15); return expandableListView;} @ Override public boolean isChildSelectable (int I, int i1) {return true ;}}  

InerAdapter:

Public class InnerAdapter extends BaseExpandableListAdapter {private List  Provinces;. // province set private Activity; // create layout use // construct the parameter public InnerAdapter (List  Provinces, Activity activity) {if (provinces! = Null) {this. provinces = provinces;} else provinces = new ArrayList <> (); this. activity = activity;} // obtain the number of outer Groups @ Override public int getGroupCount () {return provinces. size ();} // obtain the number of inner Groups @ Override public int getChildrenCount (int I) {return provinces. get (I ). getCities (). size () ;}// obtain the outer group @ Override public Object getGroup (int I) {return provinces. get (I);} // obtain the inner layer data @ Override public Object getChild (int I, int i1) {return provinces. get (I ). getCities (). get (i1);} // group subscript @ Override public long getGroupId (int I) {return I;} // subview subscript @ Override public long getChildId (int I, int i1) {return i1;} // I don't know what to do. @ Override public boolean hasStableIds () {return false ;} // obtain the group layout @ Override public View getGroupView (int I, boolean B, View view, ViewGroup viewGroup) {TextView textView = new TextView (activity); AbsListView. layoutParams params = new AbsListView. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. LayoutParams. WRAP_CONTENT); TextView. setLayoutParams (params); textView. setPadding (,); textView. setGravity (Gravity. CENTER_VERTICAL| Gravity. LEFT); TextView. setText (provinces. get (I ). getName (); return textView;} // get the child View layout @ Override public View getChildView (int I, int i1, boolean B, View view, ViewGroup viewGroup) {TextView textView = new TextView (activity); AbsListView. layoutParams params = new AbsListView. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. LayoutParams. WRAP_CONTENT); TextView. setLayoutParams (params); textView. setPadding (,); textView. setGravity (Gravity. CENTER_VERTICAL| Gravity. LEFT); TextView. setText (provinces. get (I ). getCities (). get (i1 ). getName (); return textView;} // whether the Sub-View is optional @ Override public boolean isChildSelectable (int I, int i1) {return true ;}}  

We can see that the code of the OuterAdapter and InnerAdapter are almost identical. One of the differences is that the returned sub-View is different, in the OuterAdapter neutron View, the returned result is a custom InnerExpandableListView inherited from expandableListView (the reason why we need to customize expandableListView is that the ListView display in the ListView nested ListView is incomplete. For full display, overwrite expandableListView). This is what we call expandableListView nested expandableListView. An expandableListView is returned through the adapter class. Another difference is that when the OuterAdapter returns the number of sub-views, we return 1. Remember, this is very important because all of his specific display is handed overThe sub-ExpandableListView is used to setThe sub-ExpandableListView is displayed.

The following describes several key methods.

GetGroupCount (): gets the number of first-level entries.

GetChildrenCount (): gets the number of second-level entries.

GetGroupView (): gets the corresponding layout of a level-1 entry.

GetChildView (): gets the layout corresponding to the second-level entry.

Let's take a look at the writing method of InnerExpandableListView.

Public class InnerExpandableListView extends ExpandableListView {public InnerExpandableListView (Context context) {super (context);} public partition (Context context, AttributeSet attrs) {super (context, attrs );} public InnerExpandableListView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} // recalculates the height of expandableListView using this method, make the expandableListView inside show all @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {int expandSpec = MeasureSpec. makeMeasureSpec (Integer.MAX_VALUE> 2, MeasureSpec.AT_MOST); Super. onMeasure (widthMeasureSpec, expandSpec );}}

SetAdapter () set the adapter

Public class MainActivity extends AppCompatActivity {private ExpandableListView expandableListView; List  CountyEntities = new ArrayList <> (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); expandableListView = findViewById (R. id. expandable_listView); initData (); // create an adapter instance and set the adapter OutAdapter adapter = new OutAdapter (countyEntities, this); expandableListView. setAdapter (adapter) ;}// create private void initData () {for (int m = 0; m <4; m ++) {CountyEntity countyEntity = new CountyEntity (); countyEntity. setName ("country" + m); List  Provinces = new ArrayList <> (); for (int j = 0; j <6; j ++) {CountyEntity. province province = new CountyEntity. province (); province. setName ("Province" + j); List  Cities = new ArrayList <> (); for (int I = 0; I <5; I ++) {CountyEntity. city city = new CountyEntity. city (); city. setName ("city" + I); cities. add (city);} province. setCities (cities); provinces. add (province);} countyEntity. setProvinces (provinces); countyEntities. add (countyEntity );}}}   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.