cuốn sách gpt4 ai đã làm

android - Vấn đề về chiều cao với ListView bên trong ListView và ListView lồng nhau

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-03 01:02:24 26 4
mua khóa gpt4 Nike

Tôi có bố cục cha mẹ này


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:trọng lực="trung tâm|top"
android:text="Tiêu đề"
android:textSize="20sp" />


android:id="@+id/parent_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >


Các mục danh sách của parent_listview có bố cục này


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:background="@color/mediumBlue" >


android:id="@+id/sub_title"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_height="wrap_content" />


android:id="@+id/child_listview"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_below="@id/sub_title" >



Các mục của child_listview có bố cục này


android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="@color/green" >


android:id="@+id/childName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp" />


android:id="@+id/childDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" >



tôi muốn xem

Bố cục gốc
- Xem danh sách phụ huynh
- Mục gốc 1
-Mục con 1
-Mục con 2
-Mục con 3
- Mục gốc 2
-Mục con 1
-Mục con 2
- Mục gốc 3
-Mục con 1
- Mục gốc 4
-Mục con 1
-Mục con 2
-Mục con 3

Nhưng ListView con chỉ hiển thị một mục (mục đầu tiên) như thế này

Bố cục gốc
- Xem danh sách phụ huynh
- Mục gốc 1
-Mục con 1
- Mục gốc 2
-Mục con 1
- Mục gốc 3
-Mục con 1
- Mục gốc 4
-Mục con 1

Nếu tôi điều chỉnh chiều cao của ListView con thành 250dp theo cách thủ công, nó sẽ hiển thị nhiều mục hơn nhưng tôi muốn chiều cao của ListView con tự động bao bọc dựa trên số lượng mục. Làm thế nào tôi có thể làm điều này? (Tôi có một bộ chuyển đổi cho cả hai ListView nhưng tôi không chắc liệu nó có liên quan đến cuộc thảo luận này hay không nên tôi sẽ không đưa nó vào).

câu trả lời hay nhất

Theo nguyên tắc chung, bạn không bao giờ nên nhúng ListView vào trong ListView khác. Android không thích nhúng nội dung có thể cuộn.

Nếu bạn thực sự không muốn làm những gì bạn mô tả. Bạn có thể sử dụng (nhúng) một ListView chủ động tính toán trước chiều cao của nó dựa trên nội dung của bộ điều hợp, như sau:

lớp công khai ExpandedListView mở rộng ListView {

public ExpandedListView(Context context) {
siêu (bối cảnh);
}

public ExpandedListView(Context context, AttrsAttrs) {
super(bối cảnh, attrs);
}

@Ghi đè
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setListViewHeightBasedOnChildren(this);
}

public static void setListViewHeightBasedOnChildren(ListView listView) {
Danh sách bộ điều hợpAdapter = listView.getAdapter();
nếu (listAdapter == null)
return;

int TotalHeight = 0;
int mong muốnWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
Xem listItem = listAdapter.getView(i, null, listView);
nếu (listItem != null) {
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
TotalHeight += listItem.getMeasuredHeight();
}
}

Thông số ViewGroup.LayoutParams = listView.getLayoutParams();
params.height = TotalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}

Và các mục danh sách của Parent_listview có bố cục này


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:background="@color/mediumBlue" >


android:id="@+id/sub_title"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_height="wrap_content" />

<>
android:id="@+id/child_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/sub_title" >



Về vấn đề chiều cao của ListView và ListView lồng nhau trong android - ListView, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/19228266/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress