Android成長之路-BaseExpandableListAdapter的用法

BaseExpandableListAdapter是ExpandableListAdapter的抽象基類,從一些數據中提供數據和視圖給可摺疊列表視圖。

例子詳解:

首先定義一個xml佈局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ExpandableListView   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/ecpandable"  
  11.         />  
  12.   
  13. </LinearLayout>  

Adapter:


public class TransExpandableListAdapter implements ExpandableListAdapter {
    private Context mContext;

    /**
     * ----------定義數組--------------------------------------------------
     */
    private int[] images = new int[]{
            R.drawable.ic_launcher,
            R.drawable.demo_1,
            R.drawable.demo_2
    };
    private String[] armTypes = new String[]{
            "神族", "蟲族", "人族"
    };
    private String[][] arms = new String[][]{
            {"狂戰士", "龍騎士", "黑暗聖堂"},
            {"小狗", "飛龍", "自爆妃子"},
            {"步兵", "傘兵", "護士mm"}
    };

    public TransExpandableListAdapter(Context context) {
        this.mContext = context;

    }


@Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getGroupCount() {
        return armTypes.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return arms[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return armTypes[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return arms[groupPosition][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) {
        TextView textView = getTextView();//調用定義的getTextView()方法
        textView.setText(getGroup(groupPosition).toString());//添加數據
        return textView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LinearLayout ll = new LinearLayout(mContext);
        ll.setOrientation(LinearLayout.VERTICAL);//定義爲縱向排列
        ImageView logo = new ImageView(mContext);
        logo.setImageResource(images[groupPosition]);//添加圖片
        ll.addView(logo);
        TextView textView = getTextView();//調用定義的getTextView()方法
        textView.setText(getChild(groupPosition, childPosition).toString());//添加數據
        ll.addView(textView);
        return ll;
    }

 @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

@Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    //定義一個TextView
    private TextView getTextView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 40);
        TextView textView = new TextView(mContext);
        textView.setLayoutParams(lp);
        textView.setPadding(50, 0, 0, 0);
        textView.setTextSize(16);
        return textView;
    }

}


Activity中:

 private TransExpandableListView mListView;
    private ExpandableListAdapter mAdapter;

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

    private void initView() {
        mListView = (TransExpandableListView) findViewById(R.id.trans_expandable_listview);
        mAdapter = new TransExpandableListAdapter(this);
        mListView.setAdapter(mAdapter);

    }


效果圖:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章