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

时间:2016-01-31 15:51:05

标签: android android-layout listview android-scrollview

我正在尝试在listView和。

中添加scrollView

问题是我无法以编程方式设置ListView高度并正确地重绘ScrollView,因为在更改高度之前,它下方的项目仍处于旧位置。

XML

<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>

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

/**
 * 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;
    }

}

结果: enter image description here

即使重绘ScrollView

,我也无法摆脱这个空闲空间

我该怎么办?

6 个答案:

答案 0 :(得分:0)

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

<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">

答案 1 :(得分:0)

尝试listview

android:layout_height="wrap_content"

也不要删除:

android:fillViewport="true"
来自scrollview的

答案 2 :(得分:0)

更改为线性

        <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>

答案 3 :(得分:0)

这对于聊天工作100%,即使元素&#39;高度不同。

for (i = 0; i < n-1; i++) {
  u[i+1] = (1+h)*u[i]; // Euler
  v[i+1] = v[i]/(1-h); // implicit Euler
}

答案 4 :(得分:0)

我建议使用RecyclerView代替ListView

1。 ListView

使用NestedScrollview而不是Scrollview

<android.support.v4.widget.NestedScrollView

将固定高度替换为wrap_content android:layout_height="200dp"android:layout_height="wrap_content"

计算列表视图的动态高度

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();
}

2。 RecyclerView

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);
    }
}

在attrs.xml中

<declare-styleable name="MaxHeightScrollView">
        <attr name="maxHeight" format="dimension" />
    </declare-styleable>

然后在xml中设置maxHeight = 200dp 如果高度小于200dp,则将花费wrap_content,而200dp之后,它将滚动。

答案 5 :(得分:0)

将这些listview或RecyclerView放在约束布局中。然后不需要Scrollview视图..也不必担心列表高度...