Android ListView two level catalog Check effect

Source: Internet
Author: User

A. ListView's Level Two directory and check the implementation:

recorded so that it may be used in the future, directly affixed to the source code :

First on:


Main interface:

public class Mainactivity extends Activity {//Tree ListView Display category private Expandablelistview listview1;//level private list< String> groups;//Level Two private list<list<string>> child;//adapter private Listviewadapter madapter;// Record the clicked node position, initially -1public static int children_item = -1;public static int parent_item = -1;//record the last clicked Itemprivate View view; @Ov errideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); listview1 = (Expandablelistview) Findviewbyid (r.id.listview1); madapter = new Listviewadapter    (This, listviewadapter.paddingleft >> 1); groups = Getlistdata (); First-level directory child = new arraylist<list<string>> ();        list<listviewadapter.treenode> TreeNode = Madapter.gettreenode (); Add content for each level of directory for (int i = 0; i < groups.size (); i++) {listviewadapter.treenode node = new Listviewadapter.treenode (); Node.parent = Groups.get (i), Child.add (I,getlistdata ()), for (int II = 0; II < child.get (i). sIze (); ii++) {Node.childs.add (Child.get (i). Get (ii));} Treenode.add (node);} Madapter.updatetreenode (TreeNode); Listview1.setadapter (Madapter); Listview1.setonchildclicklistener (new Onchildclicklistener () {@Overridepublic Boolean onchildclick (Expandablelistview arg0,view arg1, int parent, int Children, long Arg4) {//Current item discoloration Arg1.setbackgroundresource (R.COLOR.COLOR1);//The previous item restores if (Parent_item! =- 1&& Children_item! = -1&& (Children_item! = Children | | Parent_item! = parent)) {View.setbackgroundresour CE (R.DRAWABLE.LISTVIEW_ITEM_BG);}    View = Arg1;children_item = Children;parent_item = Parent;return false;}}); Public list<string> getlistdata () {list<string> List = new arraylist<string> (); List.add ("language"); List.add ("mathematics"), List.add ("English"), List.add ("History"), List.add ("Mao"); List.add ("nature"); return list;}}
Adapter:

public class Listviewadapter extends Baseexpandablelistadapter {public static final int itemheight = 72;//height of each item STA    Tic Final int paddingleft = 48;//height of each item private int mypaddingleft = 0; Static public class TreeNode {public String parent;public list<string> childs = new arraylist<string> ();} list<treenode> treenodes = new arraylist<treenode> (); Context Parentcontext;public Listviewadapter (context view, int mypaddingleft) {Parentcontext = View;this.mypaddingleft = Mypaddingleft;} Public list<treenode> Gettreenode () {return treenodes;} public void Updatetreenode (list<treenode> nodes) {treenodes = nodes;} public void RemoveAll () {treenodes.clear ();} Public Object getchild (int groupposition, int childposition) {return treenodes.get (groupposition). Childs.get ( childposition). toString ();} public int Getchildrencount (int groupposition) {return treenodes.get (groupposition). Childs.size ();} static public TextView Gettextview (context context) {ABSLISTVIEW.LAYOUTPArams LP = new Abslistview.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, itemheight); TextView TextView = new TextView (context); Textview.setlayoutparams (LP); Textview.setgravity (gravity.center_vertical | Gravity.left); Textview.settextsize (); return textView;} Public View getchildview (int groupposition, int childposition,boolean islastchild, View Convertview, ViewGroup parent) {T Extview TextView = Gettextview (This.parentcontext); Textview.settext (Getchild (Groupposition, childposition). ToString ()); Textview.setpadding (Mypaddingleft + paddingleft, 0, 0, 0); Textview.settextsize (); return textView;} Public View getgroupview (int groupposition, Boolean Isexpanded,view Convertview, ViewGroup parent) {//will not be checked for missing if ( Mainactivity.children_item! =-1) {if (Mainactivity.children_item = = groupposition) { Convertview.setbackgroundresource (R.color.color1);} else {convertview.setbackgroundresource (R.DRAWABLE.LISTVIEW_ITEM_BG);}} TextView TextView = Gettextview (This.parentcontext); Textview.settext (Getgroup (groupposition). toString ()); Textview.setpadding (Mypaddingleft + (paddingleft >> 1), 0, 0, 0); return textView;} public long Getchildid (int groupposition, int childposition) {return childposition;} Public Object getgroup (int groupposition) {return treenodes.get (groupposition). parent.tostring (); public int GetGroupCount () {return treenodes.size ();} public long getgroupid (int groupposition) {return groupposition;} public boolean ischildselectable (int groupposition, int childposition) {return true;} public Boolean hasstableids () {return true;}}

Xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent    " Android:background= "@android: Color/background_light"    android:orientation= "vertical" >   < Expandablelistview            android:id= "@+id/listview1"            android:layout_width= "fill_parent"            android:layout_ height= "Fill_parent"            android:choicemode= "Singlechoice"/></linearlayout>

Drawable/listview_item_bg.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:drawable=" @android: Color/background_dark "android:state_selected=" true "> </item>    <item android:drawable= "@android: Color/background_dark" android:state_pressed= "true" > </item>   <item android:drawable= "@android: Color/background_dark" android:state_focused= "true" > </item>    <item android:drawable= "@android: Color/transparent" ></item></selector>

Color.xml:

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <color name= "Color1" > @android: color/ Background_dark</color></resources>


Android ListView two level catalog Check effect

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.