android--自定义ExpandableListView+隐藏指示器图片+防数据显示混乱
2015-01-23 17:14
543 查看
类似于ListView,自定义ExpandableListView只需要写一个适配器类(继承自BaseExpandableListAdapter即可):
import java.util.List;
import java.util.zip.Inflater;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
public class MExpandableListViewAdapter extends BaseExpandableListAdapter{
private Activity mActivity;
private List<String> listGroups;
private List<List<String>> listChilds;
public MExpandableListViewAdapter(
Activity mActivity, List<String> listGroups,
List<List<String>> listChilds) {
this.mActivity = mActivity;
this.listGroups = listGroups;
this.listChilds = listChilds;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return listChilds.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
ChildViewHolder childViewHolder;
View view = convertView;
if (view ==null) {
childViewHolder = new ChildViewHolder();
view = LayoutInflater.from(mActivity).inflate(R.layout.list_item_detail, null);
}else{
childViewHolder = (ChildViewHolder) view.getTag();
}
TextView tView = (TextView) view.findViewById(R.id.tv_content_helper_item_list_detail);
tView.setText(listChilds.get(groupPosition).get(0));
view.setTag(childViewHolder);
return view;
}
class ChildViewHolder{
LinearLayout llytContent;
TextView tvAnswer;
}
@Override
public int getChildrenCount(int arg0) {
return listChilds.get(arg0).size();
}
@Override
public Object getGroup(int arg0) {
return listGroups.get(arg0);
}
@Override
public int getGroupCount() {
if (listGroups!=null) {
return listGroups.size();
}
return 0;
}
@Override
public long getGroupId(int arg0) {
return arg0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView==null) {
groupViewHolder = new GroupViewHolder();
convertView = LayoutInflater.from(mActivity).inflate(R.layout.list_item, null);
convertView.setTag(groupViewHolder);
}
else{
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
TextView tView = (TextView) convertView.findViewById(R.id.tv_title_helper_item_list);
tView.setText(listGroups.get(groupPosition));
View viewArrow = convertView.findViewById(R.id.viewArrow);
if (isExpanded) {
viewArrow.setBackgroundDrawable(mActivity.getResources().getDrawable(
R.drawable.bg_arrow_round_d));
}
else{
viewArrow.setBackgroundDrawable(mActivity.getResources().getDrawable(
R.drawable.bg_arrow_round));
}
return convertView;
}
class GroupViewHolder{
TextView tvQuestion;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
}
其中list_item.xml布局文件的内容为:
<?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:background="@color/white"
android:orientation="horizontal"
android:paddingTop="2dp" >
<TextView
android:id="@+id/tv_title_helper_item_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:textColor="@color/color_bg_index"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:id="@+id/viewArrow"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:background="@drawable/bg_arrow_round" />
</LinearLayout>
其中list_item_detail.xml布局文件的内容为:
其中bg_arrow_round和bg_arrow_round_d分别为向右和向下的箭头:
import java.util.List;
import java.util.zip.Inflater;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
public class MExpandableListViewAdapter extends BaseExpandableListAdapter{
private Activity mActivity;
private List<String> listGroups;
private List<List<String>> listChilds;
public MExpandableListViewAdapter(
Activity mActivity, List<String> listGroups,
List<List<String>> listChilds) {
this.mActivity = mActivity;
this.listGroups = listGroups;
this.listChilds = listChilds;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return listChilds.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
ChildViewHolder childViewHolder;
View view = convertView;
if (view ==null) {
childViewHolder = new ChildViewHolder();
view = LayoutInflater.from(mActivity).inflate(R.layout.list_item_detail, null);
}else{
childViewHolder = (ChildViewHolder) view.getTag();
}
TextView tView = (TextView) view.findViewById(R.id.tv_content_helper_item_list_detail);
tView.setText(listChilds.get(groupPosition).get(0));
view.setTag(childViewHolder);
return view;
}
class ChildViewHolder{
LinearLayout llytContent;
TextView tvAnswer;
}
@Override
public int getChildrenCount(int arg0) {
return listChilds.get(arg0).size();
}
@Override
public Object getGroup(int arg0) {
return listGroups.get(arg0);
}
@Override
public int getGroupCount() {
if (listGroups!=null) {
return listGroups.size();
}
return 0;
}
@Override
public long getGroupId(int arg0) {
return arg0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView==null) {
groupViewHolder = new GroupViewHolder();
convertView = LayoutInflater.from(mActivity).inflate(R.layout.list_item, null);
convertView.setTag(groupViewHolder);
}
else{
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
TextView tView = (TextView) convertView.findViewById(R.id.tv_title_helper_item_list);
tView.setText(listGroups.get(groupPosition));
View viewArrow = convertView.findViewById(R.id.viewArrow);
if (isExpanded) {
viewArrow.setBackgroundDrawable(mActivity.getResources().getDrawable(
R.drawable.bg_arrow_round_d));
}
else{
viewArrow.setBackgroundDrawable(mActivity.getResources().getDrawable(
R.drawable.bg_arrow_round));
}
return convertView;
}
class GroupViewHolder{
TextView tvQuestion;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
}
其中list_item.xml布局文件的内容为:
<?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:background="@color/white"
android:orientation="horizontal"
android:paddingTop="2dp" >
<TextView
android:id="@+id/tv_title_helper_item_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:textColor="@color/color_bg_index"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:id="@+id/viewArrow"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:background="@drawable/bg_arrow_round" />
</LinearLayout>
其中list_item_detail.xml布局文件的内容为:
<?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="wrap_content" android:background="@color/color_bg" android:orientation="vertical" android:paddingRight="10dp" > <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/bg_border" /> <TextView android:id="@+id/tv_content_helper_item_list_detail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:textColor="@color/color_text" android:textSize="14sp" /> </LinearLayout>如果不想显示指示图标(箭头),需要在主activity绑定的layout文件中设置groupIndicator为透明颜色即可。即<color name="transparent">#00000000</color>。
<ExpandableListView android:id="@+id/list_view_helper" android:layout_width="match_parent" android:layout_height="0dp" android:scrollbars="none" android:fadingEdge="none" android:groupIndicator="@color/transparent" android:layout_weight="1" android:cacheColorHint="#00000000" android:dividerHeight="1dp" android:footerDividersEnabled="false" />
其中bg_arrow_round和bg_arrow_round_d分别为向右和向下的箭头:
相关文章推荐
- ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据
- Android 解决在ListView历史复用中Edittext数据显示混乱
- Android中解决在ListView历史复用中Edittext数据显示混乱
- android中ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法
- Android中ListView没有数据的时候,显示为一张图片
- Android 自定义ListView只显示第一条数据的问题
- 一步一步学android之控件篇——ListView自定义显示数据格式
- android中ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法
- 二、Android 解决在ListView历史复用中Edittext数据显示混乱
- (原创)使用AsyncTask(带修改线程池方式)+自定义ImageLoader+LRU算法对图片三级缓存及其显示优化(只有在ListView滑动停止的时候才去网络请求获取图片数据)
- android之listview无数据时显示自定义view
- android中ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法
- Android 解决在ListView历史复用中Edittext数据显示混乱
- Android 解决在ListView历史复用中Edittext数据显示混乱
- android ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法
- Android 解决在ListView历史复用中数据显示混乱
- Android在ListView显示图片(重复混乱闪烁问题)
- Android 自定义ListView + Gallery 组合,在ListView的一个item中显示多张图片
- android从服务器端读取图片和数据混合流并显示在listview
- Android 自定义Adapter 但listview 只显示第一条数据