不管了 先上圖:上面的圖你把它換成箭頭就是類似於qq界面了是不是
android expandablelistview的實現最為關鍵是重寫BaseExpandableListAdapter,而最為關鍵的兩個方法是getChildView(),getGroupView(),注意在寫這兩個方法的時候最重要的是或者最容易出錯的地方是注意返回的內容不要是 null 還有就是用紅線所畫的兩個圖片的切換就要在getGroupView()方法里面進行判斷
if (isExpanded) { iv.setBackgroundResource(R.drawable.btn1); } else { iv.setBackgroundResource(R.drawable.btn2); } ll.addView(iv);
另外就是就是有些小技巧,比如說如何去除隱藏的箭頭?如何設置某一列是鋪開的?
expandableListView.expandGroup(0);//設置第一組張開 expandableListView.setGroupIndicator(null);//除去自帶的箭頭
好進入正題:
main.xml
<?xml version="1.0" encoding="utf-8"?> <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/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:cacheColorHint="#00000000" android:listSelector="#00000000" > </ExpandableListView> </LinearLayout>
ExpandableList.java
package com.eyu.activity_test; import android.app.Activity; import android.graphics.Color; 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.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class ExpandableList extends Activity{ ExpandableAdapter adapter; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); adapter = new ExpandableAdapter(this); ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list); expandableListView.setAdapter(adapter); expandableListView.expandGroup(0);//設置第一組張開 expandableListView.setGroupIndicator(null);//除去自帶的箭頭 //設置item點擊的監聽器 expandableListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText( ExpandableList.this, "你點擊了" + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_SHORT).show(); return false; } }); } }
ExpandableAdapter.java
package com.eyu.activity_test;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @author Administrator
* @date &{date}
*/
public class ExpandableAdapter extends BaseExpandableListAdapter{
private Context context;
public ExpandableAdapter(Context context){
this.context = context;
}
int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu};
//設置組視圖的顯示文字
private String[] generalsTypes = new String[] { "魏", "蜀", "吳" };
//子視圖顯示文字
private String[][] generals = new String[][] {
{ "夏侯惇", "甄姬", "許褚", "郭嘉", "司馬懿", "楊修" },
{ "馬超", "張飛", "劉備", "諸葛亮", "黃月英", "趙雲" },
{ "呂蒙", "陸遜", "孫權", "周瑜", "孫尚香" }
};
//子視圖圖片
public int[][] generallogos = new int[][] {
{ R.drawable.xiahoudun, R.drawable.zhenji,
R.drawable.xuchu, R.drawable.guojia,
R.drawable.simayi, R.drawable.yangxiu },
{ R.drawable.machao, R.drawable.zhangfei,
R.drawable.liubei, R.drawable.zhugeliang,
R.drawable.huangyueying, R.drawable.zhaoyun },
{ R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,
R.drawable.zhouyu, R.drawable.sunshangxiang } };
//自己定義一個獲得文字信息的方法
TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 84);
TextView textView = new TextView(
context);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 200, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return generals[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout(
context);
ll.setOrientation(0);
ImageView generallogo = new ImageView(
context);
generallogo
.setImageResource(generallogos[groupPosition][childPosition]);
ll.addView(generallogo);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition)
.toString());
ll.addView(textView);
return ll;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return generals[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return generalsTypes[groupPosition];
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return generalsTypes.length;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout(
context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView logo = new ImageView(context);
logo.setImageResource(logos[groupPosition]);
logo.setPadding(0, 0, 0, 0);
ll.addView(logo);
TextView textView = getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
ImageView iv =new ImageView(context);
iv.setPadding(70, 0, 0, 0);
if (isExpanded) {
iv.setBackgroundResource(R.drawable.btn1);
} else {
iv.setBackgroundResource(R.drawable.btn2);
}
ll.addView(iv);
return ll;
// LinearLayout ll = (LinearLayout) View.inflate( context, R.layout.list_item , null);
// TextView tv = (TextView)ll.findViewById(R.id.tv);
// ImageView iv1 = (ImageView)ll.findViewById(R.id.iv1);
// ImageView iv2 = (ImageView)ll.findViewById(R.id.iv2);
// tv.setText(getGroup(groupPosition).toString());
// iv1.setBackgroundResource(logos[groupPosition]);
// if (isExpanded) {
//
// iv2.setBackgroundResource(R.drawable.btn1);
// } else {
// iv2.setBackgroundResource(R.drawable.btn2);
// }
// return null;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
好了 大功告成!!!!
可以參考http://www.cnblogs.com/nuliniaoboke/archive/2012/11/13/2767957.html
demo下載