可扩展列表视图不使用可扩展ListActivity

时间:2011-04-27 14:20:55

标签: android xml expandablelistview expandablelistadapter

因为我还没有足够的声誉来评论之前的问题,所以我不得不创建一个新的。我需要一个可扩展的列表视图,它不会占用整个活动,因此我使用此示例在没有ExpandableListActivity的情况下执行此操作:

ExpandableList View don't expand

我稍微修改过的代码:

public class Main extends Activity {

    ExpandableListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_list);
        lv = (ExpandableListView) this.findViewById(R.id.expandableListView1);
        MyExpandableListAdapter expandableAdapter = new MyExpandableListAdapter();
        lv.setAdapter(expandableAdapter);
    }

    class MyExpandableListAdapter extends BaseExpandableListAdapter {

    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };


        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

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

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(Main.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }



        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
                View convertView, ViewGroup parent) {


            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;

        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

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

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;

        }

        public boolean hasStableIds() {
            return true;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

}

这很好用,但为了使它更健壮,我想将代码中创建的textViews分离为xml文件(这是一个好主意吗?)这是我遇到一些FC问题的地方。

group_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/row_name"
         android:paddingLeft="50px"
         android:textSize="20px"
         android:textStyle="normal"
         android:layout_width="320px"
         android:layout_height="wrap_content"/>
</LinearLayout>

我在上面的java文件中更改了这个:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    TextView parentRow = (TextView) findViewById(R.id.row_name);
    parentRow.setText(getGroup(groupPosition).toString());
    return parentRow;
}

我也尝试删除LinearLayout包装器但是没有修复任何东西。任何人都可以告诉我为什么我的xml视图不起作用?感谢。

修改 感谢Flo,使用本教程的新代码:

group_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group_name"
    android:paddingLeft="50dp"
    android:textSize="30sp"
    android:textStyle="normal"
    android:layout_height="wrap_content"/>

主:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {


        View parentView = convertView;
        if (parentView == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            parentView = vi.inflate(R.layout.group_row, null);
        }
                TextView parentText = (TextView) parentView.findViewById(R.id.group_name);
                if (parentText != null) {
                      parentText.setText(getGroup(groupPosition).toString());                     
                }

       return parentText;


    }

1 个答案:

答案 0 :(得分:0)

您对getGroupView()方法的实现是错误的。此时,您在活动的布局中搜索元素R.id.row_name不存在的元素。我想行parentRow.setText(getGroup(groupPosition).toString());会抛出异常,因为parentRow为空。

使用xml文件中的自定义行布局正确实现该方法是为了扩充此布局。查看this tutorial。这是普通的ListViews和适配器,但是行布局的概念是相同的。