如何自定义可扩展列表的视图

时间:2011-12-18 13:03:10

标签: android android-layout expandablelistview expandablelistadapter

我的Android应用中需要一个ExpandableList。通过扩展内容视图如下的ExpandableListActivity

<?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/android:list"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" />

   <TextView
      android:id="@+id/tv_add"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/txt_add" />
</LinearLayout>  

并扩展BaseExpandableListAdapter我现在能够在TextView中显示群组和子项的数据。
但是,我想自定义孩子的视图。我怎么能通过另一个xml文件这样做?

修改
这是我的孩子视图的row.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/title"
     android:textSize="16sp"
     android:textStyle="bold"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:gravity="left"
 />

 <TextView 
     android:id="@+id/description"
     android:textSize="10sp"
     android:textStyle="normal"
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"
     android:gravity="left"
 />
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

分别为Group和Child视图创建两个布局xml文件 - 例如,* group_layout.xml *和* child_layout.xml * 这些布局在自定义ExpandableListAdapter中膨胀并使用,如下所示。

您可以自定义Adapter类并将该适配器设置为ExpandableListView。

public class SampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
         ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);

         ExpandableListAdapter adapter = new ExpandableListAdapter(this, new ArrayList<String>(), new ArrayList<ArrayList<Vehicle>>());

         // Set this adapter to the list view
         listView.setAdapter(adapter);
    }
}

可以创建自定义适配器类,如下所示:

class ExpandableListAdapter extends BaseExpandableListAdapter {
 public ExpandableListAdapter(Context context, ArrayList<String> groups,
            ArrayList<ArrayList<Vehicle>> children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // Return a child view. You can load your custom layout here.
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        tv.setText("   " + vehicle.getName());
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(group);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }
}

希望这会对你有所帮助。

答案 1 :(得分:0)

Thx到PRC,但是根据我的row.xml,getChildView的正确版本是

 @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
        View convertView, ViewGroup parent) {
    LinearLayout layout;
    Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = (LinearLayout) infalInflater.inflate(R.layout.child_layout, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
    tv.setText("   " + vehicle.getName());
    return layout;
}