Android自带控件ExpandableListView的使用

做项目时恰好有用到ExpandableListView,用法比ListView要复杂些,具体代码和效果图如下:
1.效果图:
Android自带控件ExpandableListView的使用_第1张图片

2.布局:
布局文件比较简单:
activity的布局:

  <ExpandableListView  android:id="@+id/elv" android:layout_width="fill_parent" android:layout_height="fill_parent" >
    </ExpandableListView>

groupview的布局:

<TextView
        android:id="@+id/titile"
        android:textSize="22sp"
        android:padding="5dp"
        android:background="#FFFF00"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="标题" />

childview的布局:

<ImageView  android:id="@+id/iv1" android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" />

    <ImageView  android:id="@+id/iv2" android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" />

    <ImageView  android:id="@+id/iv3" android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" />

3.代码:
public class MainActivity extends Activity {

private ExpandableListView mListView;
private int[] imagesId ={R.drawable.emoji_054,R.drawable.emoji_058,R.drawable.emoji_059,
        R.drawable.emoji_095,R.drawable.emoji_096,R.drawable.emoji_097,
        R.drawable.emoji_099,R.drawable.emoji_100,R.drawable.emoji_101};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mListView = (ExpandableListView) findViewById(R.id.elv);

    MyAdapter adapter = new MyAdapter();
    mListView.setAdapter(adapter);
}

private class MyAdapter extends BaseExpandableListAdapter{

    @Override
    public int getGroupCount() {
        //组数
        return 3;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // 孩子的个数
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if(groupPosition==0){
            return "风景";
        }else if(groupPosition==1){
            return "头像";
        }else if(groupPosition==2){
            return "动物";
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        //child的对象都是图片id,返回值为int类型
        return imagesId[3*groupPosition+childPosition];

    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String title = (String) getGroup(groupPosition);
        View view = View.inflate(getApplicationContext(), R.layout.view_group, null);
        TextView titleTextView= (TextView) view.findViewById(R.id.titile);
        titleTextView.setText(title);
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        //int imageid = (Integer) getChild(groupPosition, childPosition);
        View view = View.inflate(getApplicationContext(), R.layout.view_child, null);
        ImageView view1 = (ImageView) view.findViewById(R.id.iv1);
        ImageView view2 = (ImageView) view.findViewById(R.id.iv2);
        ImageView view3 = (ImageView) view.findViewById(R.id.iv3);
        //此处要注意positon的取值,避免角标越界和图片取值相同的情况
        view1.setBackgroundResource(imagesId[3*groupPosition+0]);
        view2.setBackgroundResource(imagesId[3*groupPosition+1]);
        view3.setBackgroundResource(imagesId[3*groupPosition+2]);
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}

}

你可能感兴趣的:(Android自带控件ExpandableListView的使用)