ExpandableListView總結

在實現類似通信錄,等帶有兩層或多層組織架構的列表功能時,會使用到ExpandableListView,他是ListView的子類,使用方式也和ListView大同小異,這裏做一個總結。

 

關鍵類:

1、SimpleExpandableListAdapter

2、SimpleCursorTreeAdapter

3、BaseExpandableListAdapter

4、ExpandableListActivity

 

SimpleExpandableListAdapter

構造方法:

SimpleExpandableListAdapter(Context context,

List<? extends Map<String, ?>> groupData, //組數據

int groupLayout,//組數據使用的佈局文件

String[] groupFrom, //組數據所使用的key數組

int[] groupTo,//組數據放置的控件id,例如TextView

List<? extends List<? extends Map<String,?>>> childData,//子元素數據

int childLayout, //子元素使用的佈局文件

String[] childFrom, //子元素數據所使用的key數組

int[] childTo)//子元素數據放置的控件id,例如TextView

 

例如:生成一條組數據,組名爲“group1”

Map<String, String> map1 = new HashMap<>();

map1.put("group", "group1");//其中"group"只是一個索引key

 

集合保存所有的組數據

List<Map<String, String>> groups = new ArrayList<>();

 

保存一條子元素數據,和組數據一樣

Map<String, String> cMap1 = new HashMap<>();

cMap1.put("child","child1_data1");

child1.add(cMap1);//添加到集合

 

保存一個組的子元素數據

List<Map<String, String>> child1 = new ArrayList<>();

 

保存所有組的子元素數據

List<List<Map<String, String>>> childs = new ArrayList<>();

 

構造方法

SimpleExpandableListAdapter adapter1 = new SimpleExpandableListAdapter(this,

groups, R.layout.expandable_group, new String[]{"group"}, new int[]{R.id.text_group},

childs, R.layout.expandable_child, new String[]{"child"}, new int[]{R.id.text_child});

 

BaseExpandableListAdapter

繼承BaseExpandableListAdapter實現自己的Adapter:

public class MyAdapter extends BaseExpandableListAdapter{

    String[] groups = new String[]{"Group_111","Group_222","Group_333"};
    String[][] childs = new String[][]{
            {"g1_c1","g1_c2","g1_c3"},
            {"g2_c1","g2_c2","g2_c3"},
            {"g3_c1","g3_c2","g3_c3"}};

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;//這裏直接返回組元素下標
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;//這裏直接返回第二層元素下標
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = View.inflate(MainActivity.this, R.layout.expandable_group, null);
        TextView text = (TextView)view.findViewById(R.id.text_group);
        text.setText(groups[groupPosition]);
        return view;//實際項目中應該考慮convertView的重用
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = View.inflate(MainActivity.this, R.layout.expandable_child, null);
        TextView text = (TextView)view.findViewById(R.id.text_child);
        text.setText(childs[groupPosition][childPosition]);
        return view;
    }

    @Override//組合子元素是否擁有穩定的id,底層的數據集改表是否影響該id
    public boolean hasStableIds() {
        return false;
    }

    @Override//子元素是否可選中,有點擊反饋
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}


ExpandableListActivity和ListActivity類似

  • 寫自己的Activity繼承自該類

  • 該類自帶一個ExpandableListView。id爲:“@android:id/list”(或者代碼中爲“list”)

  • 可以有一個id爲:“@android:id/empty”佈局,作爲adapter沒數據時的替代佈局

  • 通常寫自己的佈局並滿足上述條件,並加以擴展實現自己的需求

 

SimpleCursorTreeAdapter

   不同於SimpleExpandableListAdapter數據源來自List<Map<?,?>>,SimpleCursorTreeAdapter數據源來自訪問數據庫返回的Cursor。

 


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