自訂ExpandableListView,實現APP協助介面,expandablelistview
最近公司新做的一個項目,裡邊帶有協助介面,即點擊一條問題後,自動在下面展開答案,很多人第一想法就是在下面寫個TextView且設定android:visibility="gone",當點擊時就在代碼中將其設為可見...但是,這樣耗得時間,足以做很多事了,為何不找下更好的解決方案呢..以下代碼參考:http://blog.csdn.net/zoeice/article/details/7729982
項目要實現的介面如下:
而我們自訂的ExpandableListView代碼為:
public class HelpCenterView extends ExpandableListView{private Context mContext;private List<String> mGroups;private List<String> mChilds;private HelpCenterAdapter mAdapter;public HelpCenterView(Context context) {super(context);mContext = context;mGroups = getGroupData();mChilds = getChildData();// 隱藏捲軸this.setVerticalScrollBarEnabled(false);// 隱藏左邊預設箭頭this.setGroupIndicator(null);setCacheColorHint(Color.TRANSPARENT);setDividerHeight(0);setChildrenDrawnWithCacheEnabled(false);setGroupIndicator(null);// 隱藏選擇的黃色高亮ColorDrawable drawable_tranparent_ = new ColorDrawable(Color.TRANSPARENT);setSelector(drawable_tranparent_);mAdapter = new HelpCenterAdapter();setAdapter(mAdapter);}public HelpCenterView(Context context, AttributeSet attrs) {this(context);}/** * 獲得Group資料 * @return */private List<String> getGroupData() {String[] groups = mContext.getResources().getStringArray(R.array.help_center_group);List<String> list = new ArrayList<String>();for(int i=0; i<groups.length; i++) {list.add(groups[i]);}return list;}/** * 獲得Child資料 * @return */private List<String> getChildData() {String[] childs = mContext.getResources().getStringArray(R.array.help_center_child);List<String> list = new ArrayList<String>();for(int i=0; i<childs.length; i++) {list.add(childs[i]);}return list;}public class HelpCenterAdapter extends BaseExpandableListAdapter {@Overridepublic int getGroupCount() {return mGroups.size();}@Overridepublic int getChildrenCount(int groupPosition) {//Child下只顯示一條return 1;}@Overridepublic Object getGroup(int groupPosition) {return mGroups.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {return mChilds.get(groupPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {if(convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.item_help_center_group, null);}TextView groupText = (TextView)convertView.findViewById(R.id.id_tv_item_help_center_group);ImageView groupImg = (ImageView)convertView.findViewById(R.id.id_iv_item_help_center_group);//Group展開時if(isExpanded) {groupText.setTextColor(ColorStateList.valueOf(Color.parseColor("#ff5e4c")));groupImg.setImageResource(R.drawable.icon_more_down);//Group未展開時} else {groupText.setTextColor(ColorStateList.valueOf(Color.parseColor("#555555")));groupImg.setImageResource(R.drawable.icon_main_more);}//設定Group內容groupText.setText(mGroups.get(groupPosition));return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {if(convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.item_help_center_child, null);}TextView childText = (TextView)convertView.findViewById(R.id.id_tv_item_help_center_child);childText.setText(mChilds.get(groupPosition));return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}}
在布局檔案中只需要:
<com.example.test.HelpCenterView android:id="@+id/id_elv_help_center" android:layout_width="match_parent" android:layout_height="wrap_content" android:dividerHeight="0.5dp" android:listSelector="#00000000" android:divider="@color/line" />
很簡單..自訂的ExpandableListView只是把適配器整合在裡邊而已