安卓開發樹形控制項之ExpandableListView(一),expandablelistview

來源:互聯網
上載者:User

安卓開發樹形控制項之ExpandableListView(一),expandablelistview

  這個例子非常簡單,簡單到一個初學者都能隨便開發出來,今天的目的僅僅只是為了將效果實現出來,如果想深入這裡有幾篇非常不錯的部落格:

 

Android 之ExpandableListView幾個特殊的屬性

http://blog.csdn.net/t12x3456/article/details/7828620

ExpandableListView講解
http://www.apkbus.com/thread-124715-1-1.html

和使用listview一樣,我們這裡需要繼承一個適配器,叫做BaseExpandListAdapter;然後重寫幾個父類的方法

package com.example.uitest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

public class TreeAdapter extends BaseExpandableListAdapter{

Context context;


public TreeAdapter(Context context) {
super();
this.context = context;
}

@Override
public Object getChild(int arg0, int arg1) {
return 0;
}

@Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getChildView(int arg0, int arg1, boolean arg2, View v,
ViewGroup arg4) {
v = LayoutInflater.from(context).inflate(R.layout.item2, null);
return v;
}

@Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return 3;
}

@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int getGroupCount() {
return 3;
}

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

@Override
public View getGroupView(int arg0, boolean arg1, View v, ViewGroup vgs) {
v = LayoutInflater.from(context).inflate(R.layout.item_autocomplete, null);
return v;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}

}

這裡我們只操作了四個函數,分別是:

public int getChildrenCount(int arg0) ;//擷取二級列表的子布局總記錄,也就是行數

public int getGroupCount();//擷取一級列表的行數

public View getChildView(int arg0, int arg1, boolean arg2, View v,
ViewGroup arg4);//擷取二級列表的資源布局

public View getGroupView(int arg0, boolean arg1, View v, ViewGroup vgs);//擷取一級列表的行數

最後將適配器載入至ExpandableListView中:

ExpandableListView view = (ExpandableListView)findViewById(R.id.elv);
TreeAdapter adapter = new TreeAdapter(getApplicationContext());
view.setAdapter(adapter);

主布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ExpandableListView
android:id="@+id/elv"
android:layout_width="fill_parent"

 android:groupIndicator="@null"
android:layout_height="wrap_content"></ExpandableListView>


</LinearLayout>

這是最濃縮版了大家應該很快可以懂;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.