BRVAH多级目录

一.Adapter的使用

和普通的适配器不同,如果你要实现多级目录,Adapter必须继承BaseMultiItemQuickAdapter而不是BaseQuickAdapter,如下图:

MultiItemEntity指不同类型的Bean类

同时对应着不同的布局(Layout)

二.Bean类的设置

多级联动的Bean类必须实现MultiItemEntity接口

如下图:

getItemType()返回的值就是上面在Adapter对应类型的编号

2.1继承规则:

目录按如下格式AbstractExpandableItem<Bean>依次继承它的下级目录,比如一级目录继承二级,二级目录继承三级,最低级的目录不用继承直接实现MultiItemEntity接口即可。

三.数据设置

private ArrayList<MultiItemEntity> generateData() {
    int lv0Count = 9;
    int lv1Count = 3;
    int personCount = 5;

    String[] nameList = {"Bob", "Andy", "Lily", "Brown", "Bruce"};
    Random random = new Random();

    ArrayList<MultiItemEntity> res = new ArrayList<>();
    for (int i = 0; i < lv0Count; i++) {
        Level0Item lv0 = new Level0Item("This is " + i + "th item in Level 0", "subtitle of " + i);
        for (int j = 0; j < lv1Count; j++) {
            Level1Item lv1 = new Level1Item("Level 1 item: " + j, "(no animation)");
            for (int k = 0; k < personCount; k++) {
                lv1.addSubItem(new Person(nameList[k], random.nextInt(40)));
            }
            lv0.addSubItem(lv1);
        }
        res.add(lv0);
    }
    return res;
}

四.父级动态添加子级数据

监听子控件按钮,并根据情况设置数据

 五.删除对应数据

六.在adapter内部实现扩展

package wjq.WidgetDemo; import android.app.ExpandableListActivity; import android.os.Bundle; import android.provider.Contacts.People; import android.util.Log; import android.view.ContextMenu; import android.view.Gravity; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; import android.widget.ExpandableListView.ExpandableListContextMenuInfo; public class ExpandableListDemo extends ExpandableListActivity{ private ExpandableListAdapter adapter; //private MyExpandableListAdapter MyAdapter=new MyExpandableListAdapter(); /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); adapter=new MyExpandableListAdapter(); setListAdapter(adapter); registerForContextMenu(getExpandableListView()); } /* (non-Javadoc) * @see android.app.ExpandableListActivity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo) */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("菜单"); menu.add(0, 0, 0, "Action"); } /* (non-Javadoc) * @see android.app.Activity#onContextItemSelected(android.view.MenuItem) */ @Override public boolean onContextItemSelected(MenuItem item) { boolean flag=false; // TODO Auto-generated method stub ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo(); String title=((TextView)menuInfo.targetView).getText().toString(); int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition); if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) { int groupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition); int childPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition); CharSequence str="您单击了"+title; Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); Log.i("tag", "Run Hereing..."); flag= true; } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { int groupPos = ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition); CharSequence cs="您单击了"+title; Toast.makeText(this, cs, Toast.LENGTH_SHORT).show(); Log.i("tag", "Run Here..."); flag= true; } return flag; } public class MyExpandableListAdapter extends BaseExpandableListAdapter { // Sample data set. children[i] contains the children (String[]) for groups[i]. public String[] groups = { "我的好友", "新疆同学", "亲戚", "同事" }; public String[][] children = { { "胡算林", "张俊峰", "王志军", "二人" }, { "李秀婷", "蔡乔", "别高", "余音" }, { "摊派新", "张爱明" }, { "马超", "司道光" } }; public Object getChild(int groupPosition, int childPosition) { return children[groupPosition][childPosition]; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public int getChildrenCount(int groupPosition) { return children[groupPosition].length; } public TextView getGenericView() { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(ExpandableListDemo.this); textView.setLayoutParams(lp); // Center the text vertically textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position textView.setPadding(36, 0, 0, 0); return textView; } public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } public Object getGroup(int groupPosition) { return groups[groupPosition]; } public int getGroupCount() { return groups.length; } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getGroup(groupPosition).toString()); return textView; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public boolean hasStableIds() { return true; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值