Android 使用ExpandableListView打造可展开列表组件

来源:互联网 发布:linux系统如何使用 编辑:程序博客网 时间:2024/05/16 09:33

在项目中要用到,需要实现的效果如下:

1、可遍历1-12个月份。

2、可查看每个月份下对应的照片。


运行效果如下:



实现思路:

一开始打算使用ScrollView嵌套一个LinearLayout,然后add进不同子项,不过这样写代码量会比较多,而且要做子项点击事件处理,逻辑会稍微复杂一点。最后使用了ExpandableListView组件,将数据全部抽离出来,依次填进去就搞好了,十分方便,在此记录一下。


代码步骤:

1、首先是布局文件: newphoto_layout.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:background="@android:color/white"    android:layout_height="match_parent">    <ExpandableListView        android:id="@+id/eblv_newphoto"        android:layout_width="match_parent"        android:layout_height="match_parent"         ></ExpandableListView>    <ImageButton        android:id="@+id/newphoto_returnbtn"        android:layout_width="50dp"        android:layout_height="50dp"        android:layout_margin="10dp"        android:src="@drawable/return_btn"/></RelativeLayout>


2、初始化组件:

    ExpandableListView eblv_newphoto;        eblv_newphoto = (ExpandableListView) findViewById(R.id.eblv_newphoto);  MyExpandableListAdapter adapter = new MyExpandableListAdapter(data);        eblv_newphoto.setAdapter(adapter);
3、编写MyExpandableListAdapter类:

  public class MyExpandableListAdapter implements ExpandableListAdapter {        String[] monthEnames = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};        List<PhotoPicture> mdata;        String[] colors = {"#B36AE1","#c62817","#022550",                "#118730","#1167bd","#560809",                "#f4d23e","#56a9f6","#f18f2e",                "#3b214c","#ea5e5b","#72be49"        };        public MyExpandableListAdapter(List<PhotoPicture> data ) {            this.mdata = data;        }        @Override        public void registerDataSetObserver(DataSetObserver observer) {        }        @Override        public void unregisterDataSetObserver(DataSetObserver observer) {        }        @Override        public int getGroupCount() {            return monthEnames.length;        }        @Override        public int getChildrenCount(int groupPosition) {            int num = 0;            for (int i = 0;i<mdata.size();i++) {                if (mdata.get(i).getTime().equals(String.valueOf(groupPosition))) {                    num++;                }            }            LogUtils.d("getChildrenCount:" + num);            return (num+1)/2;        }        @Override        public Object getGroup(int groupPosition) {            return monthEnames[groupPosition];        }        @Override        public Object getChild(int groupPosition, int childPosition) {            List<PhotoPicture> tempChildlist = new ArrayList<>();            List<PhotoPicture> resultChildlist = new ArrayList<>();            //得到该月份下所有的PhotoPicture            for (int i = 0;i<mdata.size();i++) {                if (mdata.get(i).getTime().equals(String.valueOf(groupPosition))) {                    tempChildlist.add(mdata.get(i));                 }            }            //得到result            if (tempChildlist.size() == childPosition*2+1) {                resultChildlist.add(tempChildlist.get(childPosition*2));            } else if (tempChildlist.size() == childPosition*2+2){                resultChildlist.add(tempChildlist.get(childPosition*2));                resultChildlist.add(tempChildlist.get(childPosition*2+1));            }            return resultChildlist;        }        @Override        public long getGroupId(int groupPosition) {            return groupPosition;        }        @Override        public long getChildId(int groupPosition, int childPosition) {            return childPosition;        }        @Override        public boolean hasStableIds() {            return true;        }        @Override        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {            TextView textView = new TextView(NewPhotoAty.this);            FrameLayout.LayoutParams btnParams = new FrameLayout.LayoutParams                    (FrameLayout.LayoutParams.MATCH_PARENT,                    DisplayUtil.dp2px(NewPhotoAty.this, 70));            textView.setLayoutParams(btnParams);            textView.setBackgroundColor(Color.parseColor(colors[groupPosition]));            textView.setText(monthEnames[groupPosition]);            textView.setTextSize(40);            textView.setGravity(Gravity.CENTER);            return textView;        }        String path1;        String path2;        @Override        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {            LinearLayout item_photorv = (LinearLayout) LayoutInflater.from(NewPhotoAty.this).inflate(R.layout.item_photorv,null);            ImageView img1 = (ImageView) item_photorv.findViewById(R.id.photo_rvitem_img1);            ImageView img2 = (ImageView) item_photorv.findViewById(R.id.photo_rvitem_img2);            List<PhotoPicture> resultChildlist = (List<PhotoPicture>) getChild(groupPosition,childPosition);            if (resultChildlist.size() ==1) {                NGImageloadHelper.displayImage(img1,Config.IMG_AUDIO_PATH +resultChildlist.get(0).getPath());            } else if (resultChildlist.size() ==2) {                NGImageloadHelper.displayImage(img2,Config.IMG_AUDIO_PATH +resultChildlist.get(1).getPath());            }            return item_photorv;        }        @Override        public boolean isChildSelectable(int groupPosition, int childPosition) {            return true;        }        @Override        public boolean areAllItemsEnabled() {            return false;        }        @Override        public boolean isEmpty() {            return false;        }        @Override        public void onGroupExpanded(int groupPosition) {        }        @Override        public void onGroupCollapsed(int groupPosition) {        }        @Override        public long getCombinedChildId(long groupId, long childId) {            return 0;        }        @Override        public long getCombinedGroupId(long groupId) {            return 0;        }    }






1 0
原创粉丝点击