简体   繁体   English

ExpandableListView仅显示一个groupView而没有childView

[英]ExpandableListView shows only one groupView and no childView

I'm a newbie in Android. 我是Android的新手。 I want to show my records in a database using an expandable list view, so that records of the same day form a group. 我想使用可扩展列表视图在数据库中显示我的记录,以便同一天的记录组成一个组。 I want something like this: 我想要这样的东西:

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
...

But what I get is this: 但是我得到的是:

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

Only one record is showing as the group view, and when I expand it, nothing shows as a child. 只有一个记录显示为组视图,而当我展开它时,没有任何显示为子视图。

FirstFragment.java: 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 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 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 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>

I've been working on this for about a week and am getting pretty desperate. 我已经为此进行了大约一个星期的工作,并且感到非常绝望。 So please, any suggestion is welcome. 因此,欢迎提出任何建议。

It's working now. 现在正在工作。 The reason is that in the fragment layout, the ExpandableListView is nested in a ScrollView . 原因是在片段布局中, ExpandableListView嵌套在ScrollView中 Apparently, this is not good practice, because the size of ExpandableListView could not be correctly calculated (for more explanations see this blog , unfortunately partly in Chinese). 显然,这不是一个好习惯,因为ExpandableListView的大小无法正确计算(有关更多说明,请参见此博客 ,不幸的是部分中文)。 This way, you'll find that getChildView() of the base SimpleCursorTreeAdapter never gets called. 这样,您将发现根本不会调用基本SimpleCursorTreeAdapter的 getChildView() That's why the child view never gets rendered. 这就是为什么子视图永远不会被渲染的原因。

Solution: 解:

  1. Get rid of ScrollView , just ExpandableListView will work fine. 摆脱ScrollView ,只需ExpandableListView即可正常工作。 Or 要么

  2. Extend ExpandableListView as this answer does, if you have to have it nested under ScrollView . 如果您必须将ExpandableListView嵌套在ScrollView下,请按照此答案进行 扩展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM