下面是實(shí)現(xiàn)步驟。1、新建類PhoneListItem,用于表示分組中的每一個(gè)手機(jī)號(hào)碼。packagecom.ydtf.android;publicclassPhoneListItem{publicStringphone,name;publicbooleanchecked;publicPhoneListItem(St" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Android開發(fā):BaseExpandableListAdapter的使用

系統(tǒng) 1911 0

項(xiàng)目需要展示一個(gè)通訊簿,通訊簿中的手機(jī)號(hào)碼是分組的,要求勾選組時(shí),自動(dòng)勾選組下的手機(jī)號(hào)碼,實(shí)現(xiàn)效果如下:

Android開發(fā):BaseExpandableListAdapter的使用 Android開發(fā):BaseExpandableListAdapter的使用

<!--StartFragment--> 下面是實(shí)現(xiàn)步驟。

1、新建類PhoneListItem,用于表示分組中的每一個(gè)手機(jī)號(hào)碼。

package com.ydtf.android;

public class PhoneListItem {

public String phone , name ;

public boolean checked ;

public PhoneListItem(String _name,String _phone, boolean _checked){

name =_name;

phone =_phone;

checked =_checked;

}

}

2、新建布局文件phone_list_item.xml,用于定義分組下面的每一條手機(jī)記錄的頁面布局。

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

android:layout_width = "fill_parent" android:layout_height = "wrap_content"

android:orientation = "horizontal" android:minHeight = "40px"

android:layout_gravity = "center_vertical" >

< CheckBox android:id = "@+id/phone_check" android:focusable = "false"

android:layout_width = "wrap_content" android:layout_height = "wrap_content"

android:layout_marginLeft = "35px" android:checked = "false" />

< TextView android:id = "@+id/phone_name" android:layout_width = "80dip"

android:layout_height = "wrap_content" android:layout_gravity = "center_vertical" />

< TextView android:id = "@+id/phone_number" android:layout_width = "wrap_content"

android:layout_height = "wrap_content" android:textColor = "?android:attr/textColorPrimary"

android:paddingLeft = "10px" android:layout_gravity = "center_vertical" />

</ LinearLayout >

3、新建類PhoneGroup,用于表示一個(gè)分組。

import java.util.List;

public class PhoneGroup {

public String title ;

private boolean checked ;

public List<PhoneListItem> children ;

public PhoneGroup(String title, boolean checked,List<PhoneListItem> children){

this . title =title;

setChecked(checked);

this . children =children;

}

public boolean getChecked(){

return checked ;

}

public void setChecked ( boolean b){

checked =b;

if ( children != null && children .size()>0){ //若children不為空,循環(huán)設(shè)置children的checked

for (PhoneListItem each : children ){

each. checked = checked ;

}

}

}

}

4、新建Activity布局文件phone_browser.xml,用于定義通訊簿的展現(xiàn)頁面。

< 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/phonelist"

android:layout_width = "fill_parent" android:layout_height = "0dip"

android:layout_weight = "1" />

</ LinearLayout >

5、新建類QxtPhoneSelect,用于呈現(xiàn)通訊簿的Activity頁面。

package com.ydtf.android;

import java.util.ArrayList;

import java.util.List;

import com.ydtf.android.PhoneGroupAdapter.ExpandableListHolder;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.ExpandableListView;

public class QxtSelectPhone extends Activity implements

ExpandableListView.OnGroupClickListener,ExpandableListView.OnChildClickListener{

private List<PhoneGroup> groups ;

private PhoneGroupAdapter exlist_adapter = null ;

private ExpandableListView exlist ;

public void onCreate(Bundle savedInstanceState) {

super .onCreate(savedInstanceState);

//加載layout

setContentView(R.layout. phone_browser );

//取得 listview

exlist = (ExpandableListView) findViewById(R.id. phonelist );

//調(diào)用 init 方法,這個(gè)方法主要是,初始化一些數(shù)據(jù)

init();

//構(gòu)建 expandablelistview 的適配器

exlist_adapter = new PhoneGroupAdapter( this , groups );

exlist .setOnChildClickListener( this );

exlist .setAdapter( exlist_adapter ); //綁定視圖-適配器

}

private void init() {

groups = new ArrayList<PhoneGroup>();

//構(gòu)建List用作group1的子項(xiàng)

List<PhoneListItem> group1_children = new ArrayList<PhoneListItem>();

//往List中添加內(nèi)容

PhoneListItem item = new PhoneListItem( "和文明" , "1308763994" , false );

group1_children.add(item);

item = new PhoneListItem( "黃文明" , "1308763994" , false );

group1_children.add(item);

item = new PhoneListItem( "王文明" , "1308763994" , false );

group1_children.add(item);

//拼裝成PhoneGroup

PhoneGroup phonegroup1= new PhoneGroup( "group1" , false ,group1_children);

//------把前面的代碼復(fù)制一遍,再添加一個(gè)組group2

//構(gòu)建List用作group2的子項(xiàng)

List<PhoneListItem> group2_children = new ArrayList<PhoneListItem>();

//往List中添加內(nèi)容

item = new PhoneListItem( "張文明" , "1589065423" , false );

group2_children.add(item);

item = new PhoneListItem( "李文明" , "1589065423" , false );

group2_children.add(item);

item = new PhoneListItem( "趙文明" , "1589065423" , false );

group2_children.add(item);

//拼裝成PhoneGroup

PhoneGroup phonegroup2= new PhoneGroup( "group2" , false ,group2_children);

//添加進(jìn)groups數(shù)組

groups .add(phonegroup1);

groups .add(phonegroup2);

}

//當(dāng)分組行背點(diǎn)擊時(shí),讓分組呈現(xiàn)“選中/取消選中”狀態(tài)。

@Override

public boolean onChildClick(ExpandableListView parent, View v,

int groupPosition, int childPosition, long id) {

PhoneGroupAdapter.ExpandableListHolder holder=(ExpandableListHolder) v.getTag();

holder. chkChecked .setChecked(!holder. chkChecked .isChecked());

groups .get(groupPosition). children .get( childPosition ). checked =

! groups .get(groupPosition). children .get(childPosition). checked ;

return false ;

}

@Override

public boolean onGroupClick(ExpandableListView parent, View v,

int groupPosition, long id) {

// groups.get(groupPosition).setChecked(!groups.get(groupPosition).getChecked());

// exlist_adapter.notifyDataSetChanged();

//

return false ;

}

}

6、新建類PhoneGroupAdapter,實(shí)現(xiàn)BaseExpandableListAdapter。
package com.ydtf.android;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class PhoneGroupAdapter extends BaseExpandableListAdapter {
class ExpandableListHolder { //定義一個(gè)內(nèi)部類,用于保存listitem的3個(gè)子視圖引用,2個(gè)textview和1個(gè)checkbox
TextView tvName;
TextView tvPhone;
CheckBox chkChecked;
}
private Context context; //父activity
private LayoutInflater mChildInflater; //用于加載listitem的布局xml
private LayoutInflater mGroupInflater; //用于加載group的布局xml
private List<PhoneGroup> groups; //所有g(shù)roup
//構(gòu)造方法:參數(shù)c - activity,參數(shù)group - 所有g(shù)roup
public PhoneGroupAdapter(Context c,List<PhoneGroup> groups){
context=c;
mChildInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mGroupInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.groups = groups;
}
@Override
public Object getChild(int arg0, int arg1) {//根據(jù)組索引和item索引,取得listitem // TODO Auto-generated method stub
return groups.get(arg0).children.get(arg1);
}
@Override
public long getChildId(int arg0, int arg1) {//返回item索引
return arg1;
}
@Override
public int getChildrenCount(int groupPosition) {//根據(jù)組索引返回分組的子item數(shù)
return groups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {//根據(jù)組索引返回組
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {//返回分組數(shù)
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {//返回分組索引
return groupPosition;
}
@Override
public View getGroupView(int position, boolean isExpanded,
View view, ViewGroup parent) {//根據(jù)組索引渲染"組視圖"
ExpandableListHolder holder = null; //清空臨時(shí)變量holder
if (view == null) { //判斷view(即view是否已構(gòu)建好)是否為空
//若組視圖為空,構(gòu)建組視圖。注意flate的使用,R.layout.browser_expandable_list_item代表了
//已加載到內(nèi)存的browser_expandable_list_item.xml文件
view = mGroupInflater.inflate(
R.layout.phone_list_item, null);
//下面主要是取得組的各子視圖,設(shè)置子視圖的屬性。用tag來保存各子視圖的引用
holder = new ExpandableListHolder();
//從view中取得textView
holder.tvName = (TextView) view.findViewById(R.id.phone_name);
//從view中取得textview
holder.tvPhone = (TextView) view.findViewById(R.id.phone_number);
//從view中取得checkbox
holder.chkChecked = (CheckBox) view
.findViewById(R.id.phone_check);
// holder.chkChecked.setEnabled(false);//禁用checkbox
//把checkbox、textview的引用保存到組視圖的tag屬性中
view.setTag(holder);
} else { //若view不為空,直接從view的tag屬性中獲得各子視圖的引用
holder = (ExpandableListHolder) view.getTag();
}
//對(duì)應(yīng)于組索引的組數(shù)據(jù)(模型)
PhoneGroup info = this.groups.get(position);
if (info != null) {
//根據(jù)模型值設(shè)置textview的文本
holder.tvName.setText(info.title);
//根據(jù)模型值設(shè)置checkbox的checked屬性
holder.chkChecked.setChecked(info.getChecked());
holder.chkChecked.setTag(info);
holder.chkChecked.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
PhoneGroup group=(PhoneGroup)v.getTag();
group.setChecked(!group.getChecked());
notifyDataSetChanged();
}
});
}
// TODO Auto-generated method stub
return view;
}
//行渲染方法
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ExpandableListHolder holder = null; //清空臨時(shí)變量
if (convertView == null) { //若行未初始化
//通過flater初始化行視圖
convertView = mChildInflater.inflate(
R.layout.phone_list_item, null);
//并將行視圖的3個(gè)子視圖引用放到tag中
holder = new ExpandableListHolder();
holder.tvName = (TextView) convertView
.findViewById(R.id.phone_name);
holder.tvPhone = (TextView) convertView.findViewById(R.id.phone_number);
holder.chkChecked = (CheckBox) convertView
.findViewById(R.id.phone_check);
// holder.chkChecked.setEnabled(false);
convertView.setTag(holder);
} else { //若行已初始化,直接從tag屬性獲得子視圖的引用
holder = (ExpandableListHolder) convertView.getTag();
}
//獲得行數(shù)據(jù)(模型)
PhoneListItem info = this.groups.get(groupPosition)
.children.get(childPosition);
if (info != null) {
//根據(jù)模型數(shù)據(jù),設(shè)置行視圖的控件值
holder.tvName.setText(info.name);
holder.tvPhone.setText(info.phone);
holder.chkChecked.setChecked(info.checked);
holder.chkChecked.setTag(info);
holder.chkChecked.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
CheckBox check=(CheckBox)v;
PhoneListItem item=(PhoneListItem)v.getTag();
item.checked=!item.checked;
// check.setChecked(!check.isChecked());
}
});
}
return convertView;
}
@Override
public boolean hasStableIds() {//行是否具有唯一id
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {//行是否可選
return true;
}
}


Android開發(fā):BaseExpandableListAdapter的使用


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 欧美第一页草草影院 | 成人黄页在线观看 | 亚洲毛片在线观看 | 日韩欧美视频在线一区二区 | 日韩伦理免费在线观看 | 色屁屁www免费看视频影院 | 奇米88| 色狠狠成人综合色 | 亚洲人和日本人jizz | 永久免费在线播放 | 欧美aaa级片| 亚洲欧洲精品成人久久曰影片 | 午夜爽爽性刺激一区二区视频 | 精品成人免费一区二区在线播放 | 色噜| 王的女人印度剧电视剧免费观看32集 | 国产在线日韩在线 | 一区二区三区免费视频 www | 天天摸日日 | 久久精品蜜芽亚洲国产a | 亚洲熟妇毛茸茸 | 免费播放欧美一级特黄 | 狠狠做深爱婷婷久久一区 | 国产成人高清视频 | 久久综合九色综合97婷婷群聊 | 久久国产热视频 | 91色在线观看 | 亚洲视频在线观看免费 | 久久亚洲精品国产精品黑人 | 日韩99 | 国产免费高清无需播放器 | 视频一区 中文字幕 | 日日夜夜天天人人 | 亚洲综合色视频在线观看 | 亚洲视频在线免费看 | 色接久久 | 日韩高清一区 | 56av国产精品久久久久久久 | 久久久久国产一区二区三区四区 | 欧美www在线观看 | 欧美日韩一区二区在线视频 |