Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android的ExpandableListView-android學習之旅(二十八)

Android的ExpandableListView-android學習之旅(二十八)

編輯:關於Android編程

ExpandableListView簡介

ExpandableListView是ListView的子類,用法和ListView類似,ExpandableListView可以創建幾個類別,每個類別下面又包括幾個條目,實現了二級目錄。

注意

因為他的數據組織是二級目錄,所以數據提供者也是有特殊的。
必須是ExpandableListAdapter的子類。
這裡寫圖片描述
這裡寫圖片描述

ExpandableListView的屬性

這裡寫圖片描述

ExpandableListView的實例

package peng.liu.testview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends Activity {
    private ExpandableListView exList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
            int[] imageIds = new int[]{
                    R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
            };
            String[] listNames = new String[]{
                    hello,java,world
            };
            String[][] names = new String[][]{
                    {hello,java,world,python},
                    {hello,java,world,python},
                    {hello,java,world}
            };
            @Override
            public int getGroupCount() {
                return listNames.length;
            }

            @Override
            public int getChildrenCount(int i) {
                return names[i].length;
            }

            @Override
            public Object getGroup(int i) {
                return listNames[i];
            }

            @Override
            public Object getChild(int i, int i2) {
                return names[i][i2];
            }

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

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

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

            @Override
            public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
                LinearLayout layout = new LinearLayout(MainActivity.this);
                ImageView image = new ImageView(MainActivity.this);
                image.setImageResource(imageIds[i]);
                layout.addView(image);
                TextView text = new TextView(MainActivity.this);
                text.setText(getGroup(i).toString());
                layout.addView(text);
                return layout;
            }
            private TextView getText(){
                AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
                TextView text = new TextView(MainActivity.this);
                text.setLayoutParams(params);
                text.setTextSize(20);
                text.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.LEFT);
                text.setPadding(36,0,0,0);
                return text;
            };
            @Override
            public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
                TextView text = getText();
                text.setText(getChild(i,i2).toString());
                return text;
            }

            @Override
            public boolean isChildSelectable(int i, int i2) {
               return true;
            }
        };
        exList = (ExpandableListView) findViewById(R.id.exList);
        exList.setAdapter(adapter);
    }
}

 


    


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved