Android Learning Note 27 Expandablelistview collapsible list and StackView stack view

Source: Internet
Author: User

Android Learning Note 27 Expandablelistview collapsible list and StackView stack view Expandablelistview collapsible list

In this section we introduce the third control with the adapter, Expandablelistview the collapsible list. This control can implement our very common friends in QQ group function, Expandablelistview is the sub-class of the ListView, using the same as the ListView, let's learn the basic use of this control:

Common Properties:

    • Android:childdivider: Specifies the separator bar between the sub-categories of the subgroups, the picture is not fully displayed, and the list of ions is a straight line
    • Android:childindicator: Displays the Drawable object next to the child list, which can be an image
    • Android:childindicatorend: The end constraint position of the child list item indicator
    • Android:childindicatorleft: Left constraint position for child list item indicator
    • Android:childindicatorright: The right constraint position of the child list item indicator
    • Android:childindicatorstart: The start constraint position of the child list item indicator
    • Android:groupindicator: Displays the Drawable object next to the group list, which can be an image
    • Android:indicatorend: End constraint position for Group list item indicator
    • Android:indicatorleft: Left constraint position for Group list item indicator
    • Android:indicatorright: Right constraint position for group list item indicator
    • Android:indicatorstart: Start constraint position for Group list item indicator

Three ways to implement Expandableadapter:

    1. The custom class inherits Baseexpandablelistadpter implementation Expandableadapter.
    2. Use Simpleexpandablelistadpater to wrap two list sets into a expandableadapter
    3. Use Simplecursortreeadapter to wrap data in the cursor into Simplecurotreeadapter

Here, we are using the first way to customize a class to implement Baseexpandablelistadapter, see the concrete implementation below:

Layout code:

Item_group

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextView    android:id="@+id/tv_group_name"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginBottom="10dp"    android:layout_marginLeft="28dp"    android:layout_marginTop="10dp"    android:padding="8dp"    android:text="张三"    android:textColor="#000000"    android:textSize="16sp" /></LinearLayout>

Item_child

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><ImageView    android:id="@+id/iv_child_icon"    android:layout_width="40dp"    android:layout_height="40dp"    android:layout_marginBottom="8dp"    android:layout_marginLeft="8dp"    android:layout_marginTop="8dp"    android:padding="2dp"    android:src="@mipmap/ic_launcher" /><TextView    android:id="@+id/tv_child_name"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginBottom="8dp"    android:layout_marginLeft="5dp"    android:layout_marginTop="8dp"    android:text="子节点"    android:textColor="#330000"    android:textSize="12sp" /></LinearLayout>

Activity_main layout

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.expandablelistviewdemo.MainActivity"><ExpandableListView    android:id="@+id/ex_list"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:childDivider="#E02D2F" /></RelativeLayout>

Custom Adapter Code:

Package Com.example.expandablelistviewdemo;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.list;/** * Created by Devin on 2016/7/12. */public class Customadapter extends Baseexpandablelistadapter {private list<group> grouplist;private List< List<child>> childlist;private Context Mcontext;public customadapter (context Mcontext, list<group>    Grouplist, list<list<child>> childlist) {this.mcontext = Mcontext;    This.grouplist = grouplist; This.childlist = childlist;} /** * Gets the number of groups. * * @return */@Overridepublic int getgroupcount () {return grouplist.size ();} /** * Gets The number of children in a specified group. * * @param i * @return */@Overridepublic int getchildrencount (int i) {return childlist.get (i). Size (); /** * Gets the DATA associated with the given group * * @param i * @return */@Overridepublic Object getgroup (int i) {return grouplist. Get (i);} /** * Gets The data associated with the given child within the given group. * * @param i * @param i1 * @return */@Overridepublic Object getchild (int i, int i1) {return childlist.get (i). get (I1);} /** * Gets The ID for the group at the given position. * * @param i * @return */@Overridepublic long getgroupid (int i) {return i;} /** * Gets The ID for the given child within the given group. * * @param i * @param i1 * @return */@Overridepublic long getchildid (int i, int i1) {return i1;} /** * Indicates whether the child and group IDs is stable across changes to the underlying data. * * @return * * @Overridepublic boolean hasstableids () {return false;} /** * Gets A view that displays the given group * * @param i * @param b * @param View * @param viewgroup * @return * * @Over    Ridepublic View Getgroupview (int i, Boolean B, view view, ViewGroup ViewGroup) {Groupviewholder groupviewholder = null;        if (view = = null) {view = Layoutinflater.from (Mcontext). Inflate (R.layout.item_group, ViewGroup, false);        Groupviewholder = new Groupviewholder ();        Groupviewholder.tv_group_name = (TextView) View.findviewbyid (r.id.tv_group_name);    View.settag (Groupviewholder);    } else {Groupviewholder = (Groupviewholder) view.gettag ();    } groupViewHolder.tv_group_name.setText (Grouplist.get (i). Getgroupname ()); return view;} /** * Gets A View that displays the data for the given child within the given group. * * @param i * @param i1 * @param b * @param view * @param viewgroup * @return */@Overridepublic view Getchildview (int i,    int I1, Boolean B, view view, ViewGroup viewgroup) {Childviewholder childviewholder = null;        if (view = = null) {view = Layoutinflater.from (Mcontext). Inflate (R.layout.item_child, ViewGroup, false);        Childviewholder = new Childviewholder (); Childviewholder.iv_child_icon =(ImageView) View.findviewbyid (R.id.iv_child_icon);        Childviewholder.tv_child_name = (TextView) View.findviewbyid (r.id.tv_child_name);    View.settag (Childviewholder);    } else {Childviewholder = (Childviewholder) view.gettag ();    } childViewHolder.iv_child_icon.setImageResource (R.mipmap.ic_launcher);    ChildViewHolder.tv_child_name.setText (Childlist.get (i). Get (I1). Getchildname ()); return view;} /** * This method needs to return true, otherwise the child node's Click event cannot be implemented * Whether the children at the specified position is selectable. * * @param i * @param i1 * @return */@Overridepublic boolean ischildselectable (int i, int i1) {return true;} private static class Groupviewholder {TextView tv_group_name;}    private static class Childviewholder {ImageView Iv_child_icon; TextView tv_child_name;}}

Two common JavaBean codes:

package com.example.expandablelistviewdemo;/** * Created by Devin on 2016/7/12. */public class Group {private String groupName;public Group() {}public Group(String groupName) {    this.groupName = groupName;}public String getGroupName() {    return groupName;}public void setGroupName(String groupName) {    this.groupName = groupName;}}package com.example.expandablelistviewdemo;/** * Created by Devin on 2016/7/12. */public class Child {private String childName;public Child() {}public Child(String childName) {    this.childName = childName;}public String getChildName() {    return childName;}public void setChildName(String childName) {    this.childName = childName;}}

Finally, the activity code:

Package Com.example.expandablelistviewdemo;import Android.support.v7.app.appcompatactivity;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 Appcompatactivity {private List< group> grouplist;private list<list<child>> childlist;private expandablelistview ex_list;private Customadapter customadapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (    Savedinstancestate);    Setcontentview (R.layout.activity_main);    Ex_list = (Expandablelistview) Findviewbyid (r.id.ex_list);    InitData ();    Customadapter = new Customadapter (this, grouplist, childlist);    Ex_list.setadapter (Customadapter); Click event Ex_list.setonchildclicklistener for child nodes (new Expandablelistview.onchildclicklistener () {@Override Publ IC Boolean Onchildclick (Expandablelistview expandablelistview, view view, int i, inT I1, long L) {toast.maketext (Mainactivity.this, "you clicked:" + childlist.get (i). Get (I1). Getchildname (), Toast.len            Gth_short). Show ();        return true; }    });}    private void InitData () {grouplist = new arraylist<> ();    Childlist = new arraylist<> ();    Grouplist.add (New Group ("laden Car"));    Grouplist.add ("Off-road car");    Grouplist.add (New Group ("Dump Car"));    Grouplist.add (New Group ("Special Purpose Vehicle"));    Grouplist.add (New Group ("Passenger Car"));    Grouplist.add (New Group ("Tractor"));    Grouplist.add (New Group ("sedan"));    Grouplist.add (New Group ("Semi-trailer"));    list<child> child1 = new arraylist<> ();    Child1.add (New Child ("minivan"));    Child1.add (New Child ("Light Truck"));    Child1.add (New Child ("Medium Goods Vehicle"));    Child1.add (New Child ("heavy Goods Vehicle"));    Childlist.add (child1);    list<child> child2 = new arraylist<> ();    Child2.add (New Child ("Light SUV"));    Child2.add (New Child ("mid-size SUV"));    Child2.add (New Child ("heavy-duty Buggy"));    Child2.add (New Child ("super heavy-duty buggy")); ChildliSt.add (CHILD2);    list<child> child3 = new arraylist<> ();    Child3.add (New Child ("Light Dump Truck"));    Child3.add (New child ("Medium Dump Truck"));    Child3.add (New child ("Heavy Dump Truck"));    Child3.add (New Child ("Mine Dump Truck"));    Childlist.add (CHILD3);    list<child> child4 = new arraylist<> ();    Child4.add (New Child ("box-car"));    Child4.add (New Child ("Can-car"));    Child4.add (New child ("lifting hoist"));    Child4.add (New Child ("silo-type Car"));    Child4.add (New Child ("Special Structure Car"));    Child4.add (New Child ("Special Dump Truck"));    Childlist.add (CHILD4);    list<child> child5 = new arraylist<> ();    Child5.add (New Child ("minibus"));    Child5.add (New Child ("Light Bus"));    Child5.add (New Child ("Midsize Bus"));    Child5.add (New child ("Big Bus"));    Child5.add (New child ("Extra large Bus"));    Childlist.add (CHILD5);    list<child> child6 = new arraylist<> ();    Child6.add (New Child ("semi-trailer Tractor"));    Child6.add (New Child ("All-trailer Tractor"));    Childlist.add (CHILD6);    list<child> Child7 = new arraylist<> (); Child7.add (New Child ("mini sedan"));    Child7.add (New Child ("ordinary sedan"));    Child7.add (New Child ("intermediate sedan"));    Child7.add (New Child ("Mid-level sedan"));    Child7.add (New Child ("premium sedan"));    Childlist.add (CHILD7);    list<child> Child8 = new arraylist<> ();    Child8.add (New Child ("Light semi-trailer"));    Child8.add (New Child ("Medium semi-trailer"));    Child8.add (New Child ("Heavy Semitrailer"));    Child8.add (New child ("overweight semi-trailer")); Childlist.add (CHILD8);}}

Final implementation:

Expandablelistview control is simply introduced here, here is just the basic implementation, there is the need to expand.

As usual, enclose the domestic mirror API

Here we learn another control StackView stack view

StackView Stack View

StackView is a control that displays a set of view to the user one by one, which is a collection of view, which is pressed one and the view can be switched freely. Below we have an example to understand how stackview is used

Layout file Code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><StackView    android:id="@+id/sk"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:loopViews="true" /><LinearLayout    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical">    <Button        android:id="@+id/btn_previous"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="前一张" />    <Button        android:id="@+id/btn_next"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="后一张" /></LinearLayout></LinearLayout>

Activity code:

Package Com.example.expandablelistviewdemo;import Android.content.context;import Android.graphics.color;import Android.os.bundle;import Android.support.annotation.nullable;import android.support.v7.app.AppCompatActivity; Import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.button;import android.widget.linearlayout;import android.widget.stackview;/** * Created by Devin on 2016/7/12. */public class Stackviewactivity extends Appcompatactivity {private StackView sk;private Button btn_previous;private Button btn_next;private int[] mcolors = {color.blue, Color.cyan, Color.gray, Color.green, color.red}; @Overrideprotected    void OnCreate (@Nullable Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_stackview);    SK = (StackView) Findviewbyid (r.id.sk);    Btn_next = (Button) Findviewbyid (R.id.btn_next);    Btn_previous = (Button) Findviewbyid (r.id.btn_previous); Coloradapter adapter =New Coloradapter (Getapplicationcontext (), mcolors);    Sk.setadapter (adapter);            Btn_previous.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {        Sk.showprevious ();    }    });            Btn_next.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {        Sk.shownext (); }    });}    /** * Custom Adapter */private class Coloradapter extends Baseadapter {private Context mcontext;    Private int[] mcolors;        Public Coloradapter (context context, int[] colors) {Mcontext = context;    Mcolors = colors;    } public int GetCount () {return mcolors = = null? 0:mcolors.length;    } public Object GetItem (int position) {return mcolors = = null? Null:mcolors[position];    } public long Getitemid (int position) {return position; Public View GetView (int position, view view, ViewGroup parent) {Linearlayout.layoutparams ColoRlayoutparams = new Linearlayout.layoutparams (400, 400);        LinearLayout colorlayout = new LinearLayout (mcontext);        Colorlayout.setbackgroundcolor (Mcolors[position]);        Colorlayout.setlayoutparams (Colorlayoutparams);    return colorlayout; }}}

Finally realized

You can toggle the gesture by dragging it, or you can click the button to toggle it. StackView Simple Introduction to Here, the students have the need to expand, can achieve a better effect. StackView is after androidAPI3.0.

Attach a domestic mirroring API

In the next section we describe the dialog control

Android Learning Note 27 Expandablelistview collapsible list and StackView stack view

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.