如何在滚动视图中展开listview

时间:2017-01-17 04:42:33

标签: android listview scrollview

由于我是Android编程的新手,我面临一个问题,如下所述:

我在ListView内有一个ScrollView,但它没有扩展(例如:如果我有3个数据,ListView只显示1个数据)我需要制作{{1}扩张。

这是我的xml:

ListView

设计

enter image description here

任何帮助都会受到赞赏。谢谢费拉斯

5 个答案:

答案 0 :(得分:0)

您可以使用ExpandableListView而不是使用listview。

 <ExpandableListView
            android:id="@+id/activity_expandable_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"/>

请参阅此内容以获取更多详细信息。

http://thedeveloperworldisyours.com/android/expandable-listview-inside-scrollview/#sthash.awUvzL7C.dpbs

答案 1 :(得分:0)

您正在设置android:layout_height="wrap_content",这就是为什么它只显示一行的原因。 wrap_content要向其调整listView的大小,所有项目都无法正常工作。

<ListView
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:id="@+id/lvProfile"
            android:choiceMode="singleChoice"
            android:headerDividersEnabled="false"
            android:divider="@null"
            android:dividerHeight="0dp"/>

答案 2 :(得分:0)

在另一个Scrolling UI组件中使用一个Scrolling UI组件不是一个好习惯。

你应该改变你的实施逻辑。

答案 3 :(得分:0)

如果您在ScrollView中使用listview,那么请按照下面的代码来帮助您解决问题:

对于exe

listview.setAdapter(youradapter);
setListViewHeightBasedOnChildren(youradapter) 

public void setCustomListViewHeightBasedOnChildren(ListView listView, YourAdapter yourAdapter) {
            if (yourAdapter == null) {
                return;
            }
            int totalHeight;
            int listWidth = listView.getMeasuredWidth();
            int items = yourAdapter.getCount();
            if (items == 0) {
                totalHeight = 0;
            } else {
                View childView = yourAdapter.getView(0, null, listView);
                childView.measure(MeasureSpec.makeMeasureSpec(listWidth, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                totalHeight = childView.getMeasuredHeight() * items;
            }
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight;
            listView.setLayoutParams(params);
            listView.requestLayout();
    }

答案 4 :(得分:0)

您可以这样做吗?

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_expandable_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
     
        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">
     
    <!-- your code --->
     
     <ExpandableListView
                android:id="@+id/activity_expandable_list_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"/>
     
        </LinearLayout>
    </ScrollView>

然后您需要在活动或片段中添加一个功能:

private fun setListViewHeight(
        listView: ExpandableListView,
        group: Int
    ) {
        val listAdapter: ExpandableListAdapter =
            listView.expandableListAdapter as ExpandableListAdapter
        var totalHeight = 0
        val desiredWidth: Int = View.MeasureSpec.makeMeasureSpec(
            listView.getWidth(),
            View.MeasureSpec.EXACTLY
        )
        for (i in 0 until listAdapter.groupCount) {
            val groupItem: View = listAdapter.getGroupView(i, false, null, listView)
            groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED)
            totalHeight += groupItem.measuredHeight
            if (listView.isGroupExpanded(i) && i != group
                || !listView.isGroupExpanded(i) && i == group
            ) {
                for (j in 0 until listAdapter.getChildrenCount(i)) {
                    val listItem: View = listAdapter.getChildView(
                        i, j, false, null,
                        listView
                    )
                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED)
                    totalHeight += listItem.measuredHeight
                }
            }
        }
        val params: ViewGroup.LayoutParams = listView.layoutParams
        var height: Int = (totalHeight
                + listView.dividerHeight * (listAdapter.groupCount - 1))
        if (height < 10) height = 200
        params.height = height
        listView.layoutParams = params
        listView.requestLayout()
    }

您可以检查this postthe example in you github