您的位置:首页 > 移动开发 > Android开发

ANDROID笔记:ExpandableListView和Adapter的简单使用

2013-10-17 11:28 501 查看
package com.example.adaptertest.doubleadapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

import com.example.adaptertest.R;

public class ExpandListViewActivity extends Activity {
private String[] groupStrings = { "家人", "同事", "朋友" };
private String[][] personStrings = { { "爸", "妈" }, { "同事1", "同事2" },
{ "朋友1", "朋友2" } };

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandlistview);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandlistview_list);
expandableListView.setAdapter(new BaseExpandListViewAdapter(
ExpandListViewActivity.this, groupStrings, personStrings));

}

}


package com.example.adaptertest.doubleadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.adaptertest.R;

public class BaseExpandListViewAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private String[] groupStrings;
private String[][] personStrings;
private Context context;

public BaseExpandListViewAdapter(Context context, String[] groupStrings,
String[][] personStrings) {
this.groupStrings = groupStrings;
this.context = context;
this.personStrings = personStrings;
inflater = LayoutInflater.from(context);
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupStrings.length;
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return personStrings[groupPosition].length;
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupStrings[groupPosition];
}

@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return personStrings[groupPosition][childPosition];
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.expandlistview_item, null);
TextView textView = (TextView) view.findViewById(R.id.expand_text);
textView.setText(groupStrings[groupPosition]);
return view;
}

@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.expandlistview_childitem, null);
view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context,
personStrings[groupPosition][childPosition],
Toast.LENGTH_SHORT).show();
}
});
TextView textView = (TextView) view
.findViewById(R.id.expand_child_text);
textView.setText(personStrings[groupPosition][childPosition]);
return view;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}

}


<?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" >

<ExpandableListView
android:id="@+id/expandlistview_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ExpandableListView>

</LinearLayout>


<?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="horizontal" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/expand_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="测试"
android:textSize="30sp" />

</LinearLayout>


<?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="horizontal" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_marginLeft="40dp"/>

<TextView
android:id="@+id/expand_child_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="测试"
android:textSize="30sp" />

</LinearLayout>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: