越研究越發現緊緊是一個listview就有很多需要學習的地方,今天研究了下ExpandableListView,下面就來介紹下如何使用吧。也許有些人還不知道這個的效果,先上個效果圖吧
點擊某一項,下面會彈出相應的列表,類似QQ好友列表界面。
廢話不多說 ,直接上代碼
主佈局就一個ExpandableListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ExpandableListView
android:id="@+id/exlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" />
</RelativeLayout>
主佈局item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:textSize="22sp"
android:text="TextView" />
<ImageView
android:id="@+id/ivfx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/btn_browser" />
</RelativeLayout>
下面是展開的佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvchild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textSize="22sp"
android:textColor="#ff0000"
android:text="TextView" />
</LinearLayout>
這裏我對字體顏色進行了區分 ,方便區分哪個是主佈局,哪個是子佈局
接下來就是最重要的activity的使用了
public class MainActivity extends Activity {
private List<String> group=new ArrayList<String>();
private List<List<String>> child=new ArrayList<List<String>>();
private ExpandableAdapter adapter;
private ExpandableListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initView() {
listView=(ExpandableListView) findViewById(R.id.exlistview);
adapter=new ExpandableAdapter();
listView.setAdapter(adapter);
listView.setCacheColorHint(0); //防止拖動時出現黑色背景
listView.setGroupIndicator(null);//設置自帶的箭頭消失
listView.setDivider(null);//設置沒有分割線
listView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "點擊了"+adapter.getChild(groupPosition, childPosition), 1000).show();
return false;
}
});
}
private void initData() {
//添加父佈局數據
group.add("測試數據1");
group.add("測試數據2");
group.add("測試數據3");
group.add("測試數據4");
group.add("測試數據5");
//添加子佈局數據
for(int j=0;j<5;j++){
List<String> childitem=new ArrayList<String>();
for(int i=0;i<5;i++){
childitem.add("子數據"+i);
}
child.add(childitem);
}
}
class ExpandableAdapter extends BaseExpandableListAdapter{
@Override
public int getGroupCount() {
return group.size();//獲取父佈局個數
}
@Override
public int getChildrenCount(int groupPosition) {
return child.get(groupPosition).size();//獲取子佈局個數
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
//父佈局樣式
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.listviewitem, null);
}
TextView tvparent=(TextView) convertView.findViewById(R.id.tvparent);
tvparent.setText(getGroup(groupPosition).toString());
ImageView ivView=(ImageView) convertView.findViewById(R.id.ivfx);
if(isExpanded){
ivView.setBackgroundResource(R.drawable.btn_browser2);
}else{
ivView.setBackgroundResource(R.drawable.btn_browser);
}
return convertView;
}
//子佈局樣式
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.listviewchild, null);
}
TextView tvchild=(TextView) convertView.findViewById(R.id.tvchild);
tvchild.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
點擊下載Demo