Customize ExpandableListView to implement the APP help interface and expandablelistview
A new project recently developed by the company contains a help interface, that is, after clicking a question, the answer is automatically displayed below. Many people first think that they should write a TextView below and set android: visibility = "gone". When you click it, set it to visible in the code... however, it takes enough time to do many things. Why not find a better solution .. the following code reference: http://blog.csdn.net/zoeice/article/details/7729982
The project implementation interface is as follows:
The custom ExpandableListView code is:
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 (); // hide the scroll bar this. setVerticalScrollBarEnabled (false); // hide the default arrow this. setGroupIndicator (null); setCacheColorHint (Color. TRANSPARENT); setDividerHeight (0); setChildrenDrawnWithCacheEnabled (false); setGroupIndicator (null); // hide the selected yellow highlighted ColorDrawable drawable_tranparent _ = new ColorDrawable (Color. TRANSPARENT); setSelector (drawable_tranparent _); mAdapter = new HelpCenterAdapter (); setAdapter (mAdapter);} public HelpCenterView (Context context, AttributeSet attrs) {this (context );} /*** obtain Group data * @ 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;}/*** get Child data * @ 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) {// only return 1 is displayed under Child;} @ 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); // if (isExpanded) {groupText when the Group is expanded. setTextColor (ColorStateList. valueOf (Color. parseColor ("# ff5e4c"); groupImg. setImageResource (R. drawable. icon_more_down); // when the Group is not expanded} else {groupText. setTextColor (ColorStateList. valueOf (Color. parseColor ("#555555"); groupImg. setImageResource (R. drawable. icon_main_more);} // sets Group content 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 ;}}}
In the layout file, you only need:
<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" />
Very simple... the custom ExpandableListView only integrates the adapter.