Android自定义ExpandableListView
2013-07-18 16:46
239 查看
http://hi.baidu.com/spz_mao/item/ae984bd403d1f81f21e250ef
实现这样的效果需要自定义一个Adapter,自定义的Adapter继承BaseExpandableListAdapter,重写getGroupView和
getChildView方法时实例化自己的布局文件就可以了。下面是实现代码:
主布局文件 main.xml
子视图布局文件
child_layout.xml
分组视图布局文件 group_layout.xml
自定义适配器 MyElistAdapter.java
MainActivity.java
完全仿QQ好友列表,自定义ExpandableListView!
/article/2305671.html
Android ExpandableListView 样式自定义
http://www.open-open.com/lib/view/open1331692400702.html
Android ExpandableListView自定义Adapter
http://mobile.9sssd.com/android/art/1397
实现这样的效果需要自定义一个Adapter,自定义的Adapter继承BaseExpandableListAdapter,重写getGroupView和
getChildView方法时实例化自己的布局文件就可以了。下面是实现代码:
主布局文件 main.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffffff" > <ExpandableListView android:id="@+id/elist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" /> </LinearLayout>
子视图布局文件
child_layout.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="30dp" > <ImageView android:id="@+id/civ" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/ren" android:padding="5dp" /> <TextView android:id="@+id/ctv" android:layout_height="wrap_content" android:layout_width="wrap_content" android:padding="5dp" android:textColor="#000000" /> </LinearLayout>
分组视图布局文件 group_layout.xml
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grlayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/gtv" android:layout_width="wrap_content" android:layout_height="fill_parent" android:paddingLeft="20dp" android:textColor="#000000" /> <ImageView android:id="@+id/giv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:paddingRight="10dp" android:src="@drawable/jia" /> </RelativeLayout>
自定义适配器 MyElistAdapter.java
publicclass MyElistAdapter extends BaseExpandableListAdapter { // 分组数据 private String[] group = { "A组", "B组", "C组", "D组" }; private String[][] child = { { "A01", "A02", "A03" }, { "B01", "B02", "B03" }, { "C01", "C02", "C03" }, { "D04", "D05", "D06" } }; private Context mContext; public MyElistAdapter(Context mContext) { super(); this.mContext = mContext; } @Override public int getGroupCount() { return group.length; } @Override public int getChildrenCount(int groupPosition) { return child[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return group[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return child[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { returnchildPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // 实例化布局文件 RelativeLayout glayout = (RelativeLayout) LayoutInflater.from(mContext) .inflate(R.layout.group_layout, null); ImageView iv = (ImageView) glayout.findViewById(R.id.giv); // 判断分组是否展开,分别传入不同的图片资源 if (isExpanded) { iv.setImageResource(R.drawable.jian); } else { iv.setImageResource(R.drawable.jia); } TextView tv = (TextView) glayout.findViewById(R.id.gtv); tv.setText(this.getGroup(groupPosition).toString()); return glayout; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // 实例化布局文件 LinearLayout clayout = (LinearLayout) LayoutInflater.from(mContext) .inflate(R.layout.child_layout, null); TextView tv = (TextView) clayout.findViewById(R.id.ctv); tv.setText(getChild(groupPosition, childPosition).toString()); return clayout; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
MainActivity.java
public class ExpandableTestActivity extends Activity { private ExpandableListView elistview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); elistview = (ExpandableListView) findViewById(R.id.elist); //这里要把系统自带的图标去掉 elistview.setGroupIndicator(null); elistview.setAdapter(new ElistAdapter(this)); // elistview.setChildDivider(null); // elistview.setDivider(null); } }
完全仿QQ好友列表,自定义ExpandableListView!
/article/2305671.htmlAndroid ExpandableListView 样式自定义
http://www.open-open.com/lib/view/open1331692400702.html
Android ExpandableListView自定义Adapter
http://mobile.9sssd.com/android/art/1397
相关文章推荐
- Android 自定义 ExpandableListView
- Android自定义ExpandableListView
- Android 自定义下拉刷新ExpandableListView
- Android自定义ViewGroup(二)——带悬停标题的ExpandableListView
- android--自定义ExpandableListView+隐藏指示器图片+防数据显示混乱
- Android---自定义带CheckBox的ExpandableListView实现
- android中ExpandableListView 使用自定义Adapter的方法
- 站在巨人的肩膀上---重新自定义 android- ExpandableListView 收缩类,实现列表的可收缩扩展
- android ExpandableListView的group是一个自定义的布局,折叠图标覆盖布局上的部分内容
- Android 自定义 ExpandableListView
- Android 自定义 ExpandableListView
- Android关于自定义ExpandableListView样式
- Android 之路43---手风琴特效ExpandableListView
- Android自定义渐变式炫酷ListView下拉刷新动画
- 玩转android自定义控件二——自定义索引栏listview
- android自定义ListView高度设置无效
- android ExpandableListView 四级目录结构
- 深更半夜搞android—— 自定义ListView
- Android ExpandableListView嵌套ListView
- Android中ExpandableListView的使用