【问题标题】:how to customize views of ExpandableList如何自定义 ExpandableList 的视图
【发布时间】:2011-12-18 13:03:09
【问题描述】:

我的 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>

【问题讨论】:

    标签: android android-layout expandablelistview expandablelistadapter


    【解决方案1】:

    分别为 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;
        }
    }
    

    希望对你有所帮助。

    【讨论】:

    • 至于getChildView,我用R.layout.row 代替你的R.layout.child_layoutR.id.title 代替你的R.id.tvChild,我已经在我的问题中添加了我的xml 文件。我得到的是java.lang.ClassCastException
    • 你能告诉你是在哪一行得到这个异常的吗?另外,请将 'ArrayList>' 类型更改为 'ArrayList>',或者根据您的要求进行更改。
    【解决方案2】:

    感谢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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多