Android-用ListView模仿ExpandableListView

来源:互联网 发布:广州网络远程教育 编辑:程序博客网 时间:2024/06/10 08:17

转载请注明出处:http://blog.csdn.net/goldenfish1919/article/details/38334995

既然有现成的ExpandableListView,为啥还非得仿一个啊?是不是脱裤子放屁多此一举呢?当然不是的。ExpandableListView只能支持两层啊,假如你想支持三层或者更多层呢?或者说页面结构比较复杂,就像这样的:

http://img.my.csdn.net/uploads/201408/01/1406870699_5231.png

  1. public class MainActivity extends Activity {  
  2.       
  3.     private ListViewAdapter listViewAdapter;  
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         //数据  
  10.         List<GroupBean> itemList = getListData();  
  11.         //view和adapter  
  12.         ListView listView = (ListView)findViewById(R.id.listview);  
  13.         listViewAdapter = new ListViewAdapter(this, itemList);  
  14.         //给adapter设置监听  
  15.         listViewAdapter.setGroupListener(new OnGroupClickListener(){  
  16.             @Override  
  17.             public void onGroupClicked(GroupView view, GroupBean group,int position) {  
  18.                 Toast.makeText(MainActivity.this"I am "+ group.getName(), Toast.LENGTH_SHORT).show();  
  19.             }  
  20.         });  
  21.         listViewAdapter.setChildListener(new OnChildClickListener(){  
  22.             @Override  
  23.             public void onChildClicked(ChildView view, ChildBean child,int position) {  
  24.                 Toast.makeText(MainActivity.this"I am "+ child.getName(), Toast.LENGTH_SHORT).show();  
  25.             }  
  26.         });  
  27.         listView.setAdapter(listViewAdapter);  
  28.     }  
  29.       
  30.     @Override  
  31.     protected void onDestroy() {  
  32.         if(listViewAdapter != null){  
  33.             listViewAdapter.destroy();  
  34.             listViewAdapter = null;  
  35.         }  
  36.         super.onDestroy();  
  37.     }  
  38.   
  39.     private static interface OnGroupClickListener{  
  40.         public void onGroupClicked(GroupView view, GroupBean group, int position);  
  41.     }  
  42.       
  43.     private static interface OnChildClickListener{  
  44.         public void onChildClicked(ChildView view, ChildBean child, int position);  
  45.     }  
  46.       
  47.     private static class ListViewAdapter extends BaseAdapter{  
  48.           
  49.         private static final int VIEW_TYPE_GROUP = 0;  
  50.         private static final int VIEW_TYPE_CHILD = 1;  
  51.           
  52.         private Context context;  
  53.         private List<GroupBean> groupList;  
  54.         private OnGroupClickListener groupListener;  
  55.         private OnChildClickListener childListener;  
  56.           
  57.         public ListViewAdapter(Context context, List<GroupBean> groupList){  
  58.             this.context = context;  
  59.             this.groupList = groupList;  
  60.         }  
  61.         @Override  
  62.         public int getCount() {  
  63.             if(groupList == null || groupList.size() <= 0){  
  64.                 return 0;  
  65.             }  
  66.             int count = 0;  
  67.             for(GroupBean group : groupList){  
  68.                 count ++;  
  69.                 List<ChildBean> childList = group.getChildList();  
  70.                 if(childList == null){  
  71.                     continue;  
  72.                 }  
  73.                 if(group.isExpand()){  
  74.                     count += childList.size();  
  75.                 }  
  76.             }  
  77.             return count;  
  78.         }  
  79.         @Override  
  80.         public Object getItem(int index) {  
  81.             if(groupList == null || groupList.size() <= 0){  
  82.                 return null;  
  83.             }  
  84.             int count = 0 ;  
  85.             for(int i=0;i<groupList.size();i++){  
  86.                 GroupBean group = groupList.get(i);  
  87.                 if(index == count){  
  88.                     return group;  
  89.                 }  
  90.                 count++;  
  91.                 if(!group.isExpand()){  
  92.                     continue;  
  93.                 }  
  94.                 List<ChildBean> childList = group.getChildList();  
  95.                 if(childList == null){  
  96.                     continue;  
  97.                 }  
  98.                 for(int j=0;j<childList.size();j++){  
  99.                     ChildBean child = childList.get(j);  
  100.                     if(index == count){  
  101.                         return child;  
  102.                     }  
  103.                     count++;  
  104.                 }  
  105.             }  
  106.             return null;  
  107.         }  
  108.         @Override  
  109.         public long getItemId(int itemid) {  
  110.             return itemid;  
  111.         }  
  112.         @Override  
  113.         public View getView(final int position, View convertView, ViewGroup parent) {  
  114.             ViewHolder holder = null;  
  115.             if(convertView == null){  
  116.                 holder = new ViewHolder();  
  117.                 int type = getItemViewType(position);  
  118.                 if(type == VIEW_TYPE_GROUP){  
  119.                     convertView = new GroupView(context, parent);  
  120.                     holder.groupView = (GroupView)convertView;  
  121.                 }else{  
  122.                     convertView = new ChildView(context, parent);  
  123.                     holder.childView = (ChildView)convertView;  
  124.                 }  
  125.                 convertView.setTag(holder);  
  126.             }else{  
  127.                 holder = (ViewHolder)convertView.getTag();  
  128.             }  
  129.             int type = getItemViewType(position);  
  130.             if(type == VIEW_TYPE_GROUP){  
  131.                 final GroupView groupView = holder.groupView;  
  132.                 final GroupBean groupBean = (GroupBean)getItem(position);  
  133.                 groupView.setName(groupBean.getName());  
  134.                 groupView.setExpand(groupBean.isExpand());  
  135.                 groupView.setOnClickListener(new View.OnClickListener() {  
  136.                     @Override  
  137.                     public void onClick(View v) {  
  138.                         groupBean.setExpand(!groupBean.isExpand());  
  139.                         notifyDataSetChanged();  
  140.                         if(groupListener != null){  
  141.                             groupListener.onGroupClicked(groupView, groupBean, position);  
  142.                         }  
  143.                     }  
  144.                 });  
  145.                 return groupView;  
  146.             }else{  
  147.                 final ChildView childView = holder.childView;  
  148.                 final ChildBean childBean = (ChildBean)getItem(position);  
  149.                 childView.setName(childBean.getName());  
  150.                 childView.setOnClickListener(new View.OnClickListener() {  
  151.                     @Override  
  152.                     public void onClick(View v) {  
  153.                         if(childListener != null){  
  154.                             childListener.onChildClicked(childView, childBean, position);  
  155.                         }  
  156.                     }  
  157.                 });  
  158.                 return childView;  
  159.             }  
  160.         }  
  161.           
  162.         @Override  
  163.         public int getItemViewType(int position) {  
  164.             Object item = getItem(position);  
  165.             if(item instanceof GroupBean){  
  166.                 return VIEW_TYPE_GROUP;  
  167.             }else{  
  168.                 return VIEW_TYPE_CHILD;  
  169.             }  
  170.         }  
  171.         @Override  
  172.         public int getViewTypeCount() {  
  173.             return 2;  
  174.         }  
  175.           
  176.         public void setGroupListener(OnGroupClickListener groupListener) {  
  177.             this.groupListener = groupListener;  
  178.         }  
  179.           
  180.         public void setChildListener(OnChildClickListener childListener) {  
  181.             this.childListener = childListener;  
  182.         }  
  183.           
  184.         public void destroy(){  
  185.             this.context = null;  
  186.             this.groupList = null;  
  187.             this.groupListener = null;  
  188.             this.childListener = null;  
  189.         }  
  190.           
  191.         private class ViewHolder{  
  192.             GroupView groupView;  
  193.             ChildView childView;  
  194.         }  
  195.     }  
  196.       
  197.     private List<GroupBean> getListData(){  
  198.         List<GroupBean> itemList = new ArrayList<GroupBean>();  
  199.         for(int i=0;i<10;i++){  
  200.             GroupBean group = new GroupBean();  
  201.             group.setName("group_"+(i+1));  
  202.             List<ChildBean> childList = new ArrayList<ChildBean>();  
  203.             for(int j=0;j<3;j++){  
  204.                 ChildBean child = new ChildBean();  
  205.                 child.setName("child"+j);  
  206.                 childList.add(child);  
  207.             }  
  208.             group.setChildList(childList);  
  209.             itemList.add(group);  
  210.         }  
  211.         return itemList;  
  212.     }  
  213.       
  214.     private static class GroupBean{  
  215.         private boolean isExpand;  
  216.         private String name;  
  217.         private List<ChildBean> childList;  
  218.           
  219.         public String getName() {  
  220.             return name;  
  221.         }  
  222.         public void setName(String name) {  
  223.             this.name = name;  
  224.         }  
  225.         public List<ChildBean> getChildList() {  
  226.             return childList;  
  227.         }  
  228.         public void setChildList(List<ChildBean> childList) {  
  229.             this.childList = childList;  
  230.         }  
  231.         public boolean isExpand() {  
  232.             return isExpand;  
  233.         }  
  234.         public void setExpand(boolean isExpand) {  
  235.             this.isExpand = isExpand;  
  236.         }  
  237.           
  238.     }  
  239.       
  240.     private static class ChildBean{  
  241.         private String name;  
  242.         public String getName() {  
  243.             return name;  
  244.         }  
  245.         public void setName(String name) {  
  246.             this.name = name;  
  247.         }  
  248.     }  
  249.       
  250.     private static class GroupView extends RelativeLayout {  
  251.         private TextView nameView;  
  252.         private ImageView arrowView;  
  253.         public GroupView(Context context, ViewGroup parent) {  
  254.             super(context);  
  255.             init(parent);  
  256.         }  
  257.         private void init(ViewGroup parent) {  
  258.             final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());  
  259.             View v = mLayoutInflater.inflate(R.layout.list_item_group, parent, false);  
  260.             addView(v);  
  261.             nameView = (TextView)v.findViewById(R.id.username);  
  262.             arrowView = (ImageView)v.findViewById(R.id.group_arrow);  
  263.         }  
  264.         public void setName(String name) {  
  265.             this.nameView.setText(name);  
  266.         }  
  267.         public void setExpand(boolean expand){  
  268.             if(expand){  
  269.                 arrowView.setBackgroundResource(R.drawable.arrow_down);  
  270.             }else{  
  271.                 arrowView.setBackgroundResource(R.drawable.arrow_right);  
  272.             }  
  273.         }  
  274.     }  
  275.       
  276.     private static class ChildView extends RelativeLayout {  
  277.         private TextView nameView;  
  278.         public ChildView(Context context,  ViewGroup parent) {  
  279.             super(context);  
  280.             init(parent);  
  281.         }  
  282.         private void init(ViewGroup parent) {  
  283.             final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());  
  284.             View v = mLayoutInflater.inflate(R.layout.list_item_child, parent, false);  
  285.             addView(v);  
  286.             nameView = (TextView)v.findViewById(R.id.username);  
  287.         }  
  288.         public void setName(String name) {  
  289.             this.nameView.setText(name);  
  290.         }  
  291.     }  
  292. }  
activity_main.xml:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <ListView  
  7.         android:id="@+id/listview"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent" />  
  10. </LinearLayout>  
list_item_group.xml

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.     <ImageView  
  6.         android:id="@+id/group_arrow"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_alignParentLeft="true"  
  10.         android:layout_centerVertical="true"  
  11.         android:layout_marginLeft="20dp" />  
  12.     <TextView  
  13.         android:id="@+id/username"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="50dp"  
  16.         android:layout_centerInParent="true" />  
  17. </RelativeLayout>  
list_item_child.xml:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.     <ImageView  
  6.         android:id="@+id/usericon"  
  7.         android:layout_width="50dp"  
  8.         android:layout_height="50dp"  
  9.         android:src="@drawable/ic_launcher" />  
  10.     <TextView  
  11.         android:id="@+id/username"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginLeft="20dp"  
  15.         android:layout_toRightOf="@id/usericon" />  
  16. </RelativeLayout>  
源码在:http://download.csdn.net/detail/goldenfish1919/7703143
0 0
原创粉丝点击