【问题标题】:ExpandableListView shows only one groupView and no childViewExpandableListView 只显示一个 groupView 而没有 childView
【发布时间】:2017-10-31 05:20:29
【问题描述】:

我是 Android 新手。我想使用可扩展的列表视图在数据库中显示我的记录,以便同一天的记录形成一个组。我想要这样的东西:

day 1   
-------------------
record 1 of day 1    
record 2 of day 1

day 2
-------------------
record 1 of day 2
record 2 of day 2
record 3 of day 2
...

但我得到的是:

day 1
-------------------

只有一条记录显示为组视图,当我展开它时,没有任何内容显示为子视图。

FirstFragment.java:

public class FirstFragment extends Fragment {

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        createExpandableListView();

    } 

    private void createExpandableListView() {
        String time = Long.toString(((ShowBillBookActivity)getActivity()).book.getTime().getTimeInMillis());
        BillTableHandler handler = new BillTableHandler(getActivity(),
            ((MyApplication) getActivity().getApplication()).BILL_TABLE_NAME_PREFIX + time);
        ExpandableListView view =  (ExpandableListView) getView().findViewById(R.id.expandableListView);
        Cursor c = handler.getAllBills();
        Log.d("tag", "handler.getAllbills(): "+c.getCount());
        ExpandableListAdapter adapter = new ExpandableListAdapter(handler,
            this.getActivity(),
            handler.getAllBills(),
            R.layout.group_expandable_list_view,
            new String[]{BillEntry.DAY, BillEntry.AMOUNT},
            new int[]{R.id.textview_time, R.id.textview_extra},
            R.layout.child_expandable_list_view,
            new String[]{BillEntry.PAYER, BillEntry.AMOUNT},
            new int[]{R.id.textview_payer, R.id.textview_amount});
        view.setAdapter(adapter);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
}

ExpandableListAdapter.java

public class ExpandableListAdapter extends SimpleCursorTreeAdapter {
    BillTableHandler handler;

    public ExpandableListAdapter(BillTableHandler handler, Context context, Cursor cursor, int groupLayout,
                             String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom,
                             int[] childTo){
        super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo);
        this.handler = handler;
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        long time = groupCursor.getLong(groupCursor.getColumnIndexOrThrow(BillTableHandler.BillEntry.DAY));
        Cursor c = handler.getBillsOfTime(time);
        Log.d("tag","child Count : "+c.getCount());
        c.moveToFirst();
        return c;
    }
}

group_expandable_list_view.xml

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/textview_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:text="TextView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textview_extra"
    android:layout_width="61dp"
    android:layout_height="18dp"
    android:text="TextView"
    app:layout_constraintLeft_toRightOf="@+id/textview_time"
    android:layout_marginLeft="32dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp" />
</android.support.constraint.ConstraintLayout>

child_expandable_list_view.xml

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:focusable="false"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/textview_payer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:text="hello"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textview_amount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="8dp"
    android:text="world"
    app:layout_constraintLeft_toRightOf="@+id/textview_payer"
    app:layout_constraintTop_toTopOf="parent" />
<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:text="text"
    app:layout_constraintTop_toBottomOf="@+id/textview_amount"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>

我已经为此工作了大约一个星期,并且变得非常绝望。所以,欢迎提出任何建议。

【问题讨论】:

标签: java android expandablelistview


【解决方案1】:

它现在正在工作。原因是在片段布局中,ExpandableListView嵌套在一个ScrollView中。显然,这不是一个好的做法,因为 ExpandableListView 的大小无法正确计算(更多解释见this blog,不幸的是部分中文)。这样,您会发现基础 SimpleCursorTreeAdaptergetChildView() 永远不会被调用。这就是为什么子视图永远不会被渲染的原因。

解决方案:

  1. 去掉 ScrollView,只需要 ExpandableListView 就可以了。或者

  2. this answer 那样扩展 ExpandableListView,如果您必须将其嵌套在 ScrollView 下。

【讨论】:

  • 尝试了第二种解决方案及其工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多