android - ScrollView 内不可滚动的 ListView

标签 android android-listview scrollview

我正在尝试制作这样的布局:

问题是我不希望 ListViews 可滚动。 我希望它们尽可能高,并使整个屏幕可滚动。如果我将 ListView 的高度设置为 wrap_content ,这是行不通的。让它工作的唯一方法是设置一个特定的高度 - 但我不知道 ListView 中有多少项目。

我知道我不应该将 ListView 放入 ScrollView - 但我不想要 ListView可滚动,只是为了显示所有项目。

也许有更好的方法让它发挥作用?

最佳答案

您创建不可滚动的自定义 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 文件中

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

关于android - ScrollView 内不可滚动的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18813296/

相关文章:

android - 如何在多选 ListView 中保存勾选位置项的状态

android - 向 ListView 添加水印

android - 使用自定义 ListView 打开对话框

android - 使用 ImageView 进行全景查看

iOS 8.4 : Scroll view resets contentOffset with Voice Over enabled shortly after view appear

java - Libgdx 如何设置闹钟?

android - 如何关闭 Activity ,以便无法从 Activity 应用程序再次打开?

android - 调用服务的 onDestroy()

java - 如何从 Android 向 Web 服务器发送多部分/表单数据?

android - 在 ScrollView 屏幕底部定位按钮并设置比例宽度