[Android Basics] Use ExpandableListView to implement a custom drop-down list. The android custom drop-down box

Source: Internet
Author: User
Tags addgroup

[Android Basics] Use ExpandableListView to implement a custom drop-down list. The android custom drop-down box
1. Introduction to ExpandableListView

The drop-down list (Extensible list control) is very common in App applications. It is a must-have control in Android development. The following describes the development of the ExpandableListView control.

ExpandableListView is divided into group list items and sub-list items. Click group list items to display all sub-list items in the group. Like ListView, it connects data and display through the Adapter data Adapter, but it uses another interface: ExpandableListAdapter.

What we need to do today is implement a class that inherits its parent interface: BaseExpandableListAdapter to implement a custom drop-down list (just like in ListView, set the interface layout file layout for its list items ).

2. Example

The example contains four files, two Java files and two layout files. The Java file contains an Activity and the MyExpandAdapter implemented by ourselves. The layout file is used for the Activity, and the other is the interface layout file for sub-list items. As for the layout of group list items, we create a TextView directly in the Code. In this way, we also introduce two methods to implement the drop-down list style.

Plan_manager.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ExpandableListView        android:id="@+id/expandableListView_plan_manager"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </ExpandableListView></LinearLayout>

Layout file, add controls

PlanManager. java
Package com. plan; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. widget. expandableListView; import android. widget. toast; import com. example. plan. r; public class PlanManager extends Activity {ExpandableListView sp_date_list = null; // List MyExpandAdapter adapter = null; // data adapter List <String> group_head; // group List item, each group has a sublist <List <Stri Ng> child; // sublist item @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. plan_manger); // obtain the control sp_date_list = (ExpandableListView) findViewById (R. id. expandableListView_plan_manager); // initialization group and sublist item group_head = new ArrayList <String> (); child = new ArrayList <List <String> (); // set the adapter sp_date_list.setAdapter (new MyExpandAdapter (this, grou P_head, child); // set the data adapter sp_date_list.setCacheColorHint (0); // when dragging the list, the black background addGroup ("") is not displayed; addGroup ("Chunxiao "); addChild (0, "Moonlight in front of bed"); addChild (0, "suspected to be frost on the ground"); addChild (1, "don't know spring sleep"); addChild (1, "");} // Add group list item public void addGroup (String group) {group_head.add (group); child. add (new ArrayList <String> (); // add a new array to child} // add the corresponding group's self-list item public void addChild (int position, String child) {List <String> it = thi S. child. get (position); if (it! = Null) {it. add (child) ;}else {it = new ArrayList <String> (); it. add (child) ;}/// the response method public void childSelect (int groupPosition, int childPosition) selected for the sublist item {Toast. makeText (getBaseContext (), Integer. toString (groupPosition) + ":" + Integer. toString (maid), 2000 ). show ();}}

Query controls, connect display and adapters, and add some sample data. addGroup, addChild, and childSelect can be provided to other components as interfaces.

Plain_text.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <TextView        android:id="@+id/textView_plain_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView" /></LinearLayout>

This is the layout file of the sub-list items. It is just a simple example. If it is too complicated to learn, you only use one text box. You can add several more controls to the actual project, change the type of child (preferably change to map ).

MyExpandAdapter. java
Package com. plan; import java. util. arrayList; import java. util. list; import android. content. context; import android. util. log; import android. view. gravity; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. absListView; import android. widget. baseExpandableListAdapter; import android. widget. textView; import com. example. plan. r; public class MyExpandAdapter extends BaseExpandableListAdapter {List <String> group_head; List <String> child; PlanManager context; public MyExpandAdapter (Context content) {// initialization group and sublist item group_head = new ArrayList <String> (); child = new ArrayList <List <String> ();} public MyExpandAdapter (PlanManager context, list <String> group_head, List <String> child) {this. context = context; // initialization group and sublist item this. group_head = group_head; this. child = child;} @ Override public int getGroupCount () {// TODO Auto-generated method stub return this. group_head.size () ;}@ Override public int getChildrenCount (int position) {// TODO Auto-generated method stub if (position <0 | position> = this. child. size () return 0; return child. get (position ). size () ;}@ Override public Object getGroup (int groupPosition) {// TODO Auto-generated method stub return group_head.get (groupPosition) ;}@ Override public Object getChild (int groupPosition, int childPosition) {// TODO Auto-generated method stub return child. get (childPosition ). get (childPosition) ;}@ Override public long getGroupId (int groupPosition) {// TODO Auto-generated method stub return groupPosition ;}@ Override public long getChildId (int groupPosition, int childPosition) {// TODO Auto-generated method stub return childPosition;} @ Override public boolean hasStableIds () {// TODO Auto-generated method stub return true ;} @ Override public View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {// obtain the text String text = group_head.get (groupPosition); if (convertView = null) {// there is only one text box in the group list interface, which directly generates convertView = new TextView (context); // set the interface. AbsListView: The base class AbsListView used to implement the virtual list of items. layoutParams lp = new AbsListView. layoutParams (ViewGroup. layoutParams. FILL_PARENT, 60); (TextView) convertView ). setGravity (Gravity. CENTER_VERTICAL | Gravity. LEFT); // place the text box in the central convertView. setPadding (45, 0, 0, 0); // you can specify a more convertView for the drop-down graph in the text. setLayoutParams (lp); Log. d ("Group", text) ;}( (TextView) convertView ). setText (text); return convertView;} @ Override public View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {// sublist control if (convertView = null) is designed through the interface file {// convert will be reused during running. if it is not empty, it indicates that LayoutInflater layoutInflater does not need to be retrieved again; // use this to load the interface layoutInflater = LayoutInflater. from (context); convertView = layoutInflater. inflate (R. layout. plain_text, null);} TextView TV = (TextView) convertView. findViewById (R. id. textView_plain_text); String text = child. get (groupPosition ). get (childPosition); TV. setText (text); // obtain the text control and set the return convertView;} @ Override public boolean isChildSelectable (int groupPosition, int childPosition) {// call the ChildSelect method context in the Activity. childSelect (groupPosition, childPosition); return true ;}}

After the interfaces are inherited, Eclipse can automatically generate these methods. You just need to change them. Log is used in some places. d method, used to output logs in logcat (also available System. out. println) is mainly used to optimize the program. By checking the log several times, you can see whether the methods in the program have been repeatedly called.

Related Article

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.