超级吊的ExpandableListView三级目录

MainActivity:

public class MainActivityTest extends Activity {
    private ExpandableListView expandableListView;
    private List<String>grounpList=new ArrayList<>();
    private ParentExpandAdapter parentExpandAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_test);
        initdata();
        initview();
    }
    private void initdata() {
        for (int i=0;i<10;i++){
            grounpList.add("这是菜单"+(1+i));
        }
    }
    private void initview() {
        expandableListView=(ExpandableListView)findViewById(R.id.List);
        parentExpandAdapter=new ParentExpandAdapter(MainActivityTest.this,grounpList);
        expandableListView.setAdapter(parentExpandAdapter);
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                for(int i=0;i<parentExpandAdapter.getGroupCount();i++){
                    if(i != groupPosition){
                        expandableListView.collapseGroup(i);
                    }
                }
            }
        });
    }

}

MainActivity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.fae.mobile.activity.MainActivityTest">
<ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/List"
    android:groupIndicator="@null"
    android:scrollbars="none"></ExpandableListView>
</RelativeLayout>

ParentExpandAdapter:

public class ParentExpandAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String>parentList;
    public ParentExpandAdapter(Context context,List<String>parentList){
        this.context=context;
        this.parentList=parentList;
    }
    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return parentList.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) {
        ParentViewHolder parentViewHolder;
        if(convertView==null){
            parentViewHolder=new ParentViewHolder();
            convertView=View.inflate(context, R.layout.grounplayout,null);
            parentViewHolder.textViewtv=(TextView)convertView.findViewById(R.id.parentGroupTV);
            parentViewHolder.imageViewtv=(ImageView)convertView.findViewById(R.id.iv_parent_group_indicator);
            convertView.setTag(parentViewHolder);
        }
        else {
           parentViewHolder=(ParentViewHolder)convertView.getTag();
        }
        parentViewHolder.textViewtv.setText(parentList.get(groupPosition));
        if (isExpanded) {
            parentViewHolder.imageViewtv.setImageResource(R.drawable.a_1);

        } else {
            parentViewHolder.imageViewtv.setImageResource(R.drawable.abc_ic_voice_search_api_mtrl_alpha);

        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
       final ChildViewHolder childViewHolder;
        if(convertView==null){


            childViewHolder=new ChildViewHolder();
            convertView=View.inflate(context,R.layout.item_expand_group_child,null);
            childViewHolder.childExpandLv=(ExpandableListView)convertView.findViewById(R.id.expand_group_item);
            convertView.setTag(childViewHolder);
        }
        else {
            childViewHolder=(ChildViewHolder)convertView.getTag();
        }

        final ChildExpandAdapter childExpandAdapter = new ChildExpandAdapter(context, parentList.subList(0, 10)
        );

        childViewHolder.childExpandLv.setAdapter(childExpandAdapter);

        childViewHolder.childExpandLv.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {

                for (int i = 0; i < childExpandAdapter.getGroupCount(); i++) {
                    if (i != groupPosition) {
                        childViewHolder.childExpandLv.collapseGroup(i);
                    }
                }
            }
        });

        return convertView;
    }

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

    class ParentViewHolder{
        TextView textViewtv;
        ImageView imageViewtv;
    }
    class ChildViewHolder{
        ExpandableListView childExpandLv;
    }
}

grounplayout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:background="#eff0f2"
    android:paddingLeft="10dp">
    <TextView
        android:layout_marginLeft="20dp"
        android:id="@+id/parentGroupTV"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:textColor="#0061b2"
        android:textSize="16sp" />
    <ImageView
        android:id="@+id/iv_parent_group_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/parentGroupTV"
        android:layout_marginLeft="10dp"/>
</RelativeLayout>

item_expand_group_child.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <com.fae.mobile.activity.CustomExpandableListView
        android:id="@+id/expand_group_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:groupIndicator="@null"
        android:scrollbars="none"/>
</RelativeLayout>

ChildExpandAdapter:


public class ChildExpandAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String>parentList;
    public ChildExpandAdapter(Context context,List<String>parentList){
        this.context=context;
        this.parentList=parentList;
    }
    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return parentList.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 false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder parentHolder;
        if (null == convertView) {
            parentHolder = new ParentViewHolder();
            convertView = View.inflate(context, R.layout.fire_tab_child_group_item, null);
            parentHolder.parentTv = (TextView) convertView.findViewById(R.id.childGroupTV);
            parentHolder.parentIv = (ImageView) convertView.findViewById(R.id.iv_fire_tab_child_indicator);
            convertView.setTag(parentHolder);
        } else {
            parentHolder = (ParentViewHolder) convertView.getTag();
        }
        parentHolder.parentTv.setText(parentList.get(groupPosition));
        if (isExpanded) {
            parentHolder.parentIv.setImageResource(R.drawable.a_1);
        } else {
            parentHolder.parentIv.setImageResource(R.drawable.abc_ic_voice_search_api_mtrl_alpha);
        }
        return convertView;
    }
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childHolder;
        if (null == convertView) {
            childHolder = new ChildViewHolder();
            convertView = View.inflate(context, R.layout.item_child_child, null);
            childHolder.gridView = (GridView) convertView.findViewById(R.id.gv_child_child_item);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildViewHolder) convertView.getTag();
        }
        childHolder.gridView.setAdapter(new GridViewAdapter(context, getList()));
        childHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(context, "GridView - " + position, Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class ParentViewHolder {
        TextView parentTv;
        ImageView parentIv;
    }
    class ChildViewHolder {

        GridView gridView;
    }
    private List<String> getList() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("And " + i);
        }
        return list;
    }
}

fire_tab_child_group_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="30dp">

    <TextView
        android:layout_marginLeft="20dp"
        android:id="@+id/childGroupTV"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:text="数码"
        android:textColor="#262626"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/iv_fire_tab_child_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:src="@drawable/abc_list_selector_disabled_holo_dark" />
</RelativeLayout>

item_addtab_grid.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/rl_addtab_grid_item"
    android:layout_width="100dp"
    android:layout_height="35dp">

    <TextView
        android:id="@+id/tv_add_item_tab_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="热点"
        android:textColor="#40ff00"
        android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>

item_child_child.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <com.fae.mobile.activity.CustomGridView
        android:id="@+id/gv_child_child_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:horizontalSpacing="15dp"
        android:numColumns="4"
        android:scrollbars="none"
        android:verticalSpacing="10dp"/>


</LinearLayout>

GridViewAdapter:

public class GridViewAdapter extends BaseAdapter {

        private Context context;
        private List<String> list;

        public GridViewAdapter(Context context, List<String> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (null == view) {
                holder = new ViewHolder();
                view = View.inflate(context, R.layout.item_addtab_grid, null);
                holder.textView = (TextView) view.findViewById(R.id.tv_add_item_tab_name);


                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.textView.setText(list.get(i));
            return view;
        }

        class ViewHolder {

            TextView textView;
        }
    }

自定义ExpandableListView:

public class CustomExpandableListView extends ExpandableListView {
    public CustomExpandableListView(Context context) {
        super(context);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2
                , MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

自定义GridView:

public class CustomGridView extends GridView{
        public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public CustomGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public CustomGridView(Context context) {
            super(context);
        }


        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
        }
}

这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个 ExpandableListView 二级分栏的例子: 首先在布局文件中定义 ExpandableListView 和 Adapter: ```xml <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- 定义自定义的 ExpandableListAdapter --> ``` 然后在 Activity 中设置 Adapter 并为 ExpandableListView 设置点击事件: ```java public class MainActivity extends AppCompatActivity { private ExpandableListView expandableListView; private ExpandableListAdapter adapter; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 准备数据 prepareListData(); // 设置 Adapter adapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); expandableListView.setAdapter(adapter); // 设置点击事件 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " : " + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return false; } }); } // 准备数据 private void prepareListData() { listDataHeader = new ArrayList<>(); listDataChild = new HashMap<>(); // 添加一级分栏 listDataHeader.add("Fruits"); listDataHeader.add("Vegetables"); // 添加二级分栏 List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); List<String> vegetables = new ArrayList<>(); vegetables.add("Carrot"); vegetables.add("Tomato"); vegetables.add("Potato"); // 将二级分栏添加到对应的一级分栏下 listDataChild.put(listDataHeader.get(0), fruits); listDataChild.put(listDataHeader.get(1), vegetables); } } ``` 最后是 ExpandableListAdapter 的实现: ```java public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { this.context = context; this.listDataHeader = listDataHeader; this.listDataChild = listDataChild; } @Override public int getGroupCount() { return this.listDataHeader.size(); } @Override public int getChildrenCount(int groupPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return this.listDataHeader.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return this.listDataChild.get(this.listDataHeader.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 false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_group, null); } TextView lblListHeader = convertView.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item, null); } TextView txtListChild = convertView.findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 其中 `list_group.xml` 和 `list_item.xml` 分别是一级分栏和二级分栏的布局文件,可以根据需要自行定义。 以上就是 ExpandableListView 二级分栏的例子,希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值