【问题标题】:Fixed height and internal scrolling for expanablelistview可扩展列表视图的固定高度和内部滚动
【发布时间】:2016-02-12 11:45:44
【问题描述】:

我有一个以编程方式填充的可扩展列表视图。我希望该可扩展列表视图具有固定长度,并且在每个组内滚动而不是整个列表。

我的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll_price_list_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/theme_blue"
        android:orientation="horizontal"
        android:weightSum="8" >

        <TextView
            android:id="@+id/tv_pricelisthead_item"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:gravity="left"
            android:text="PRICING"
            android:textColor="@color/white"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_pricelisthead_np"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="2"
            android:gravity="center"
            android:text="NORMAL"
            android:textColor="@color/white"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_pricelisthead_ep"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="2"
            android:gravity="center"
            android:text="EXPRESS"
            android:textColor="@color/white"
            android:textSize="18sp" />
    </LinearLayout>
    <!--
 android:background="@drawable/hf_bg"  
 <TextView
        android:id="@+id/tv_price_list"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="16dp"
        android:text="Price List"/>
    -->

    <ExpandableListView
        android:id="@+id/elv_pricelist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:transcriptMode="alwaysScroll" >
    </ExpandableListView>

</LinearLayout>

通过上面的布局,我可以获得一个列表视图。 我希望这个列表视图占据固定长度。应始终显示标题,并且滚动应该用于内部子项而不是整个列表。 我只有 3 个组,每个组可能有大约 10-30 个子项。

【问题讨论】:

    标签: android scroll expandablelistview


    【解决方案1】:

    我不久前在 SO 上找到了此代码,我尝试搜索此代码,但找不到。如果有人这样做,请编辑我的答案并提供原始答案的链接。由于ExpandableListView 扩展了ListView,因此您可以使用此代码,但请确保在您的适配器中子节点已经展开。

    private void justifyListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter adapter = listView.getAdapter();
            if (adapter == null) {
                return;
            }
            int totalHeight = 0;
            for (int i = 0; i < adapter.getCount(); i++) {
                View listItem = adapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams par = listView.getLayoutParams();
            par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
            listView.setLayoutParams(par);
            listView.requestLayout();
        }
    

    【讨论】:

    • 感谢您的回复...但添加此代码后我的列表停止滚动
    • 将您的LinearLayout 放入ScrollView,这样就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多