【问题标题】:Listview Height issue inside ScrollView in AndroidAndroid中ScrollView中的Listview高度问题
【发布时间】:2014-06-23 09:37:47
【问题描述】:

我在 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 之间留下额外的大空间。

请帮我解决这个问题。

【问题讨论】:

  • 这不是你的答案,但它不是将listView 放入ScrollView 的好方法
  • listview 已经是一个滚动视图
  • 根据您的要求,将 id 为 tvMoreRecords 的 textview 放在 listview 的页脚视图中,这样您就不需要在 scrollview 中使用 listview 了。
  • @shayanpourvatan,那我该怎么用呢?
  • @RaviBhatt,我知道,但是那样,它会在我不想要的固定位置显示 TextView

标签: android listview android-listview


【解决方案1】:

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

这个类计算高度并自动调整layoutparams。

解决办法:-

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

【讨论】:

  • 哈克!拿那个安卓! =0
  • 非常好。只需要在 xml 和 Java 代码中使用 FullLengthListView。
  • 它就像一个魅力。简单而优雅的解决方案。太棒了!
【解决方案2】:

将列表视图放在滚动视图中不是一个好习惯。如果将列表视图放在滚动中,则列表视图的高度是一个度量问题。您必须修复 listview 的高度。要滚动 listview 和滚动视图,您必须在 listview 上设置触摸侦听器,例如:

listview.setOnTouchListener(new OnTouchListener() {

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

现在,当您触摸列表视图时,它将禁用滚动视图的滚动,并且您的列表视图将起作用。否则,当您触摸屏幕的其他部分时,滚动视图将正常工作。只有这可能的解决方案。

更新

现在你使用 recyclerview,因为 23.0.2 支持 lib,它也支持 wrap_content 高度。

【讨论】:

  • 感谢您的回答。那么,当用户在活动中从上到下时如何自动加载数据???
  • 你只需要设置adpater dude,并修复listview的高度。就是这样。如果你使用任何其他类型的listview,那么你还必须设置适配器onlu,这里的问题是高度滚动视图中的列表视图以及滚动中的另一个问题滚动视图,因为列表视图父类是滚动视图。只需修复高度,您将获得所需的一切
【解决方案3】:

正如Dev Carlsberg 建议的不要使用 ListViewScrollView 内,您可以将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

希望对你有帮助:)

【讨论】:

  • 带有加载更多按钮的Android ListView,这已经按照我的要求解决了我的问题。
【解决方案4】:

这是您问题的最佳答案,由 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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多