可伸缩的ListView--ExpandListView

类似于 QQ 联系人
1、xml布局,注意:height千万不能使用 wrap_content

<ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/expanfListView">
</ExpandableListView>

2、数据源以及适配器的实现(由于只是初步学习,暂不将适配器封装了)

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private myAdapter myAdapter;
    private List<String> group;
    private List<List<String>> child;

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

        expandableListView = (ExpandableListView) this.findViewById(R.id.expanfListView);
        // 去掉前面箭头
        //expandableListView.setGroupIndicator(null);
        myAdapter = new myAdapter();
        initData();
        expandableListView.setAdapter(myAdapter);

    }

    public void initData(){
        group = new ArrayList<String>();
        child = new ArrayList<List<String>>();
        addInfo("jack", new String[] {"a","aa","aaa"});
        addInfo("rose", new String[] {"b","bb","bbb"});
        addInfo("tom", new String[] {"c","cc","ccc"});
    }

    public void addInfo(String g, String[] c){
        group.add(g);
        List<String> item = new ArrayList<>();
        for(int i = 0; i < c.length; i++){
            item.add(c[i]);
        }
        child.add(item);
    }

    public class myAdapter extends BaseExpandableListAdapter{

        @Override
        public int getGroupCount() {
            return group.size();
        }

        @Override
        public int getChildrenCount(int i) {
            return child.size();
        }

        @Override
        public Object getGroup(int i) {
            return group.get(i);
        }

        @Override
        public Object getChild(int i, int i1) {
            return child.get(i).get(i1);
        }

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

        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

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

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

            TextView textView = null;
            if (view == null){
                textView = new TextView(MainActivity.this);
            }else{
                textView = (TextView) view;
            }
            textView.setText(group.get(i));
            textView.setTextSize(20);
            textView.setPadding(70,0,0,40);
            return textView;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

            TextView textView = null;
            if (view == null){
                textView = new TextView(MainActivity.this);
            }else{
                textView = (TextView) view;
            }
            textView.setText(child.get(i).get(i1));
            textView.setTextSize(20);
            textView.setPadding(70,0,0,40);
            return textView;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,652评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,794评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 7,414评论 0 17
  • 自古最悲帝王家,父子相残,兄弟相杀的惨剧,几千年来,发生的不止一起两起。 太子兵败之后,参与作战和平叛的官员将领都...
    懒虫小狮子阅读 242评论 0 0
  • 春暖花开,万物复苏,小细菌们也猖狂起来,保护好家人,从时刻保护好双手的清洁开始。日本本土狮王儿童洗手液,纯植物配方...
    爱笑的眼睛37阅读 2,439评论 0 50

友情链接更多精彩内容