• android入门——UI(5)


    最近时间实在匆忙,博客的代码基本没有解释。

    介绍ExpandableListView

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ExpandableListView
            android:id="@+id/demo_expandable_list_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
        </ExpandableListView>
    </LinearLayout>
    expandable_list_view_index.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/tv_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    item_group.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:paddingLeft="50dp">
    
        <TextView
            android:id="@+id/tv_child"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    item_child.xml

    我们首先使用SimpleExpandableListAdapte来为其填充数据

    package com.ouc.wkp.ui1;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ExpandableListView;
    import android.widget.SimpleExpandableListAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by wkp on 2016/8/24.
     */
    public class JustLook6 extends Activity {
    
        ExpandableListView expandableListView;
        String[] groupStringArr = {"腾讯", "百度", "阿里"};
        String[][] childStringArrs = {
                {"QQ", "微信", "QQ浏览器"},
                {"百度搜索", "百度地图", "百度外卖"},
                {"淘宝", "支付宝", "天猫商城"}};
        List<Map<String, ?>> groupData=new ArrayList<>();
        List<List<Map<String,?>>> childData=new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.expandable_list_view_index);
    
            expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view);
    
            for (int i = 0; i < groupStringArr.length; i++) {
                Map<String, String> map = new HashMap<>();
                map.put("groupName", groupStringArr[i]);
                groupData.add(map);
    
                List<Map<String,?>> itemList=new ArrayList<>();
                for(int j=0;j<childStringArrs[i].length;j++){
                    Map<String,String> map0=new HashMap<>();
                    map0.put("itemName",childStringArrs[i][j]);
                    itemList.add(map0);
                }
                childData.add(itemList);
            }
    
            SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(this,
                    groupData, R.layout.item_group, new String[]{"groupName"}, new int[]{R.id.tv_group},
                    childData,R.layout.item_child,new String[]{"itemName"},new int[]{R.id.tv_child});
    
            expandableListView.setAdapter(simpleExpandableListAdapter);
        }
    }
    JustLook6.java
    SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(this,
    groupData, R.layout.item_group, new String[]{"groupName"}, new int[]{R.id.tv_group},
    childData,R.layout.item_child,new String[]{"itemName"},new int[]{R.id.tv_child});

    这个函数比较吓人,有9个参数,但是我们可以拆分成1+4+4的形式,其中的后面两组4个参数和SimpleAdapter类似。
    依次为数据源,布局文件,“从哪里来(from)”,“对应到哪里去(to)”


    然后我们可以使用实现BaseExpandableListAdapter类来填充数据
    package com.ouc.wkp.ui1;
    
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.util.TypedValue;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.TextView;
    
    /**
     * Created by wkp on 2016/8/24.
     */
    public class JustLook7 extends Activity {
    
        private ExpandableListView expandableListView;
        private MyDemoBaseExpandableListAdapter adapter;
        String[] groupStringArr = {"腾讯", "百度", "阿里"};
        String[][] childStringArrs = {
                {"QQ", "微信", "QQ浏览器"},
                {"百度搜索", "百度地图", "百度外卖"},
                {"淘宝", "支付宝", "天猫商城"}};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.expandable_list_view_index);
    
            expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view);
    
            adapter = new MyDemoBaseExpandableListAdapter();
    
            expandableListView.setAdapter(adapter);
        }
    
        class MyDemoBaseExpandableListAdapter extends BaseExpandableListAdapter {
    
            @Override
            public int getGroupCount() {
                return groupStringArr.length;
            }
    
            @Override
            public int getChildrenCount(int i) {
                return childStringArrs[i].length;
            }
    
            @Override
            public Object getGroup(int i) {
                return groupStringArr[i];
            }
    
            @Override
            public Object getChild(int i, int i1) {
                return childStringArrs[i][i1];
            }
    
            @Override
            public long getGroupId(int i) {
                return i * 100;
            }
    
            @Override
            public long getChildId(int i, int i1) {
                return i * 100 + i1;
            }
    
            @Override
            public boolean hasStableIds() {
                return false;
            }
    
            @Override
            public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
    
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_group, null);
                TextView textView = (TextView) view.findViewById(R.id.tv_group);
                Object data = getGroup(i);
                textView.setText((String) data);
                if (i % 2 == 1) {
                    textView.setTextColor(Color.RED);
                } else {
                    textView.setTextColor(Color.GREEN);
                }
                return view;
            }
    
            @Override
            public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
                view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_child,null);
                TextView textView=(TextView)view.findViewById(R.id.tv_child);
                Object childData=getChild(i,i1);
                textView.setText((String)childData);
                if(i1>1){
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30);
                }else{
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
                }
                return view;
            }
    
            @Override
            public boolean isChildSelectable(int i, int i1) {
                return false;
            }
        }
    }
    JustLook7.java

    运行效果



  • 相关阅读:
    337粘贴与选择性粘贴
    Excel VBA定制开发如何将数据重复指定次数听语音
    317、单元格的Offset
    Shape.Type属性名称及对应值列表
    Mysql解决The total number of locks exceeds the lock table size错误
    python QT5 tableview 用法1
    python qt5 菜单栏一级菜单响应单击事件
    自定义类加载器
    SpringCloudConfig与Nacos多个领域配置的不同
    多线程工具ForkJoinPool
  • 原文地址:https://www.cnblogs.com/wangkaipeng/p/5804608.html
Copyright © 2020-2023  润新知