日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中ExpandableListView控件基本使用

發布時間:2023/12/13 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中ExpandableListView控件基本使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

???????????? 本文採用一個Demo來展示Android中ExpandableListView控件的使用,如怎樣在組/子ListView中綁定數據源。直接上代碼例如以下:

程序結構圖:

layout文件夾下的 main.xml 文件源代碼例如以下:

<?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"><!-- 我們會自定義listview的顯示方式(在另外一個布局文件中邊)不用默認的方式 假設自定義listview的顯示方式這里這個android:id="@id/android:list" 必須這樣寫 --><!-- android:drawSelectOnTop="false"此屬性用來設置listview上的背景顏色會不會擋住(覆蓋)內容 , 假設這是為false就表示不會覆蓋掉 --> <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawSelectorOnTop="false"/> </LinearLayout>


包 com.andyidea.demo中ContactsActivity.java源代碼例如以下:

package com.andyidea.demo;import java.util.ArrayList; import java.util.List;import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView;public class ContactsActivity extends ExpandableListActivity {List<String> group; //組列表List<List<String>> child; //子列表ContactsInfoAdapter adapter; //數據適配器/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE); //設置為無標題setContentView(R.layout.main);getExpandableListView().setBackgroundResource(R.drawable.default_bg);initializeData();getExpandableListView().setAdapter(new ContactsInfoAdapter());getExpandableListView().setCacheColorHint(0); //設置拖動列表的時候防止出現黑色背景}/*** 初始化組、子列表數據*/private void initializeData(){group = new ArrayList<String>();child = new ArrayList<List<String>>();addInfo("Andy",new String[]{"male","138123***","GuangZhou"});addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});addInfo("Tom",new String[]{"female","138123***","ShangHai"});addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});}/*** 模擬給組、子列表加入數據* @param g-group* @param c-child*/private void addInfo(String g,String[] c){group.add(g);List<String> childitem = new ArrayList<String>();for(int i=0;i<c.length;i++){childitem.add(c[i]);}child.add(childitem);}class ContactsInfoAdapter extends BaseExpandableListAdapter{//-----------------Child----------------//@Overridepublic Object getChild(int groupPosition, int childPosition) {return child.get(groupPosition).get(childPosition);}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic int getChildrenCount(int groupPosition) {return child.get(groupPosition).size();}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {String string = child.get(groupPosition).get(childPosition); return getGenericView(string);}//----------------Group----------------//@Overridepublic Object getGroup(int groupPosition) {return group.get(groupPosition);} @Overridepublic long getGroupId(int groupPosition) {return groupPosition;} @Overridepublic int getGroupCount() {return group.size();} @Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {String string = group.get(groupPosition); return getGenericView(string);}//創建組/子視圖 public TextView getGenericView(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 40);TextView text = new TextView(ContactsActivity.this); text.setLayoutParams(lp); // Center the text vertically text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position text.setPadding(36, 0, 0, 0); text.setText(s); return text; } @Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;} @Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}} }


最后,程序執行后截圖例如以下:

???????

轉載于:https://www.cnblogs.com/bhlsheji/p/4182934.html

總結

以上是生活随笔為你收集整理的Android中ExpandableListView控件基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。