在Android中ScrollView内的Listview高度问题

3

我正在Android中使用ScrollView内部的ListView。正如我们所知,这会导致高度问题。为此,我正在使用以下代码设置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之间留下额外的大空间。
请帮助我解决这个问题。

2
这是一个不好的做法,将listView放在ScrollView里面。 - Shayan Pourvatan
根据您的需求,在listview的页脚视图中放置id为tvMoreRecords的textview,这样您就不需要在scrollview内部使用listview。 - Ravi Bhatt
@shayanpourvatan,那我该怎么使用它? - Jeeten Parmar
@Saqib,我知道这件事。 - Jeeten Parmar
@shayanpourvatan,我想要将TextView精确地显示在ListView的下方,并且没有任何额外的空间。 - Jeeten Parmar
显示剩余7条评论
4个回答

9

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

这个类可以计算高度并自动调整布局参数。

解决方案:

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

2
黑客!拿这个,安卓! =0 - Iatsenko Anton
非常好。只需要在xml中使用FullLengthListView,在Java代码中也同样如此。 - user2983041
它的工作效果非常好。简单而优雅的解决方案。太棒了! - gderaco

2

把ListView放在ScrollView里不是个好习惯。如果把ListView放在ScrollView里,那么ListView的高度就会成为一个测量问题。你需要固定ListView的高度。要同时滚动ListView和ScrollView,你需要在ListView上设置touch listener,例如:

listview.setOnTouchListener(new OnTouchListener() {

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

现在,当您触摸ListView时,它将禁用ScrollView的滚动,并且您的ListView将正常工作。否则,当您触摸屏幕的其他部分时,ScrollView将平稳地工作。这是唯一的可能解决方案。
更新
现在您可以使用RecyclerView,自23.0.2支持库以来,它也支持wrap_content高度。

谢谢你的回答。那么,当用户从上向下滑动活动时,我该如何自动加载数据呢? - Jeeten Parmar
你只需设置适配器并修复ListView的高度,就可以了。如果您使用其他类型的ListView,则仍然只需要设置适配器。问题在于ScrollView中的ListView的高度以及另一个问题是ScrollView内部的滚动,因为ListView的父类是ScrollView。只需解决高度问题,您就可以得到想要的一切了。 - Pankaj Arora

1
这是你问题的最佳答案,由ListView之父Romain Guy提供。 如何在ScrollView中放置ListView而不使其折叠? 你应该使用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;
    }
}

1
Dev Carlsberg所建议,不要在ScrollView中使用ListView,您可以将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

Android - 当滚动到底部时,ListView会加载更多项目

希望对您有所帮助 :)


Android ListView带有“加载更多”按钮,根据我的需求,这解决了我的问题。 - Jeeten Parmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接