如何设置ListView高度取决于ScrollView中的项目

问题描述 投票:6回答:6

我正在尝试在listViewscrollView中加入ListView

问题是我不能以编程方式设置ScrollView高度并正确地重绘<ScrollView android:id="@+id/movie_detail_scroll_view" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/movie_detail" style="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipChildren="false"> ........... <ListView android:id="@+id/trails_list_view" android:layout_width="match_parent" android:layout_height="200dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/movie_videos_container" android:choiceMode="none"/> <include android:id="@+id/movie_reviews_container" layout="@layout/partial_movie_reviews" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/trails_list_view"/> ............ </RelativeLayout> </ScrollView> ,因为在更改高度之前它下方的项目仍处于旧位置。

XML

/**
 * Sets ListView height dynamically based on the height of the items.
 *
 * @param listView to be resized
 * @return true if the listView is successfully resized, false otherwise
 */
public static boolean setListViewHeightBasedOnItems(ListView listView, ViewGroup container) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

        //redraw the container layout.
        container.requestLayout();

        return true;

    } else {
        return false;
    }

}

我用来调整listView大小的方法:

enter image description here

结果:ScrollView

即使我重绘public static boolean setListViewHeightBasedOnItems(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter != null) { int numberOfItems = listAdapter.getCount(); // Get total height of all items. int totalItemsHeight = 0; for (int itemPos = 0; itemPos < numberOfItems; itemPos++) { View item = listAdapter.getView(itemPos, null, listView); float px = 500 * (listView.getResources().getDisplayMetrics().density); item.measure(View.MeasureSpec.makeMeasureSpec((int) px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); totalItemsHeight += item.getMeasuredHeight(); } // Get total height of all item dividers. int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1); // Get padding int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom(); // Set list height. ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalItemsHeight + totalDividersHeight + totalPadding; listView.setLayoutParams(params); listView.requestLayout(); //setDynamicHeight(listView); return true; } else { return false; } } ,我也无法摆脱这个自由空间

我该怎么办?

android android-layout listview android-scrollview
6个回答
1
投票

即使元素的高度不同,这也可以100%用于聊天。

<ScrollView
 android:id="@+id/movie_detail_scroll_view"
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:fillViewport="true">

0
投票

使您的滚动视图如下所示:

android:layout_height="wrap_content"

0
投票

尝试listview

android:fillViewport="true"

也不要删除:

        <LinearLayout
            android:id="@+id/movie_detail"
            style="?android:attr/textAppearanceLarge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:clipChildren="false">
                     ...........
             <ListView
                android:id="@+id/trails_list_view"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/movie_videos_container"
                android:choiceMode="none"/>
            <include
                android:id="@+id/movie_reviews_container"
                layout="@layout/partial_movie_reviews"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
                  ............
    </LinearLayout>
</ScrollView>

来自scrollview


0
投票

变为线性

RecyclerView

0
投票

我建议使用ListView而不是<android.support.v4.widget.NestedScrollView

1. ListView

使用NestedScrollview而不是Scrollview

wrap_content

将修复高度替换为qazxsw poi qazxsw poi到qazxsw poi

计算listview的动态高度

android:layout_height="200dp"

2. RecyclerView

android:layout_height="wrap_content"

在attrs.xml中

public void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);

        if(listItem != null){
            // This next line is needed before you call measure or else you won't get measured height at all. The listitem needs to be drawn first to know the height.
            listItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();

        }
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

然后在xml中设置public class MaxHeightRecyclerView extends RecyclerView { private int mMaxHeight; public MaxHeightRecyclerView(Context context) { super(context); } public MaxHeightRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); initialize(context, attrs); } public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initialize(context, attrs); } private void initialize(Context context, AttributeSet attrs) { TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView); mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightScrollView_maxHeight, mMaxHeight); arr.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mMaxHeight > 0) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } 如果高度小于200dp那么它将需要<declare-styleable name="MaxHeightScrollView"> <attr name="maxHeight" format="dimension" /> </declare-styleable> 并且在200dp之后它将滚动。


0
投票

在Constraint Layout中获取那些listview或RecyclerView ..然后不需要Scrollview视图..也不用担心列表HEIGHT ...

© www.soinside.com 2019 - 2024. All rights reserved.