Android development practices: encapsulation and Application of Multi-level lists

Source: Internet
Author: User

In Android, the multilevel list can be implemented using ExpandableListView and SimpleExpandableListAdapter. However, SimpleExpandableListAdapter is difficult to understand and has poor scalability. Therefore, custom sub-classes of the BaseExpandableListAdapter class and encapsulation-related operations are more intuitive and convenient to use. I will paste the encapsulation I designed for reference by new users.


First ,:


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/06201M312-0.png "title =" .png "/>


1. First, design the title class of the Multi-Level list.


Just as files and folders can be abstracted using the File class in a unified manner, the level-1 title and level-2 title of a multi-level list can also be abstracted using the same base class. Therefore, I have designed a base class and two subclasses, GroupList, GroupListChild, and GroupListParent. Their implementation is as follows:


1) abstract base class of the title of the GroupList Multi-Level list


public abstract class GroupList {    private final String mTitle;                                                                                                                                                                                                                                                                               public GroupList(String title) {        mTitle = title;    }                                                                                                                                                                                                                                                                               public String getTitle() {        return mTitle;    }                                                                                                                                                                                                                                                                               public abstract List<GroupList> getChild();                                                                                                                                                                                                                                                                               public abstract int getResource();         public abstract void buildView(View v);}


2) GroupListChild Multi-Level list second-level title subclass


public class GroupListChild extends GroupList {                                                                                                                                                                                                                                     public GroupListChild(String title) {        super(title);          }                                                                                                                                                                                                                                  @Override    public int getResource() {        return R.layout.grouplist_child;    }                                                                                                                                                                                                                                  @Override    public List<GroupList> getChild() {             return null;    }                                                                                                                                                                                                                                  @Override    public void buildView(View v) {                                                                                                                                                                                                                                             TextView textView  = (TextView)v.findViewById(R.id.GroupListChild);                           textView.setText(getTitle());    } }


3) GroupListParent level-1 title subclass of Multi-Level list


public class GroupListParent extends GroupList {                                                                                                                                                                                                                      private List<GroupList> mPopListChilds;                                                                                                                                                                                                                          public GroupListParent(String title,List<GroupList> childs) {        super(title);              mPopListChilds = childs;    }                                                                                                                                                                                                                      @Override    public int getResource() {        return R.layout.grouplist_parent;    }                                                                                                                                                                                                                      @Override    public List<GroupList> getChild() {        return mPopListChilds;    }                                                                                                                                                                                                                          @Override    public void buildView(View v) {                                                                                                                                                                                                                                         TextView textView = (TextView)v.findViewById(R.id.GroupListParent);                      textView.setText(getTitle());    }}


2. Design a subclass of BaseExpandableListAdapter


The subclass I designed is a common Adapter subclass. The implementation of the class does not include the specific Layout implementation. All Layout is handled by getResource and buildView of GroupList. Therefore, you can flexibly modify the specific implementation of Layout without modifying the Adapter code.


public class GroupListAdapter extends BaseExpandableListAdapter {                                                                                                                                                                                 private Context mContext;    private List<GroupList> mGroups;                                                                                                                                                                                     public GroupListAdapter(Context context, List<GroupList> groups) {        this.mContext = context;        this.mGroups  = groups;    }                                                                                                                                                                                     @Override    public Object getChild(int groupPosition, int childPosition) {                                                                                                                                                                                            List<GroupList> chList = mGroups.get(groupPosition).getChild();        if( chList == null) {            return null;        }                                                                                                                                                                                             return chList.get(childPosition);    }                                                                                                                                                                                 @Override    public long getChildId(int groupPosition, int childPosition) {                                                                                                                                                                                      return childPosition;    }                                                                                                                                                                                   @Override    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,ViewGroup parent) {                                                                                                                                                                                                                            GroupList child = (GroupList)getChild(groupPosition, childPosition);        if( child == null) {            return null;        }                                                                                                                                                                                                 if (view == null) {               LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                    view = (RelativeLayout)inflater.inflate(child.getResource(), null);        }                                                                                                                                                                                                  child.buildView(view);                                                                                                                                                                                            return view;    }                                                                                                                                                                                   @Override    public int getChildrenCount(int groupPosition) {                                                                                                                                                                                            List<GroupList> chList = mGroups.get(groupPosition).getChild();        if( chList == null) {            return 0;        }                                                                                                                                                                                            return chList.size();    }                                                                                                                                                                                   @Override    public Object getGroup(int groupPosition) {        return mGroups.get(groupPosition);    }                                                                                                                                                                                   @Override    public int getGroupCount() {        return mGroups.size();    }                                                                                                                                                                                   @Override    public long getGroupId(int groupPosition) {        return groupPosition;    }                                                                                                                                                                                   @Override    public View getGroupView(int groupPosition, boolean isLastChild, View view,ViewGroup parent) {                                                                                                                                                                                      GroupList group = (GroupList)getGroup(groupPosition);                                                                                                                                                                                              if (view == null) {                                                                                                                                                                                           LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                    view = (RelativeLayout)inflater.inflate(group.getResource(), null);          }                                                                                                                                                                                       group.buildView(view);                                                                                                                                                                                            return view;    }                                                                                                                                                                                   @Override    public boolean hasStableIds() {        return true;    }                                                                                                                                                                                   @Override    public boolean isChildSelectable(int arg0, int arg1) {        return true;    }}                                                                                                                                                                              


3. Application code


To simplify the process, I used the encapsulated classes in MainActivity to demonstrate the multi-level list function. The example is as follows:


public class MainActivity extends Activity implements OnGroupClickListener,OnChildClickListener {                                                                                                                                                 private ExpandableListView mlistView;     private GroupListAdapter mAdapter;                                                                                                                                                     private static final String[] mParentMenu = {        "Book", "Video", "Audio"    };                                                                                                                                                     private static final String[][] mChildMenu = {        { "book1", "book2", "book3", "book4" },        { "video1", "video2" },        { "audio1", "audio2", "audio3","audio4"}    };                                                                                                                                                     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                                                                                                                                                                   mlistView = new ExpandableListView(this);                              mlistView.setOnGroupClickListener(this);              mlistView.setOnChildClickListener(this);                                                                                                                                                              List<GroupList> groups = new ArrayList<GroupList>();        for( int i=0; i<mParentMenu.length; i++) {                     List<GroupList> childs = new ArrayList<GroupList>();            for( int j=0; j<mChildMenu[i].length; j++ ) {                childs.add(new GroupListChild(mChildMenu[i][j]));            }            groups.add(new GroupListParent(mParentMenu[i],childs));        }                                                                                                                                                     mAdapter = new GroupListAdapter(this,groups);        mlistView.setAdapter(mAdapter);                                                                                                                                                             setContentView(mlistView);        }                                                                                                                                                 @Override    public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {                                                                                                                                                             return false;    }                                                                                                                                                 @Override    public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {                                                                                                                                                           return false;    }}


4. Related xml files


1) grouplist_child.xml File


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">                                                                                                                                         <TextView        android:id="@+id/GroupListChild"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#FFFF0000"        android:layout_margin="10dp"        android:layout_centerInParent="true"/>                                                                                                                                     </RelativeLayout>


2) grouplist_parent.xml File


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">                                                                                                              <TextView        android:id="@+id/GroupListParent"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="@android:color/black"        android:textStyle="bold"        android:layout_margin="10dp"        android:layout_centerInParent="true"/>                                                                                                          </RelativeLayout>


5. Summary


This article provides examples of Android Multi-Level list encapsulation and application. I personally feel that the encapsulation design is highly scalable, especially with two features: 1. the differences between Level 1 titles and level 2 titles are shielded, and the interface is unified; 2. place layout in the Item class of the Adapter for implementation, so that the Adapter and layout are not coupled. These two design ideas are often used in future software design. The entire project code in the attachment, the code structure is still clear, there is no need to explain too much, there are doubts welcome to leave a message or send a letter to the lujun.hust@gmail.com exchange.


This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1289642

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.