android ExpandableListView簡單應用

  首先我們還是來看一些案例,還是拿搜狐新聞客戶端,因為我天天上下班沒事愛看這個東東,因為上班沒時間看新聞,上下班路途之餘瀏覽下新聞打發時間嘛.          
                         
看這個效果挺棒吧,其實實現起來也不難,我簡單說明下.
首先我們用到的控件是:ExpandableListView
佈局文件:
[java] 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
 
    <!– 
     android:groupIndicator="@null" 取消默認圖片 
    android:childIndicatorLeft 設置孩子左邊間距 
    android:piderHeight 這個高度一定要設置,不然顯示不出來分割線,估計默認為0 吧 
     android:childDivider="@drawable/child_bg" 這個直接引color,或者圖片會導致整個孩子背景都為這個顏色  ,不知道原因,如果有誰知道,請Give me say. 
    –> 
 
    <ExpandableListView 
        android:id="@+id/expandablelist" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:cacheColorHint="@null" 
        android:childDivider="@drawable/child_bg" 
        android:childIndicatorLeft="0dp" 
        android:pider="@color/Grey" 
        android:piderHeight="1dp" 
        android:groupIndicator="@null" 
        android:scrollbarAlwaysDrawHorizontalTrack="true" > 
    </ExpandableListView> 
 
</RelativeLayout> 
MyexpandableListAdapter.java
[java] 
/***
     * 數據源
     * 
     * @author Administrator
     * 
     */ 
    class MyexpandableListAdapter extends BaseExpandableListAdapter { 
        private Context context; 
        private LayoutInflater inflater; 
 
        public MyexpandableListAdapter(Context context) { 
            this.context = context; 
            inflater = LayoutInflater.from(context); 
        } 
 
        // 返回父列表個數 
        @Override 
        public int getGroupCount() { 
            return groupList.size(); 
        } 
 
        // 返回子列表個數 
        @Override 
        public int getChildrenCount(int groupPosition) { 
            return childList.get(groupPosition).size(); 
        } 
 
        @Override 
        public Object getGroup(int groupPosition) { 
 
            return groupList.get(groupPosition); 
        } 
 
        @Override 
        public Object getChild(int groupPosition, int childPosition) { 
            return childList.get(groupPosition).get(childPosition); 
        } 
 
        @Override 
        public long getGroupId(int groupPosition) { 
            return groupPosition; 
        } 
 
        @Override 
        public long getChildId(int groupPosition, int childPosition) { 
            return childPosition; 
        } 
 
        @Override 
        public boolean hasStableIds() { 
 
            return true; 
        } 
 
        @Override 
        public View getGroupView(int groupPosition, boolean isExpanded, 
                View convertView, ViewGroup parent) { 
            GroupHolder groupHolder = null; 
            if (convertView == null) { 
                groupHolder = new GroupHolder(); 
                convertView = inflater.inflate(R.layout.group, null); 
                groupHolder.textView = (TextView) convertView 
                        .findViewById(R.id.group); 
                groupHolder.imageView = (ImageView) convertView 
                        .findViewById(R.id.image); 
                groupHolder.textView.setTextSize(15); 
                convertView.setTag(groupHolder); 
            } else { 
                groupHolder = (GroupHolder) convertView.getTag(); 
            } 
 
            groupHolder.textView.setText(getGroup(groupPosition).toString()); 
            if (isExpanded)// ture is Expanded or false is not isExpanded 
                groupHolder.imageView.setImageResource(R.drawable.expanded); 
            else 
                groupHolder.imageView.setImageResource(R.drawable.collapse); 
            return convertView; 
        } 
 
        @Override 
        public View getChildView(int groupPosition, int childPosition, 
                boolean isLastChild, View convertView, ViewGroup parent) { 
            if (convertView == null) { 
                convertView = inflater.inflate(R.layout.item, null); 
            } 
            TextView textView = (TextView) convertView.findViewById(R.id.item); 
            textView.setTextSize(13); 
            textView.setText(getChild(groupPosition, childPosition).toString()); 
            return convertView; 
        } 
 
        @Override 
        public boolean isChildSelectable(int groupPosition, int childPosition) { 
            return true; 
        } 
    } 
 
    @Override 
    public boolean onGroupClick(final ExpandableListView parent, final View v, 
            int groupPosition, final long id) { 
 
        return false; 
    } 
上面實現起來比較簡單.相信對listview熟悉的朋友看這個一定很熟悉,無外乎就是多瞭個孩子.
selector_group.xml
[java] 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 
    <item android:drawable="@color/Grey" android:state_pressed="true"></item> 
    <item android:drawable="@color/Grey" android:state_selected="true"></item> 
    <item android:drawable="@color/LightGray"></item> 
 
</selector> 
selector_item.xml  同理.

效果圖:               
                    
效果雖然醜瞭點,不過就是這麼回事,至於顯示group的item,還是孩子的item,你可以隨意定制.
   不想敲的同學,可以下載源碼,稍作調整.
  

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *