亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android ExpandableListView展開(kāi)列表控件使用實(shí)例

 更新時(shí)間:2014年07月10日 09:14:38   投稿:junjie  
這篇文章主要介紹了Android ExpandableListView展開(kāi)列表控件使用實(shí)例,本文實(shí)現(xiàn)了一個(gè)類(lèi)似手機(jī)QQ好友列表的界面效果,需要的朋友可以參考下

你是否覺(jué)得手機(jī)QQ上的好友列表那個(gè)控件非常棒? 不是..... 那也沒(méi)關(guān)系,學(xué)多一點(diǎn)知識(shí)對(duì)自己也有益無(wú)害。

那么我們就開(kāi)始吧。

展開(kāi)型列表控件, 原名ExpandableListView
是普通的列表控件進(jìn)階版, 可以自由的把列表進(jìn)行收縮, 非常的方便兼好看。
首先看看我完成的截圖, 雖然界面不漂亮, 但大家可以自己去修改界面。

該控件需要一個(gè)主界面XML 一個(gè)標(biāo)題界面XML及一個(gè)列表內(nèi)容界面XML
首先我們來(lái)看看 mian.xml 主界面

復(fù)制代碼 代碼如下:
//該界面非常簡(jiǎn)單, 只要一個(gè)ExpandableListView即可
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

groups.xml 該界面是父標(biāo)題界面
我們只要放上一個(gè)要顯示出來(lái)的標(biāo)題TextView控件上去即可

復(fù)制代碼 代碼如下:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <TextView
        android:id="@+id/textGroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="40px"
        android:paddingTop="6px"
        android:paddingBottom="6px"
        android:textSize="15sp"
        android:text="No data"
    />

</LinearLayout>

childs.xml 是子控件, 直接顯示列表內(nèi)容

復(fù)制代碼 代碼如下:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <TextView 
        android:id="@+id/textChild"
   android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="60px"
        android:paddingTop="10px"
        android:paddingBottom="10px"
        android:textSize="20sp"
   android:text="No Data"
/>

</LinearLayout>


接下來(lái)再上主代碼, 命名有點(diǎn)亂, 大家真正用于開(kāi)發(fā)時(shí)可不要這樣命名啊.

復(fù)制代碼 代碼如下:
public class ExpandActivity extends ExpandableListActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //創(chuàng)建二個(gè)一級(jí)條目標(biāo)題
        Map<String, String> title_1 = new HashMap<String, String>();
        Map<String, String> title_2 = new HashMap<String, String>();
       
        title_1.put("group", "開(kāi)發(fā)");
        title_2.put("group", "管理");
       
        //創(chuàng)建一級(jí)條目容器
        List<Map<String, String>> gruops = new ArrayList<Map<String,String>>();
       
        gruops.add(title_1);
        gruops.add(title_2);
       
        //創(chuàng)建二級(jí)條目?jī)?nèi)容
       
        //內(nèi)容一
        Map<String, String> content_1 = new HashMap<String, String>();
        Map<String, String> content_2 = new HashMap<String, String>();
       
        content_1.put("child", "VC++");
        content_2.put("child", "Java");
       
        List<Map<String, String>> childs_1 = new ArrayList<Map<String,String>>();
        childs_1.add(content_1);
        childs_1.add(content_2);
       
        //內(nèi)容二
        Map<String, String> content_3 = new HashMap<String, String>();
        Map<String, String> content_4 = new HashMap<String, String>();
       
        content_3.put("child", "敏捷開(kāi)發(fā)");
        content_4.put("child", "迭代開(kāi)發(fā)");
       
        List<Map<String, String>> childs_2 = new ArrayList<Map<String,String>>();
        childs_2.add(content_3);
        childs_2.add(content_4);
       
        //存放兩個(gè)內(nèi)容, 以便顯示在列表中
        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
        childs.add(childs_1);
        childs.add(childs_2);
       
        //創(chuàng)建ExpandableList的Adapter容器
        //參數(shù): 1.上下文    2.一級(jí)集合 3.一級(jí)樣式文件 4. 一級(jí)條目鍵值  5.一級(jí)顯示控件名
        //   6. 二級(jí)集合 7. 二級(jí)樣式 8.二級(jí)條目鍵值 9.二級(jí)顯示控件名
        SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
                this, gruops, R.drawable.groups, new String[]{"group"}, new int[]{R.id.textGroup},
                childs, R.drawable.childs, new String[]{"child"}, new int[]{R.id.textChild}
                );
       
        //加入列表
        setListAdapter(sela);
    }
}
//最后, 如果想響應(yīng)各操作的話, 就要重載下面的方法
//列表內(nèi)容按下
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
    // TODO Auto-generated method stub
    return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

//二級(jí)標(biāo)題按下
@Override
public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup)
{
    // TODO Auto-generated method stub
    return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
}

//一級(jí)標(biāo)題按下
@Override
public void setSelectedGroup(int groupPosition)
{
    // TODO Auto-generated method stub
    super.setSelectedGroup(groupPosition);
}

再最后, 運(yùn)行你的模擬器就可以看見(jiàn)啦。
將上面的ExpandableListView控件化.
控件化比較簡(jiǎn)單我們只要用普通的Activity類(lèi)就可以了, 不用再繼承ExpandableListView.
只需要在成員變量中添加
private ExpandableListView expandList;
然后在添加內(nèi)容時(shí)改成
expandList.setAdapter(sela);
就可以了。 當(dāng)然, 響應(yīng)事件Listener也可以自己添加。

附:另一個(gè)例子

ExpandableListView的用法與ListView和GridView,Gallery 類(lèi)似,都是通過(guò)一個(gè)Adapter來(lái)顯示.
main.xml:

復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ExpandableListView android:id="@+id/elv" android:indicatorRight="160dp"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    </ExpandableListView>
    <!-- indicatorRight 設(shè)置那個(gè)小圖標(biāo)右邊緣與 ExpandableListView左邊緣的間距-->
</LinearLayout>

ElvAdapter.java
復(fù)制代碼 代碼如下:
package com.test;
 
import java.util.ArrayList;
 
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
 
public class ElvAdapter extends BaseExpandableListAdapter
{
 
    ArrayList<ElvObject> objs;
    Context context;
    ElvAdapter(Context context,ArrayList<ElvObject> objs)
    {
        this.objs=objs;
        this.context=context;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return objs.get(groupPosition).childs.get(childPosition);
    }
 
    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return childPosition;
    }
 
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        //子元素的View
        TextView tv=new TextView(context);
        tv.setText(objs.get(groupPosition).childs.get(childPosition));
        tv.setLayoutParams(new ExpandableListView.LayoutParams(ExpandableListView.LayoutParams.FILL_PARENT,ExpandableListView.LayoutParams.WRAP_CONTENT));
        return tv;
    }
 
    @Override
    public int getChildrenCount(int groupPosition)
    {
        // TODO Auto-generated method stub
        return objs.get(groupPosition).childs.size();
    }
 
    @Override
    public Object getGroup(int groupPosition)
    {
        // TODO Auto-generated method stub
        return objs.get(groupPosition);
    }
 
    @Override
    public int getGroupCount()
    {
        // TODO Auto-generated method stub
        return objs.size();
    }
 
    @Override
    public long getGroupId(int groupPosition)
    {
        // TODO Auto-generated method stub
        return groupPosition;
    }
 
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        //分組的View
        TextView tv=new TextView(context);
        tv.setText(objs.get(groupPosition).groupName);
        ExpandableListView.LayoutParams params=new ExpandableListView.LayoutParams(ExpandableListView.LayoutParams.FILL_PARENT,60);
        tv.setLayoutParams(params);
        return tv;
    }
 
    @Override
    public boolean hasStableIds()
    {
        // TODO Auto-generated method stub
        return false;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return true;
    }
 
}

class ElvObject{
    String groupName="";
    ArrayList<String> childs=new ArrayList<String>();
    ElvObject(String groupName,ArrayList<String> childs)
    {
        this.groupName=groupName;
        this.childs=childs;
    }
}


注意下面還有一個(gè)ElvObject類(lèi)
主程序AndroidTestActivity.java
復(fù)制代碼 代碼如下:
package com.test;
 
import java.util.ArrayList;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
 
public class AndroidTestActivity extends Activity
{
    /** Called when the activity is first created. */
    ExpandableListView elv;
    ElvAdapter adapter;
    ArrayList<ElvObject> objs=new ArrayList<ElvObject>();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        elv=(ExpandableListView)findViewById(R.id.elv);
        adapter=new ElvAdapter(this,objs);
        elv.setAdapter(adapter);
        ArrayList<String> list=new ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        objs.add(new ElvObject("英文",list));
        ArrayList<String> list2=new ArrayList<String>();
        list2.add("111");
        list2.add("222");
        list2.add("333");
        list2.add("444");
        objs.add(new ElvObject("數(shù)字",list2));
 
    }
}

相關(guān)文章

最新評(píng)論