里面滚动型非滚动的ListView里面、ListView

2023-09-11 20:33:00 作者:废物^迷妹

我试图做这样的布局:

现在的问题是,我不希望列表视图是滚动的。我想,因为他们需要,使整个屏幕会滚动他们能够尽可能多的新高。如果我设置的ListView的高度 WRAP_CONTENT ,不能正常工作。使它工作的唯一办法是设置一个特定的高度 - 但我不知道有多少项目会在ListView

The problem is that I don't want the ListViews to be scrollable. I want them to be as much high as they need to and make the whole screen be scrollable. If I set the height of the ListView to wrap_content, that doesn't work. The only way to make it work is to set a specific height - but I don't know how many items will be in the ListView.

我知道我不应该把里面的ListView滚动型 - 但我不希望在ListView是滚动的,只是为了显示所有项目

I know that I should not put ListView inside ScrollView - but I don't want the ListView to be scrollable, just to show all items.

也许有更好的方法,使工作?

Maybe there is a better way to make it work?

推荐答案

创建自定义的ListView其中非滚动

public class NonScrollListView extends ListView {

    public NonScrollListView(Context context) {
        super(context);
    }
    public NonScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();    
    }
}

在布局资源文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fadingEdgeLength="0dp"
    android:fillViewport="true"
    android:overScrollMode="never"
    android:scrollbars="none" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <!-- com.Example Changed with your Package name -->

        <com.Example.NonScrollListView
            android:id="@+id/lv_nonscroll_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </com.Example.NonScrollListView>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lv_nonscroll_list" >

            <!-- Your another layout in scroll view -->

        </RelativeLayout>
    </RelativeLayout>

</ScrollView>

在Java文件

创建的ListView你customListview的对象,而不是 这样的:     NonScrollListView non_scroll_list =(NonScrollListView)findViewById(R.id.lv_nonscroll_list);

Create a object of your customListview instead of ListView like : NonScrollListView non_scroll_list = (NonScrollListView) findViewById(R.id.lv_nonscroll_list);