Android中ExpandableListView的实现

  这个例子是小弟上网搜了相关资料和文章和看了APIdemo后自己实现的一个例子,用在小项目中。都是从小学习到大的,所以都知道在不会的时候很想找人帮自己一把。希望这个对正在研究Android的兄弟(姐们)们有帮助。
  不知道怎么显示截图,只能放在附件里,sorry!不多说,上菜。
1。首先:定义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"
    >
    <ExpandableListView
    android:id="@+id/ex_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    />   
</LinearLayout>
我的截图中有不相关的控件定义,所以在这省略不写了。自己把握。
2。定义一个Adapter来继承BaseExpandableListView,并实现相关方法。
package com.ldci.second.tools;
import com.ldci.second.mypnone.CallInActivity;


import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;

import android.widget.TextView;


public class CallInExpandableAdapter extends BaseExpandableListAdapter {
CallInActivity GIS;

public CallInExpandableAdapter(CallInActivity _GIS) {
super();
GIS = _GIS;
}
//父菜单内容和子菜单选项内容
private String[] in_groups = {"来电背景", "字体设置","号码设置"};
public String[][] in_children = {
                    {"启用背景图片"},
                    {"字体颜色","字体大小"},
                    {"号码颜色","号码大小"}
                    };

//**************************************
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.v("MyAdapter","pos="+groupPosition);
TextView view = getGenericView();
view.setText(getGroup(groupPosition).toString());
return view;
}

public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 45);

TextView tv = new TextView(GIS);

tv.setTextSize(30);
tv.setLayoutParams(lp);
// Set the text starting position
tv.setPadding(36, 0, 0, 0);

return tv;

}

public long getGroupId(int groupPosition) {
return groupPosition;
}

public Object getGroup(int groupPosition) {
return in_groups[groupPosition];

}

public int getGroupCount() {
return in_groups.length;

}
//子列表属性设置
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView view = getChildTextView(groupPosition, childPosition);
view.setTextSize(24);
return view;
}

public TextView getChildTextView(int gos, int cos) {
TextView view = new TextView(GIS);
view.setText(in_children[gos][cos]);
return view;
}

public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

public Object getChild(int groupPosition, int childPosition) {
return in_children[groupPosition][childPosition];
}

public int getChildrenCount(int groupPosition) {
return in_children[groupPosition].length;
}
//**************************************
public boolean hasStableIds() {
return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}
3。就是自己写一个Activity来显示在界面上,直观的看到效果
package com.ldci.second.mypnone;

import java.util.Timer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;

import com.ldci.second.tools.CallInExpandableAdapter;
import com.ldci.second.tools.PhoneNumService;
import com.ldci.second.tools.ServiceStateMark;
public class CallInActivity extends Activity{
CallInExpandableAdapter adapter;

ExpandableListView expandableList;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.call_in);

adapter = new CallInExpandableAdapter(CallInActivity.this);  
expandableList = (ExpandableListView) findViewById(R.id.ex_list);


expandableList.setAdapter(adapter);

expandableList.setOnChildClickListener(new OnChildClickListener(){
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if(groupPosition==0 && childPosition==0){

}
if(groupPosition== 1&& childPosition==0){
                                         
}
if(groupPosition== 1&& childPosition==1){


}
if(groupPosition== 2&& childPosition==0){

}
if(groupPosition== 2&& childPosition==1){

}

return false;
}
});
}

}
三步下来就会实现ExpandableListView的效果。代码肯定有不足之处,欢迎大家指正,共同进步

你可能感兴趣的:(android,xml,OS)