ExpandableListView初步使用

来源:互联网 发布:电脑弹钢琴软件 编辑:程序博客网 时间:2024/06/04 19:49

大家好,我是刚学Android不久的小菜鸟,虽然觉得技术不行,但是我觉得还是想写一些东西,能和一起还在努力的人分享一下,如果觉得我写的不好,或者代码有不对的地方,还请各路大神多多指导。

好了,开始写正文。

还是先看layout布局中的代码

<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"     >    <ExpandableListView         android:id="@+id/expandable"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:drawSelectorOnTop="false">        <!-- drawSelectorOnTop属性用于背景颜色不会覆盖内容 -->    </ExpandableListView>    </LinearLayout>
当然还有一些其他的基本属性,我这里就不写了,大家百度一下就好了。

我的项目是放在fragment中,如果有的同学是写在activity中的话有些细节稍微注意一下。

public class MyMusicFragment extends Fragment {private List<String> groupData;private List<List<Children>>childrenData;private MyMusicExpandableListViewAdapter adapter;private ExpandableListView expand;private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// TODO Auto-generated method stubview=inflater.inflate(R.layout.fragment_mymusic, null);initView();//初始化控件方法setGroupData();//获取ExpandableListView中父控件的数据setChildrenData();//获取ExpandableListView中子控件的数据setAdapter();//设置适配器控件return view;}private void setChildrenData() {// TODO Auto-generated method stubchildrenData = new ArrayList<List<Children>>();List<Children> childItem =null;if(childrenData.size()==0){childItem=new ArrayList<Children>();Children children=new Children(R.drawable.ic_stub, "我喜欢的音乐","0");childItem.add(children);}else{childItem =new ArrayList<Children>();}childrenData.add(childItem);}private void setGroupData() {// TODO Auto-generated method stubgroupData = new ArrayList<String>();groupData.add("我创建的歌单");groupData.add("我收藏的歌单");}private void initView() {// TODO Auto-generated method stubexpand=(ExpandableListView) view.findViewById(R.id.expandable);}private void setAdapter() {// TODO Auto-generated method stubadapter=new MyMusicExpandableListViewAdapter(groupData, childrenData);expand.setAdapter(adapter);}}
其实仔细发现他和listview 的区别还是不大的,我在这里对控件和数据进行了初始化,然后将数据加载到适配器中显示出来。

然后就是适配器的代码:

import java.util.List;import com.wonder.wonderplayer.MusicApp;import com.wonder.wonderplayer.R;import com.wonder.wonderplayer.entity.Children;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyMusicExpandableListViewAdapter extends BaseExpandableListAdapter {private List<String> groupTitle;private List<List<Children>> childrenList;public MyMusicExpandableListViewAdapter(List<String> groupTitle,List<List<Children>>childrenList){this.groupTitle=groupTitle;this.childrenList=childrenList;}//----------------------------------------Group--------------------------------------------//获取父控件的数量@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn groupTitle.size();}//获取指定位置的父控件@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn groupTitle.get(groupPosition);}//获取指定位置的父控件的Id@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}//获取父控件中要显示的控件@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {GroupHolder groupHolder;if(convertView==null){groupHolder=new GroupHolder();convertView=View.inflate(MusicApp.getApp(), R.layout.expandableview_group_item, null);groupHolder.iv_group=(ImageView) convertView.findViewById(R.id.iv_group);groupHolder.tv_group=(TextView) convertView.findViewById(R.id.tv_group);convertView.setTag(groupHolder);}else{groupHolder=(GroupHolder) convertView.getTag();}if(isExpanded){groupHolder.iv_group.setImageResource(R.drawable.down);}else{groupHolder.iv_group.setImageResource(R.drawable.right);}groupHolder.tv_group.setText(groupTitle.get(groupPosition));return convertView;}class GroupHolder{ImageView iv_group;TextView tv_group;}}//-------------------------------------------------------------------------------//获取子控件的数量@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn childrenList.size();}//获取指定位置父控件中的指定位置的子控件@Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childrenList.get(groupPosition).get(childPosition).getTitleName();}//获取指定位置父控件中的指定位置的子空间的Id@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;}//获取子控件中要显示的控件@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {// TODO Auto-generated method stubChildHolder childHolder;if(convertView==null){childHolder=new ChildHolder();convertView=View.inflate(MusicApp.getApp(), R.layout.expandablelistview_child_item, null);childHolder.iv_child=(ImageView) convertView.findViewById(R.id.iv_child);childHolder.tv_childTitle=(TextView) convertView.findViewById(R.id.tv_child_title);childHolder.tv_songNum=(TextView) convertView.findViewById(R.id.tv_child_songNum);convertView.setTag(childHolder);}else{childHolder=(ChildHolder) convertView.getTag();}childHolder.iv_child.setImageResource(childrenList.get(groupPosition).get(childPosition).getIamgeId());childHolder.tv_childTitle.setText(childrenList.get(groupPosition).get(childPosition).getTitleName());childHolder.tv_songNum.setText(childrenList.get(groupPosition).get(childPosition).getSongNum());return convertView;}//子项是否含有点击事件,如果有,返回值设置为true@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}class ChildHolder{ImageView iv_child;TextView tv_childTitle;TextView tv_songNum;}
之前看了很多大神写的代码,在getChildrenCount方法中返回值是childrenList.get(groupPostion).size()。刚开始我也是这么写的,后来在我的项目中就出现了一个小bug,每次点击打开子项时就崩溃了,后来我就试着改成了childrenList.size(),如果有和我出现一样的问题的童鞋你们可以尝试一下。我对这个控件进行了自定义,我把自定义部分也贴出来,大家就随意看一下吧。

group:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView         android:layout_marginLeft="10dp"        android:id="@+id/iv_group"        android:layout_width="wrap_content"        android:layout_height="30dp"        android:src="@drawable/right"/>    <TextView         android:id="@+id/tv_group"        android:layout_width="0dp"        android:layout_height="30dp"        android:layout_weight="1"        android:gravity="center_vertical"        android:textSize="18sp"        android:layout_marginLeft="10dp"        android:textColor="#666666"        android:text="我创建的歌单(0)"/>    </LinearLayout>

child:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    >    <ImageView         android:id="@+id/iv_child"        android:layout_width="80dp"         android:layout_height="80dp"        android:src="@drawable/ic_stub"        android:layout_marginLeft="10dp"        />    <LinearLayout         android:layout_width="match_parent"        android:layout_height="80dp"        android:orientation="vertical"        android:layout_marginLeft="10dp"        android:gravity="center_vertical">    <TextView         android:id="@+id/tv_child_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="我收藏的歌曲"/>    <TextView         android:id="@+id/tv_child_songNum"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="0首"/>            </LinearLayout>    </LinearLayout>
还有一个children实体类:

public class Children {private int iamgeId;private String titleName;private String songNum;public Children(int iamgeId, String titleName, String songNum) {super();this.iamgeId = iamgeId;this.titleName = titleName;this.songNum = songNum;}public Children() {super();}public int getIamgeId() {return iamgeId;}public void setIamgeId(int iamgeId) {this.iamgeId = iamgeId;}public String getTitleName() {return titleName;}public void setTitleName(String titleName) {this.titleName = titleName;}public String getSongNum() {return songNum;}public void setSongNum(String songNum) {this.songNum = songNum;}}
 排版有点混乱,大家就将就着点看好了=3=,本来挺想把效果图放上来的,但是试了很久都贴不上来,然后我放弃了,希望能对大家有点帮助,如果有不对的地方请大神们留言指教










0 0
原创粉丝点击