[英]ExpandableListView shows only one groupView and no childView
我是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>
我已經為此進行了大約一個星期的工作,並且感到非常絕望。 因此,歡迎提出任何建議。
現在正在工作。 原因是在片段布局中, ExpandableListView嵌套在ScrollView中 。 顯然,這不是一個好習慣,因為ExpandableListView的大小無法正確計算(有關更多說明,請參見此博客 ,不幸的是部分中文)。 這樣,您將發現根本不會調用基本SimpleCursorTreeAdapter的 getChildView() 。 這就是為什么子視圖永遠不會被渲染的原因。
解:
擺脫ScrollView ,只需ExpandableListView即可正常工作。 要么
如果您必須將ExpandableListView嵌套在ScrollView下,請按照此答案進行 擴展 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.