例如在显示群组信息的时候,我们往往需要点击群组之后能够出现该群组详细列表,这里可以采用ExpandableListView这个控件来实现,其实如果会了ListView 以及 BaseAdapter的重写,那么这个很快就可以掌握了,只不过再多设计一个父类的显示吧了。
就不美化了,直接看吧。。。
看代码
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/expand" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ExpandableListView>
</LinearLayout> |
这里有点说明:如果你要使用原来自带的@id/listview 那么就必须继承ExpandableListActivity
Parent.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" >
<TextView android:id="@+id/parent" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout> |
Child.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" >
<LinearLayout android:id="@+id/child" android:layout_height="wrap_content" android:layout_width="fill_parent">
</LinearLayout>
</LinearLayout> |
下面就是MainActivity 主要看下Adapter怎么实现的,这里children里是各个child,有点嵌套,别晕了。。
package com.ylf;
import java.util.ArrayList; import java.util.List;
import android.app.Activity; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.LinearLayout; import android.widget.TextView;
public class MainActivity extends Activity {
List<String> parents = null; List<List> children = null;
ExpandableListView listView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
listView = (ExpandableListView)findViewById(R.id.expand);
parents = new ArrayList<String>(); children = new ArrayList<List>();
List<String> children1 =new ArrayList<String>(); List<String> children2 =new ArrayList<String>(); List<String> children3 =new ArrayList<String>(); List<String> children4 =new ArrayList<String>();
parents.add("水果"); parents.add("蔬菜"); parents.add("卫生间"); parents.add("垃圾堆");
children1.add("香蕉"); children1.add("苹果"); children2.add("空心菜"); children2.add("西红柿"); children3.add("马桶"); children3.add("水池"); children4.add("屎虫"); children4.add("苍蝇");
children.add(children1); children.add(children2); children.add(children3); children.add(children4);
listView.setAdapter(new MyExpandableListAdapter()); listView.setGroupIndicator(null); } class MyExpandableListAdapter extends BaseExpandableListAdapter{ @Override public Object getChild(int arg0, int arg1) { return children.get(arg0).get(arg1); }
@Override public long getChildId(int arg0, int arg1) { return arg0*parents.size()+arg1; }
@Override public View getChildView(int parentCount, int childCount, boolean arg2, View arg3, ViewGroup arg4) { LayoutInflater inflater = LayoutInflater.from(MainActivity.this); View view = inflater.inflate(R.layout.child, null); LinearLayout ll = (LinearLayout)view.findViewById(R.id.child); TextView tv = new TextView(MainActivity.this); tv.setPadding(60, 5, 5, 5); tv.setText((String)getChild(parentCount, childCount)); ll.addView(tv); ll.setPadding(5, 5, 5, 5);
return view; }
@Override public int getChildrenCount(int groupPosition) { return children.get(groupPosition).size(); }
@Override public Object getGroup(int groupPosition) { return parents.get(groupPosition); }
@Override public int getGroupCount() { return parents.size(); }
@Override public long getGroupId(int groupPosition) { return groupPosition; }
@Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(MainActivity.this); View view = inflater.inflate(R.layout.parent, null); TextView tv = (TextView)view.findViewById(R.id.parent);
tv.setPadding(50, 5, 5, 5); tv.setText((String)getGroup(groupPosition)); return view; }
@Override public boolean hasStableIds() { return false; }
@Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; }
} } |