項(xiàng)目需要展示一個(gè)通訊簿,通訊簿中的手機(jī)號(hào)碼是分組的,要求勾選組時(shí),自動(dòng)勾選組下的手機(jī)號(hào)碼,實(shí)現(xiàn)效果如下:
<!--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;
}
}
< 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 ;
}
}
更多文章、技術(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ì)您有幫助就好】元
