Android中ScrollView内的Listview高度问题

时间:2014-06-23 09:37:47

标签: android listview android-listview

我在Android中的ListView内使用ScrollView。所以,众所周知,它会发生高度问题。为此,我使用以下代码设置ListView大小:

public static void getListViewSize(ListView myListView, Context context) {
        ListAdapter myListAdapter = myListView.getAdapter();

        if (myListAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {

            View listItem = myListAdapter.getView(size, null, myListView);
            if (listItem instanceof ViewGroup)
                listItem.setLayoutParams(new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));

            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            @SuppressWarnings("deprecation")
            int screenWidth = display.getWidth();
            int listViewWidth = screenWidth - 65;
            int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,
                    MeasureSpec.AT_MOST);
            listItem.measure(widthSpec, 0);

            totalHeight += listItem.getMeasuredHeight();
        }

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

Xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lvHomeStream"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp" >
        </ListView>

        <TextView
            android:id="@+id/tvMoreRecords"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/list"
            android:gravity="center"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:text="@string/viewmore"
            android:textColor="@color/blue"
            android:textSize="16sp" />
    </LinearLayout>

</ScrollView>

如果每个ListView项的高度相同,则可以正常工作。但是,如果每个ListView项的高度异常,则会在ListView结束边界和TextView之间留出更大的空间。

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:5)

好的,我在这里分享我的项目代码可能对你有帮助..

此类计算高度并自动调整布局参数。

解决方案: -

 package com.kview;

 import android.content.Context;
 import android.util.AttributeSet;
 import android.widget.GridView;
 import android.widget.ListView;

 public class FullLengthListView extends ListView {

boolean expanded = true;

public FullLengthListView(Context context, AttributeSet attrs,
        int defaultStyle) {
    super(context, attrs, defaultStyle);
}

public FullLengthListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public boolean isExpanded() {
    return expanded;
}

public void setExpanded(boolean expanded) {
    this.expanded = expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // HACK! TAKE THAT ANDROID!
    if (isExpanded()) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        android.view.ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
  }

答案 1 :(得分:2)

将listview放在scrollView中并不是一个好习惯。如果将listview放在滚动内,那么listview的高度就是一个度量问题。你必须修改listview的高度。要滚动列表视图和滚动视图,你必须在listview上设置触摸列表器,如:

listview.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

现在,当你触摸列表视图时,它将禁用滚动视图的滚动,你的列表视图将工作。当你触摸屏幕的其他部分时,滚动视图将顺利运行。这只是可能的解决方案。

更新

现在你的使用recyclelerview,从23.0.2支持lib,它也支持wrap_content高度。

答案 2 :(得分:1)

这是您问题的最佳答案,由 Romain Guy 提供。 (他是ListView的父亲

How can I put a ListView into a ScrollView without it collapsing?

您应该使用LinearLayout代替ListView。 像这样:

public class MyListLayout extends LinearLayout implements
        View.OnClickListener {

    private Adapter list;
    private View.OnClickListener mListener;

    public MyListLayout(Context context) {
        super(context);
    }

    public MyListLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    public MyListLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onClick(View v) {
        if (mListener!=null)
            mListener.onClick(v);
    }

    public void setList(Adapter list) {
        this.list = list;

        //Popolute list
        if (this.list!=null){
            for (int i=0;i<this.list.getCount();i++){
                View item= list.getView(i, null,null);
                this.addView(item);
            }
        }

    }

    public void setmListener(View.OnClickListener mListener) {
        this.mListener = mListener;
    }
}

答案 3 :(得分:1)

由于Dev CarlsbergListView内建议不要使用 ScrollView,您可以将xml更改为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light" >

    <TextView
        android:id="@+id/tvMoreRecords"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/list"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:text="@string/viewmore"
        android:textColor="@color/blue"
        android:textSize="16sp" />

    <ListView
        android:id="@+id/lvHomeStream"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" >
    </ListView>

</RelativeLayout>

或者你也可以找到

Android ListView with Load More Button

Android - ListView to load more items when reached end

希望有所帮助:)