android學習筆記13——ExpandableListView

來源:互聯網
上載者:User

標籤:

ExpandableListView==>可展開的列表組件

==》

ExpandableListView是ListView的子類,對其進行了擴充,其將應用中的清單項目分為幾組,每組中又包含多個清單項目;

ExpandableListView的用法和ListView非常像,只是其所顯示的清單項目應該由ExpandableListAdapter提供;

ExpandableListView支援的額外屬性:

android:childDivider 指定各組內各子清單項目之間的分隔條
android:childIndicator 顯示在子清單項目旁的Drawable對象
android:groupIndicator 顯示在組清單項目旁的Drawable對象

 

 

 

執行個體:

布局檔案==》<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/list"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="#ffffff"        android:cacheColorHint="#00000000"        android:listSelector="#00000000" >    </ExpandableListView></LinearLayout>實現代碼==》package com.example.myexpandablelistview;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity{// 設定組視圖的圖片private int[] logos = new int[]{ R.drawable.one, R.drawable.two, R.drawable.three };// 設定組視圖的顯示文字private String[] generalsTypes = new String[]{ "One", "Two", "Three" };// 子視圖顯示文字private String[][] generals = new String[][]{{ "西瓜", "櫻桃", "草莓" },{ "葡萄", "梨子", "青苹果" },{ "香蕉", "橙子", "芒果" } };// 子視圖圖片public int[][] generallogos = new int[][]{{ R.drawable.one, R.drawable.one, R.drawable.one },{ R.drawable.two, R.drawable.two, R.drawable.two, },{ R.drawable.three, R.drawable.three, R.drawable.three } };@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);final ExpandableListAdapter adapter = new BaseExpandableListAdapter(){// 自己定義一個獲得文字資訊的方法TextView getTextView(){@SuppressWarnings("deprecation")AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);TextView textView = new TextView(MainActivity.this);textView.setLayoutParams(lp);textView.setGravity(Gravity.CENTER_VERTICAL);textView.setPadding(36, 0, 0, 0);textView.setTextSize(20);textView.setTextColor(Color.BLACK);return textView;}// 重寫ExpandableListAdapter中的各個方法@Overridepublic int getGroupCount(){return generalsTypes.length;}@Overridepublic Object getGroup(int groupPosition){return generalsTypes[groupPosition];}@Overridepublic long getGroupId(int groupPosition){return groupPosition;}@Overridepublic int getChildrenCount(int groupPosition){return generals[groupPosition].length;}@Overridepublic Object getChild(int groupPosition, int childPosition){return generals[groupPosition][childPosition];}@Overridepublic long getChildId(int groupPosition, int childPosition){return childPosition;}@Overridepublic boolean hasStableIds(){return true;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent){LinearLayout ll = new LinearLayout(MainActivity.this);ll.setOrientation(0);ImageView logo = new ImageView(MainActivity.this);logo.setImageResource(logos[groupPosition]);logo.setPadding(50, 0, 0, 0);ll.addView(logo);TextView textView = getTextView();textView.setTextColor(Color.BLACK);textView.setText(getGroup(groupPosition).toString());ll.addView(textView);return ll;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent){LinearLayout ll = new LinearLayout(MainActivity.this);ll.setOrientation(0);ImageView generallogo = new ImageView(MainActivity.this);generallogo.setImageResource(generallogos[groupPosition][childPosition]);ll.addView(generallogo);TextView textView = getTextView();textView.setText(getChild(groupPosition, childPosition).toString());ll.addView(textView);return ll;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition){return true;}};ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);expandableListView.setAdapter(adapter);// 設定item點擊的監聽器expandableListView.setOnChildClickListener(new OnChildClickListener(){@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition, long id){Toast.makeText(MainActivity.this,"你點擊了" + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_SHORT).show();return false;}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu){getMenuInflater().inflate(R.menu.main, menu);return true;}}

實現效果:

 

android學習筆記13——ExpandableListView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.