【问题标题】:ExpandableList View not using ExpandableListActivity未使用可扩展 ListActivity 的可扩展列表视图
【发布时间】:2011-04-27 14:20:54
【问题描述】:

因为我还没有足够的声望点来评论以前的问题,所以我不得不创建一个新问题。我需要一个不占用整个活动的可扩展列表视图,因此我使用此示例在没有 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;


    }

【问题讨论】:

    标签: android xml expandablelistview expandablelistadapter


    【解决方案1】:

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

    使用来自 xml 文件的自定义行布局的方法的正确实现是扩展此布局。查看this tutorial。它适用于普通的 ListViews 和适配器,但膨胀行布局的概念是相同的。

    【讨论】:

    • 这很有帮助。我正在尝试将内部类 MyExpandableListAdapter 移动到它自己的类中,以便我可以将它用于我的所有活动,但我无法将上下文获取到新类。具体来说 getSystemService 是未定义的。有什么想法吗?
    • 因为它是您的自定义适配器类,您可以定义构造函数的签名,这样您就可以简单地定义它,以便它将 Context 作为参数。然后,您可以将上下文存储为适配器类的成员变量,并在您想要的任何地方使用它。
    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多