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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/259570.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/259570.shtml
英文地址,請注明出處:http://en.pswp.cn/news/259570.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【暴力】Codeforces Round #398 (Div. 2) A. Snacktower

題意不復述。 用個bool數組記錄一下&#xff0c;如果某一天&#xff0c;當前剩下的最大的出現了的話&#xff0c;就輸出一段。 #include<cstdio> using namespace std; int n; bool vis[100010]; int main() { // freopen("a.in","r",stdin);scanf(&…

javascript的對象創建模式---命名空間模式

javascript中對象的概念是很普遍的&#xff0c;對象是是對象&#xff0c;數組是對象&#xff0c;函數也是對象&#xff0c;字符串其實也是對象。常見的對象創建方法有對象字面量、構造函數創建。我們先來看看對象的創建還有哪些更高級的模式。 一、命名空間模式 二、模塊模式 三…

把Liststring集合,編程string,并以“,”號分割

List<int> roleIdList GetRoleIdList(user.ID); string roleIdsStr ""; if (roleIdList ! null) {roleIdsStr string.Join(",", roleIdList); } 轉載于:https://www.cnblogs.com/lijingran/p/6420364.html

武林外史java游戲,武林外史_網絡游戲專區_騰訊游戲頻道

1 、武師用防御工具1) 武衛類> 用土布制作的防護工具。階段圖片名稱說明1武威帶所需功力值所需力量所需技藝所需體力所需智能所需敏捷性防御力防御成功率8013013021 %武威甲所需功力值所需力量所需技藝所需體力所需智能所需敏捷性防御力防御成功率4160150021 %武威長褲所需功…

簡單實現KeyChain實例

目錄結構如下&#xff1a; AppDelegate.m 1 //2 // AppDelegate.m3 // KeyChain4 //5 // Created by apple on 14-12-26.6 // Copyright (c) 2014年 ll. All rights reserved.7 //8 9 #import "AppDelegate.h" 10 11 interface AppDelegate () 12 13 end 14 1…

Oracle 建立包 和 包體

--創建包create or replace package pac_stuastype cur_stu is ref cursor;procedure getStu(i in number,cur_stu out cur_stu);end pac_stu;--創建包體create or replace package body pac_stuasprocedure getStu(i in number,cur_stu out cur_stu)asnums number(10);begins…

alibaba fastjson

JSON解析器fastjson&#xff08;阿里巴巴出品&#xff0c;版本1.1.33&#xff09; import com.alibaba.fastjson.JSON; public static final Object parse(String text); // 把JSON文本parse為JSONObject或者JSONArraypublic static final JSONObject parseObject(String text)…

matlab 讀取excel一列,讀取excel中的數據把第一列相同的所有行數據輸出成一個excel...

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓284 1113436773300.00 1113436773.30 44.55284 1113436773400.00 1113436773.40 44.55284 1113436773500.00 1113436773.50 44.55284 1113436773600.00 1113436773.60 44.55284 1113436773700.00 1113436773.70 43.77284 111343677…

js function理解

1.function是對象&#xff0c;定義一個function就會在堆中創建一個對象。生成函數如下&#xff1a; (1) var a new Function("document.write(1)"); (2) var a function(){document.write(1);} (3) function a(){ document.write(1); } (4) (function(){ document.…

鍋巴H264播放器地址和說明

鍋巴H264播放器地址和說明 軟件說明: 此工具專門用來播放安防監控行業的H264錄像文件,不管是哪個設備廠家的視頻協議,只要您的錄像文件里有 H264數據,就可以播放. 備注: 因為被一些事情的影響,本來做好了很多的功能, 猶豫很長時間,還是去掉了,這個播放器僅僅是演示我們的解碼器…

poj2431 Expedition

直接代碼、、、 #include<string.h> #include<stdio.h> #include<queue> #include<iostream> #include<algorithm> using namespace std; struct node {int fuel,dist;//bool operator < (const node&d) const{// return dist>d.dist…

JAVA入門[6]-Mybatis簡單示例

初次使用Mybatis,先手寫一個hello world級別的例子&#xff0c;即根據id查詢商品分類詳情。 一、建表 create table Category ( Id INT not null, Name varchar(80) null, constraint pk_category primary key (Id) ); 插入測試數據 INSERT INTO category VALUES (1,Fish); INS…

qpsk調制matlab實現,QPSK調制解調Matlab實現(ing待補充說明)

自寫%QPSKclose all;clc;%思路&#xff1a;1.輸入一組隨機初始信息x[01矩陣]&#xff1b;% 2.按兩兩一組通過for循環判別4種組合&#xff0c;分別對應星座圖4個點% 3.做星座圖% a.過程中考慮過將01序列兩兩分開表示出來&#xff0c;不知是否有必要&#xff0c;未實現% b.考慮兩…

猴子吃桃問題(南陽ACM324)

猴子吃桃問題 時間限制&#xff1a;3000 ms | 內存限制&#xff1a;65535 KB難度&#xff1a;0描述有一堆桃子不知數目&#xff0c;猴子第一天吃掉一半&#xff0c;又多吃了一個&#xff0c;第二天照此方法&#xff0c;吃掉剩下桃子的一半又多一個&#xff0c;天天如此&#…

ASP.NET MVC5 + EF6 入門教程 (6) View中的Razor使用

ASP.NET MVC5 EF6 入門教程 (6) View中的Razor使用 原文:ASP.NET MVC5 EF6 入門教程 (6) View中的Razor使用文章來源&#xff1a; Slark.NET-博客園 http://www.cnblogs.com/slark/p/mvc-5-ef-6-get-started-model.html 上一節&#xff1a;ASP.NET MVC5 EF6 入門教程 (5) M…

matlab中求三維中的多個體積,用matlab計算由下面2個幾何體圍成的體積: x^2+y^2+z^2=36,((x-4)/5)^2+((y-1)/3)^2+((z-2)/5)^2=1...

答&#xff1a;>> triplequad((x,y,z)1*(x.^2y.^2z.^2答&#xff1a;首先建立一個m文件 我取的名字叫 syfs0000 function ysyfs0000(x) y[9*x(1)^236*x(2)^24*x(3)^2-36; x(1)^2-2*x(2)^2-20*x(3); 16*x(1)-x(1)^3-2*x(2)^2-16*x(3)^2;]; end 然后在command window 輸入 …

分析分布式服務框架

出處&#xff1a;http://www.cnblogs.com/zhangs1986/ 技術是為需求而服務的&#xff0c;分布式服務框架也同樣如此&#xff0c;它不是憑空誕生的&#xff0c;也是因為有這樣的需求才會有分布式服務框架這么樣的東西誕生&#xff0c;在這篇blog中來詳細的分析分布式服務框架誕…

PL/SQL注冊碼

code:j6stndb9tk72xfbhbqczcdqnjd8lyj466n number:882851 ps&#xff1a;xs374ca轉載于:https://www.cnblogs.com/myblogslh/p/4203173.html

遞歸--基于回溯和遞歸的八皇后問題解法

八皇后問題是在8*8的棋盤上放置8枚皇后&#xff0c;使得棋盤中每個縱向、橫向、左上至右下斜向、右上至左下斜向均只有一枚皇后。八皇后的一個可行解如圖所示&#xff1a; 思路 對于八皇后的求解可采用回溯算法&#xff0c;從上至下依次在每一行放置皇后&#xff0c;進行搜索&a…

matlab emf 讀取,20140219-Emf_Demo EMF 矢量圖 可以讀取和保存EMF 的封閉類 非常實用 matlab 238萬源代碼下載- www.pudn.com...

文件名稱: 20140219-Emf_Demo下載收藏√ [5 4 3 2 1 ]開發工具: Visual C文件大小: 6312 KB上傳時間: 2014-07-10下載次數: 2詳細說明&#xff1a;EMF 矢量圖 可以讀取和保存EMF矢量圖的封閉類非常實用-EMF EMF vector can read and save the class very useful vector cl…