Android 開發:(九)ExpandableListView篇

隨便扯點兒

前幾天做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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章