Android 开发:(十)初识ExpandableListView(可扩展的下拉列表组件)

版权声明:转载请注明出处:http://blog.csdn.net/kevindongkun https://blog.csdn.net/Kevindongkun/article/details/68485265

随便扯点儿

前几天做iOS仿QQ,其中好友列表页面就有下拉列表的功能,iOS做法应该比安卓稍微复杂一点,其中布局以及一些实现方法(协议方法)都类似,不一样的应该是动画切换效果,安卓提供现成的组件,用原生的就可以实现。

iOS示例

http://blog.csdn.net/kevindongkun/article/details/60765551

Android示例

  1. 效果图:

  2. 示例代码:
    .xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lzc.gsww.androidstudy.ExpandableListviewActivity">

    <ExpandableListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/expand"></ExpandableListView>
</LinearLayout>

.Activity.java文件:

package com.lzc.gsww.androidstudy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ExpandableListviewActivity extends AppCompatActivity {

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

        ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

            private int[]      logos = new int[]{
                    R.mipmap.caocao, R.mipmap.liubei, R.mipmap.sunquan
            };
            private String[]   armTypes = new String[]{
                    "曹操", "刘备", "孙权"
            } ;
            private String[][] arms = new String[][]{
                    {"于禁","许褚","张颌","张辽", "乐进", "徐晃", "典韦"},
                    {"关羽","张飞","赵子龙","黄忠", "马超", "魏延", "姜维"},
                    {"周瑜","太史慈","甘宁","黄盖", "程普", "吕蒙", "陆逊"}
            };



            @Override

            //section个数
            public int getGroupCount() {
                return armTypes.length;
            }
            //section数据源
            @Override
            public Object getGroup(int groupPosition) {
                return armTypes[groupPosition];
            }

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

            //section布局
            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                LinearLayout ll = new LinearLayout(ExpandableListviewActivity.this);
                ll.setOrientation(0);

                ImageView logo = new ImageView(ExpandableListviewActivity.this);
                logo.setLayoutParams(new LinearLayoutCompat.LayoutParams(200,200));
                logo.setImageResource(logos[groupPosition]);
                ll.addView(logo);

                TextView textView = new TextView(ExpandableListviewActivity.this);
                textView.setLayoutParams(new ViewGroup.LayoutParams(800, 200));
                textView.setGravity(Gravity.CENTER | Gravity.LEFT);
                textView.setText(getGroup(groupPosition).toString());
                textView.setPadding(60,0,0,0);
                textView.setTextSize(20);
                ll.addView(textView);
                return ll;
            }




            //row个数
            @Override
            public int getChildrenCount(int groupPosition) {
                return arms[groupPosition].length;
            }

            //row数据源
            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return arms[groupPosition][childPosition];
            }

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

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

                TextView textView = new TextView(ExpandableListviewActivity.this);
                textView.setLayoutParams(new ViewGroup.LayoutParams(1000, 100));
                textView.setGravity(Gravity.CENTER | Gravity.LEFT);
                textView.setText(getChild(groupPosition, childPosition).toString());
                textView.setPadding(200,0,0,0);
                textView.setTextSize(14);
                return textView;
            }


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

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

        ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expand);
        expandableListView.setAdapter(adapter);
    }
}

猜你喜欢

转载自blog.csdn.net/Kevindongkun/article/details/68485265