美文网首页
可伸缩的ListView--ExpandListView

可伸缩的ListView--ExpandListView

作者: 亦晴工作室 | 来源:发表于2017-01-09 16:20 被阅读228次

类似于 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;
        }
    }

}

相关文章

网友评论

      本文标题:可伸缩的ListView--ExpandListView

      本文链接:https://www.haomeiwen.com/subject/wqdhbttx.html