Android-用ListView模仿ExpandableListView

來源:互聯網
上載者:User

標籤:android   des   blog   http   java   os   io   資料   

轉載請註明出處:http://blog.csdn.net/goldenfish1919/article/details/38334995

既然有現成的ExpandableListView,為啥還非得仿一個啊?是不是脫褲子放屁多此一舉呢?當然不是的。ExpandableListView只能支援兩層啊,假如你想支援三層或者更多層呢?或者說頁面結構比較複雜,就像這樣的:


用原生的ExpandableListView還真是不太好實現,也不是說不能,看上去有點複雜啊。ListView的adapter有一個getItemViewType()和getViewTypeCount()這麼兩個方法,挺好用的,我們來試下。

MainActivity.java:

public class MainActivity extends Activity {private ListViewAdapter listViewAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//資料List<GroupBean> itemList = getListData();//view和adapterListView listView = (ListView)findViewById(R.id.listview);listViewAdapter = new ListViewAdapter(this, itemList);//給adapter設定監聽listViewAdapter.setGroupListener(new OnGroupClickListener(){@Overridepublic void onGroupClicked(GroupView view, GroupBean group,int position) {Toast.makeText(MainActivity.this, "I am "+ group.getName(), Toast.LENGTH_SHORT).show();}});listViewAdapter.setChildListener(new OnChildClickListener(){@Overridepublic void onChildClicked(ChildView view, ChildBean child,int position) {Toast.makeText(MainActivity.this, "I am "+ child.getName(), Toast.LENGTH_SHORT).show();}});listView.setAdapter(listViewAdapter);}@Overrideprotected void onDestroy() {if(listViewAdapter != null){listViewAdapter.destroy();listViewAdapter = null;}super.onDestroy();}private static interface OnGroupClickListener{public void onGroupClicked(GroupView view, GroupBean group, int position);}private static interface OnChildClickListener{public void onChildClicked(ChildView view, ChildBean child, int position);}private static class ListViewAdapter extends BaseAdapter{private static final int VIEW_TYPE_GROUP = 0;private static final int VIEW_TYPE_CHILD = 1;private Context context;private List<GroupBean> groupList;private OnGroupClickListener groupListener;private OnChildClickListener childListener;public ListViewAdapter(Context context, List<GroupBean> groupList){this.context = context;this.groupList = groupList;}@Overridepublic int getCount() {if(groupList == null || groupList.size() <= 0){return 0;}int count = 0;for(GroupBean group : groupList){count ++;List<ChildBean> childList = group.getChildList();if(childList == null){continue;}if(group.isExpand()){count += childList.size();}}return count;}@Overridepublic Object getItem(int index) {if(groupList == null || groupList.size() <= 0){return null;}int count = 0 ;for(int i=0;i<groupList.size();i++){GroupBean group = groupList.get(i);if(index == count){return group;}count++;if(!group.isExpand()){continue;}List<ChildBean> childList = group.getChildList();if(childList == null){continue;}for(int j=0;j<childList.size();j++){ChildBean child = childList.get(j);if(index == count){return child;}count++;}}return null;}@Overridepublic long getItemId(int itemid) {return itemid;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if(convertView == null){holder = new ViewHolder();int type = getItemViewType(position);if(type == VIEW_TYPE_GROUP){convertView = new GroupView(context, parent);holder.groupView = (GroupView)convertView;}else{convertView = new ChildView(context, parent);holder.childView = (ChildView)convertView;}convertView.setTag(holder);}else{holder = (ViewHolder)convertView.getTag();}int type = getItemViewType(position);if(type == VIEW_TYPE_GROUP){final GroupView groupView = holder.groupView;final GroupBean groupBean = (GroupBean)getItem(position);groupView.setName(groupBean.getName());groupView.setExpand(groupBean.isExpand());groupView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {groupBean.setExpand(!groupBean.isExpand());notifyDataSetChanged();if(groupListener != null){groupListener.onGroupClicked(groupView, groupBean, position);}}});return groupView;}else{final ChildView childView = holder.childView;final ChildBean childBean = (ChildBean)getItem(position);childView.setName(childBean.getName());childView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(childListener != null){childListener.onChildClicked(childView, childBean, position);}}});return childView;}}@Overridepublic int getItemViewType(int position) {Object item = getItem(position);if(item instanceof GroupBean){return VIEW_TYPE_GROUP;}else{return VIEW_TYPE_CHILD;}}@Overridepublic int getViewTypeCount() {return 2;}public void setGroupListener(OnGroupClickListener groupListener) {this.groupListener = groupListener;}public void setChildListener(OnChildClickListener childListener) {this.childListener = childListener;}public void destroy(){this.context = null;this.groupList = null;this.groupListener = null;this.childListener = null;}private class ViewHolder{GroupView groupView;ChildView childView;}}private List<GroupBean> getListData(){List<GroupBean> itemList = new ArrayList<GroupBean>();for(int i=0;i<10;i++){GroupBean group = new GroupBean();group.setName("group_"+(i+1));List<ChildBean> childList = new ArrayList<ChildBean>();for(int j=0;j<3;j++){ChildBean child = new ChildBean();child.setName("child"+j);childList.add(child);}group.setChildList(childList);itemList.add(group);}return itemList;}private static class GroupBean{private boolean isExpand;private String name;private List<ChildBean> childList;public String getName() {return name;}public void setName(String name) {this.name = name;}public List<ChildBean> getChildList() {return childList;}public void setChildList(List<ChildBean> childList) {this.childList = childList;}public boolean isExpand() {return isExpand;}public void setExpand(boolean isExpand) {this.isExpand = isExpand;}}private static class ChildBean{private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}private static class GroupView extends RelativeLayout {private TextView nameView;private ImageView arrowView;public GroupView(Context context, ViewGroup parent) {super(context);init(parent);}private void init(ViewGroup parent) {final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());View v = mLayoutInflater.inflate(R.layout.list_item_group, parent, false);addView(v);nameView = (TextView)v.findViewById(R.id.username);arrowView = (ImageView)v.findViewById(R.id.group_arrow);}public void setName(String name) {this.nameView.setText(name);}public void setExpand(boolean expand){if(expand){arrowView.setBackgroundResource(R.drawable.arrow_down);}else{arrowView.setBackgroundResource(R.drawable.arrow_right);}}}private static class ChildView extends RelativeLayout {private TextView nameView;public ChildView(Context context,  ViewGroup parent) {super(context);init(parent);}private void init(ViewGroup parent) {final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());View v = mLayoutInflater.inflate(R.layout.list_item_child, parent, false);addView(v);nameView = (TextView)v.findViewById(R.id.username);}public void setName(String name) {this.nameView.setText(name);}}}
activity_main.xml:

<LinearLayout 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:orientation="vertical" >    <ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>
list_item_group.xml

<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="wrap_content" >    <ImageView        android:id="@+id/group_arrow"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_centerVertical="true"        android:layout_marginLeft="20dp" />    <TextView        android:id="@+id/username"        android:layout_width="wrap_content"        android:layout_height="50dp"        android:layout_centerInParent="true" /></RelativeLayout>
list_item_child.xml:

<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="wrap_content" >    <ImageView        android:id="@+id/usericon"        android:layout_width="50dp"        android:layout_height="50dp"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/username"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:layout_toRightOf="@id/usericon" /></RelativeLayout>
源碼在:http://download.csdn.net/detail/goldenfish1919/7703143

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.