Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 列表ListView和可擴展列表ExpandableListView的實現

android 列表ListView和可擴展列表ExpandableListView的實現

編輯:關於Android編程

此demo實現了列表ListView和可擴展列表ExpandableListView。

ListViewActivity:

package fk.androiddemo_16;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//1,繼承ListActivity
//2,Activity布局文件中添加ListView控件
//3,為列表創建布局文件
public class MyListActivity extends ListActivity {

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

        List> data = new ArrayList<>();//列表
        Map map1= new HashMap<>();//列表項
        map1.put("name", "liming1.mp3");//項鍵,鍵值(項內容)
        Map map2 = new HashMap<>();
        map2.put("name", "liming2.mp3");
        Map map3 = new HashMap<>();
        map3.put("name", "liming3.mp3");
        data.add(map1);//添加第一項
        data.add(map2);//添加第二項
        data.add(map3);//添加第三項

        //創建適配器(Activity,列表,布局文件
        // 列表項的鍵,列表項在布局中的位置)
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.child,
                new String[] { "name" }, new int[] { R.id.child });
        setListAdapter(adapter);//添加適配器顯示列表

        //設置列表長按的監聽函數來監聽長按操作
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
                if(id==0) Toast.makeText(MyListActivity.this, "Fuck Finger!!", Toast.LENGTH_LONG).show();
                else if(id==1) Toast.makeText(MyListActivity.this, "Give me a try ?!", Toast.LENGTH_LONG).show();
                else if(id==2) Toast.makeText(MyListActivity.this, "OH !! Come on , baby ..", Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }

    @Override
    //短點擊列表item時會調用此函數
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //l為被點擊的列表,然後根據position得到點擊的item,然後強轉HashMap,再取得對應鍵的值
        String ss=((HashMap)l.getItemAtPosition(position)).get("name");//也可以用下面的方法得到Item內容
        //String ss=((TextView)v.findViewById(R.id.child)).getText().toString();
        System.out.println("Position:"+position+" ID:"+id+" Content:"+ss);
    }
}
ExpandableListViewActivity:
package fk.androiddemo_16;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

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

//1,繼承ExpandableListActivity
//2,Activity布局文件中添加ExpandableListView控件
//3,分別為group和child列表創建布局文件
public class MyExpendableListActivity extends ExpandableListActivity {

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

        //存放組列表,每個組都要有一個對應的子列表,否則出錯
        List> groups = new ArrayList<>();
        Map group1 = new HashMap<>();
        group1.put("group", "group1");//鍵,組名
        Map group2 = new HashMap<>();
        group2.put("group", "group2");
        groups.add(group1);//加入第一個組
        groups.add(group2);//加入第二個組


        //一個子列表
        List> child1 = new ArrayList<>();
        Map child1Date1 = new HashMap<>();
        child1Date1.put("child", "child1Date1");
        Map child1Date2 = new HashMap<>();
        child1Date2.put("child", "child1Date2");
        child1.add(child1Date1);//子列表第一項
        child1.add(child1Date2);//子列表第二項

        //一個子列表
        List> child2 = new ArrayList<>();
        Map child2Date1 = new HashMap<>();
        child2Date1.put("child", "child2Date1");
        Map child2Date2 = new HashMap<>();
        child2Date2.put("child", "child2Date2");
        child2.add(child2Date1);//子列表第一項
        child2.add(child2Date2);//子列表第二項

        //存放所有組的子列表
        List>> childs = new ArrayList<>();
        childs.add(child1);//第一個組的子列表
        childs.add(child2);//第二個組的子列表

        //創建適配器(Activity,組列表,組布局文件,組名鍵值對鍵,布局中組名顯示位置
        // 子列表,子列表布局文件,子列表項鍵值對鍵,布局中子列表項內容顯示位置)
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                this, groups, R.layout.group, new String[]{"group"}, new int[]{R.id.group},
                childs, R.layout.child, new String[]{"child"}, new int[]{R.id.child});
        setListAdapter(adapter);//添加適配器,顯示列表

        //設置列表長按的監聽函數來監聽長按操作
        getExpandableListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
                //這裡的position會根據group展開與否而變化,所以沒有意義
                System.out.println("position="+position+" id="+id);
                return true;
            }
        });
    }

    @Override
    //短點擊列表項時調用
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

        System.out.println(((TextView) v.findViewById(R.id.child)).getText());
        System.out.println("groupPosition:"+groupPosition+" childPosition:"+childPosition+" id:"+id);

        return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }
}

ListViewActivity布局:

 

 




    


ExpandableListViewActivity布局:

 

 




    


ListView和ExpandableListView的child條目布局:

 

 




    


ExpandableListView的group條目布局:

 

 




    


運行截圖:

 

\\

 

控制台輸出結果:

10-04 09:56:22.073 2794-2840/fk.androiddemo_16 D/OpenGLRenderer: endAllActiveAnimators on 0x7ffedc87d800 (RippleDrawable) with handle 0x7ffedbe326c0
10-04 09:56:24.273 2794-2794/fk.androiddemo_16 I/System.out: Position:0 ID:0 Content:liming1.mp3
10-04 09:56:25.948 2794-2794/fk.androiddemo_16 I/System.out: Position:1 ID:1 Content:liming2.mp3
10-04 09:56:27.413 2794-2794/fk.androiddemo_16 I/System.out: Position:2 ID:2 Content:liming3.mp3
10-04 09:56:43.984 2794-2840/fk.androiddemo_16 D/OpenGLRenderer: endAllActiveAnimators on 0x7ffedc87dc00 (RippleDrawable) with handle 0x7ffedba5c240
10-04 09:56:50.848 2794-2794/fk.androiddemo_16 I/System.out: child1Date1
10-04 09:56:50.848 2794-2794/fk.androiddemo_16 I/System.out: groupPosition:0 childPosition:0 id:0
10-04 09:56:52.850 2794-2794/fk.androiddemo_16 I/System.out: child1Date2
10-04 09:56:52.850 2794-2794/fk.androiddemo_16 I/System.out: groupPosition:0 childPosition:1 id:1
10-04 09:56:57.696 2794-2794/fk.androiddemo_16 I/System.out: child2Date1
10-04 09:56:57.696 2794-2794/fk.androiddemo_16 I/System.out: groupPosition:1 childPosition:0 id:0
10-04 09:57:00.852 2794-2794/fk.androiddemo_16 I/System.out: child2Date2
10-04 09:57:00.853 2794-2794/fk.androiddemo_16 I/System.out: groupPosition:1 childPosition:1 id:1

 

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