ExpandableLIstView,可展开的ListView

ExpandableLIstView,可展开的ListView,第1张

ExpandableLIstView,可展开的ListView

可以实现像qq好友分类一样的效果

 首先当然是需要在主界面的布局里面放一个ExpandableListView,因为我放东西喜欢在可视化界面里直接拖进去然后再去xml里面修改,这里我发现可视化界面里找不到这个组件,没办法只能手写,而且ExpandableListView好像只能放在线性布局里面,需要手动把初始的约束布局改成线性



    

    

接下来再做两个布局,一个用来加载分类栏,也就是外层的布局,一个用来加载内容栏,也就是内层的布局

外层布局




    

    

内层布局




    

    

 

 这里可以根据个人喜好让两个布局位置调一下

接下来就是最重要的逻辑部分,和listview一样,我们也用自制的适配器来实现,

 package com.example.expandblelistview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.baseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

 public class MainActivity extends AppCompatActivity {
    private ExpandableListView listView;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listView);
        listView.setAdapter(new MyExpandble());
         
        listView.setonChildClickListener(new ExpandableListView.onChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Toast.makeText(MainActivity.this,strings[i][i1],Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

     private String[] groups = {"ss","dd","ff","ff"};
     private String[][] strings = {{"ee","rr","tt"},{"gg","kk","ll"},{"77","88","22"},{"agd","asgr","ger"}};

     
     class MyExpandble extends baseExpandableListAdapter {
        @Override
        //外层的长度
        public int getGroupCount() {
            return groups.length;
        }

        @Override
        //内层的长度
        public int getChildrenCount(int i) {
            return strings[i].length;
        }

        @Override
        //外层的内容
        public Object getGroup(int i) {
            return groups[i];
        }

        @Override
        //内层的内容
        public Object getChild(int i, int i1) {
            return strings[i][i1];
        }

        @Override
        //获得外层的id
        public long getGroupId(int i) {
            return i;
        }

        @Override
        //获得内层的id
        public long getChildId(int i, int i1) {
            return i;
        }

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

        @Override
        //加载外层的布局
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            if (view == null){
                view = getLayoutInflater().inflate(R.layout.group,null);
            }
            ImageView icon = view.findViewById(R.id.imageView);
            TextView title = view.findViewById(R.id.textView);

            title.setText(groups[i]);
            return view;
        }

        @Override
        //加载内层的布局
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            if (view == null){
                view = getLayoutInflater().inflate(R.layout.child,null);
            }
            ImageView icon = view.findViewById(R.id.imageView1);
            TextView title = view.findViewById(R.id.textView1);
            title.setText(strings[i][i1]);
            return view;
        }

        @Override
        //内层是否能被选中
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }
    }
}

 最终效果就实现了,

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5717915.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存