标签:android expandablelistview 隐藏指示器 防止数据显示错乱
类似于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); } }
<?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>
<?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--自定义ExpandableListView+隐藏指示器图片+防数据显示混乱
标签:android expandablelistview 隐藏指示器 防止数据显示错乱
原文地址:http://blog.csdn.net/wanggsx918/article/details/43058185