安卓 三级菜单 三级树目录

三级树目录:

下载地址:https://download.csdn.net/download/hdhhd/12527325

先上图:

安卓 三级菜单 三级树目录_第1张图片

安卓 三级菜单 三级树目录_第2张图片

下载打开即可直接使用:只需要在gson.fromJson("  ",Get_Personne.class);这里添加自己的json数据即可,然后传给adapter


        //数据可以自己添加
        get_personne=gson.fromJson("{}",Get_Personne.class);

     
        ExpandableListviewAdapter adapter = new ExpandableListviewAdapter(MainActivity.this, get_personne);

实现:

activity_main:




    
    

    
        
        
        
        
        
        
        

MainActivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.google.gson.Gson;

import youli.com.example.administrator.threelevelmenu.adapter.ExpandableListviewAdapter;
import youli.com.example.administrator.threelevelmenu.info.Get_Personne;

public class MainActivity extends AppCompatActivity  implements ExpandableListviewAdapter.OnRvItemClickListener{

    private ExpandableListView expand_list_id;

    private Get_Personne get_personne;

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

    private void initview() {

        Gson gson=new Gson();

        //这里是gson解析,数据可以自己添加
        get_personne=gson.fromJson("{}",Get_Personne.class);



     ExpandableListviewAdapter adapter;
        expand_list_id = findViewById(R.id.expand_list_id);
        adapter = new ExpandableListviewAdapter(MainActivity.this,get_personne);
        adapter.setOnRvItemClickListener(this);  //点击事件
        expand_list_id.setAdapter(adapter);

        //默认展开第一个数组
//        expand_list_id.expandGroup(0);
        //关闭数组某个数组,可以通过该属性来实现全部展开和只展开一个列表功能
        //expand_list_id.collapseGroup(0);
        expand_list_id.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {

//                Toast.makeText(getActivity(),get_personne1.getSubName().get(groupPosition).getName(),Toast.LENGTH_SHORT).show();
//                window.dismiss();
                return false;
            }
        });
        //子视图的点击事件
        expand_list_id.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
                Toast.makeText(MainActivity.this, get_personne.getSubName().get(groupPosition).getSubName().get(childPosition).getSubName().get(childPosition).getName(), Toast.LENGTH_SHORT).show();

                return true;
            }
        });

        expand_list_id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
       //   Toast.makeText(getActivity(), get_personne1.getSubName().get(position).getSubName().get(position).getSubName().get(position).getName(), Toast.LENGTH_SHORT).show();

            }
        });


        //用于当组项折叠时的通知。
        expand_list_id.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(MainActivity.this,"折叠了数据"+get_personne.getSubName().get(groupPosition).getName(),Toast.LENGTH_LONG).show();

            }
        });

        //用于当组项折叠时的通知。
        expand_list_id.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(MainActivity.this,"展开了数据"+get_personne.getSubName().get(groupPosition).getName(),Toast.LENGTH_LONG).show();
            }
        });
    }


  //二三层点击事件
    @Override
    public void OnItemClick(int position, int position1) {
        String values = get_personne.getSubName().get(position).getSubName().get(position1).getName();
        Toast.makeText(MainActivity.this,values+"",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void OnOptionClick(int position, int position1, int position2) {
        String values = get_personne.getSubName().get(position).getSubName().get(position1).getSubName().get(position2).getName();
        Toast.makeText(MainActivity.this,values+"",Toast.LENGTH_SHORT).show();

    }

}

CustomExpandableListView:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;

/**
 * author:Created by LiangSJ
 * date: 2020/6/15.
 * description:解决,expandablelistview嵌套冲突问题
 */

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

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

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

    public CustomExpandableListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 解决显示不全的问题
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2
                , MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
ExpandableListviewAdapter:
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import youli.com.example.administrator.threelevelmenu.R;
import youli.com.example.administrator.threelevelmenu.info.Get_Personne;

/**
 * Created by Administrator on 2020/6/15.
 */

public class ExpandableListviewAdapter extends BaseExpandableListAdapter {

    private Context context;
    private Get_Personne get_personne;

    public ExpandableListviewAdapter(Context context, Get_Personne get_personne) {
        this.context = context;
        this.get_personne = get_personne;
    }


//设置二三层点击事件接口
 private OnRvItemClickListener onRvItemClickListener;

    public interface OnRvItemClickListener {
        void OnItemClick(int position,int position1);

        void OnOptionClick(int position,int position1,int position2);
    }

    public void setOnRvItemClickListener(OnRvItemClickListener onRvItemClickListener) {
        this.onRvItemClickListener = onRvItemClickListener;
    }


    //获取第一层组计数
    @Override
    public int getGroupCount() {
        return get_personne.getSubName().size();
    }

    //获取第二层子计数
    @Override
    public int getChildrenCount(int groupPosition) {
//        重点这里改成1才显示三层结构
        return 1;  //get_personne.getSubName().get(groupPosition).getSubName().size();
    }

    //获取组
    @Override
    public Object getGroup(int groupPosition) {
        return get_personne.getSubName().get(groupPosition);
    }

    //获取子目录
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return get_personne.getSubName().get(groupPosition).getSubName().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态,true=展开
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     */
    @SuppressLint("WrongViewCast")
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        GroupViewHolder groupViewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.sanjicaidan_one_layout, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.parent_textview_id = convertView.findViewById(R.id.parent_textview_id);
            groupViewHolder.parent_image = convertView.findViewById(R.id.parent_image);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        groupViewHolder.parent_textview_id.setText(get_personne.getSubName().get(groupPosition).getName());

//        如果是展开状态
        if (isExpanded) {
            groupViewHolder.parent_image.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.pwpjt2));
        } else {
            groupViewHolder.parent_image.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.pwpjt1));
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        //如果要显示二级树目录直接把线面注释、然后添加需要展示的数据

        CustomExpandableListView customExpandableListView = (CustomExpandableListView) convertView;

        if (convertView == null) {
            customExpandableListView = new CustomExpandableListView(context);
        }
        customExpandableListView.setGroupIndicator(null); //去掉默认箭头

        Get_Personne.SubNameBeanXX url = get_personne.getSubName().get(groupPosition);

        SecondAdapter secondAdapter = new SecondAdapter(context, url.getSubName()); //Adapter

        customExpandableListView.setAdapter(secondAdapter);  //显示二层、三层

   //点击事件
        customExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int childPosition, long id) {

                if (onRvItemClickListener != null) {
                    onRvItemClickListener.OnItemClick(groupPosition,childPosition);
                }

                return false;
            }
        });


    customExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                if (onRvItemClickListener != null) {
                    onRvItemClickListener.OnOptionClick(groupPosition,groupPosition,childPosition);
                }

                return false;
            }
        });

        return customExpandableListView;
    }

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


    class SecondAdapter extends BaseExpandableListAdapter {

        //第二三层数据
        private List subxxList = new ArrayList<>();

        private Context context;

        public SecondAdapter(Context context, List subxxList) {
            this.subxxList = subxxList;
            this.context = context;
        }

        @Override
        public int getGroupCount() {

            return subxxList.size();

        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return subxxList.get(groupPosition).getSubName().size();
        }

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

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return subxxList.get(groupPosition).getSubName().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) {

            GroupViewHolder2 groupViewHolder2 = null;
            if (convertView == null) {
                groupViewHolder2 = new GroupViewHolder2();
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.snajicaidan_two_layout, parent, false);
                groupViewHolder2.chidren_item = convertView.findViewById(R.id.chidren_item);
                groupViewHolder2.parent_image2 = convertView.findViewById(R.id.parent_image2);
                convertView.setTag(groupViewHolder2);
            } else {
                groupViewHolder2 = (GroupViewHolder2) convertView.getTag();
            }

            if (!TextUtils.equals(subxxList.get(groupPosition).getName(), "当前就业")) {
                //如果是展开状态
                if (isExpanded) {
                    groupViewHolder2.parent_image2.setImageDrawable(ContextCompat.getDrawable(context,R.color.yr));
                } else {
                    groupViewHolder2.parent_image2.setImageDrawable(ContextCompat.getDrawable(context,R.color.yr));
                }
            }

            if (TextUtils.equals(subxxList.get(groupPosition).getName(), "当前就业")) {
                //如果是展开状态
                if (isExpanded) {
                    groupViewHolder2.parent_image2.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.pwpjt2));
                } else {
                    groupViewHolder2.parent_image2.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.pwpjt1));
                }
            }




            groupViewHolder2.chidren_item.setText(subxxList.get(groupPosition).getName());

            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            GroupViewHolder3 groupViewHolder3;

            if (convertView == null) {

                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.snajicaidan_three_layout, parent, false);
                groupViewHolder3 = new GroupViewHolder3();
                groupViewHolder3.chidren_item_iten = convertView.findViewById(R.id.chidren_item_iten);
                convertView.setTag(groupViewHolder3);

            } else {
                groupViewHolder3 = (GroupViewHolder3) convertView.getTag();
            }

            groupViewHolder3.chidren_item_iten.setText(subxxList.get(groupPosition).getSubName().get(childPosition).getName());

            return convertView;
        }

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


    static class GroupViewHolder {
        TextView parent_textview_id;
        ImageView parent_image;
    }

    static class GroupViewHolder2 {
        TextView chidren_item;
        ImageView parent_image2;
    }


    static class GroupViewHolder3 {
        TextView chidren_item_iten;
    }


}

重点是这个Adapter里面,点击事件是一个接口的回调 ,都有注释

 

数据使用gson解析,所以依赖也附上:

    //gson解析
    compile 'com.google.code.gson:gson:2.7'

 

你可能感兴趣的:(安卓进阶,android入门教学,android基础)