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>
相关文章推荐
- ExpandableListview的简单使用
- 《安卓应用开发学习》——关于使用ExpandableListView.setAdapter()方法发生空指针异常
- android ExpandableListView ExpandableListAdapter使用
- ExpandableListView扩展(BaseExpandableListAdapter的使用)
- android中ListView+ArrayAdapter简单使用
- Android ExpandableListView 的简单使用
- Android开发之ExpandableListView扩展(BaseExpandableListAdapter的使用)(完整版)
- Android实例demo11之listview的简单使用(arrayadapter、simpleadapter)
- Expandablelistview 简单使用
- android中经常用到的 ExpandableListView 的简单使用
- ExpandableListView的简单使用
- ExpandableListView的简单使用
- android组件ListView之ArrayAdapter简单使用
- android中ExpandableListView 使用自定义Adapter的方法
- marsAndroid学习——常见控件ExpandableListActivityh和SimpleExpandableListAdapter的简单使用
- listview的简单使用(Baseadapter)
- marsAndroid学习——常见控件ExpandableListActivityh和SimpleExpandableListAdapter的简单使用
- android组件ListView之BaseAdapter简单使用
- 使用PullToRefreshExpandableListView不能setAdapter的问题
- android组件ListView之SimpleAdapter简单使用