【问题标题】:List View not scrollable列表视图不可滚动
【发布时间】:2012-10-01 12:39:53
【问题描述】:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" >

        <RelativeLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="fill_parent"
            android:layout_height="600dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >

            <include
                android:id="@+id/include1"
                android:layout_width="fill_parent"
                android:layout_height="70dp"
                layout="@layout/header_history" >
            </include>

            <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:cacheColorHint="#00000000"
                android:divider="@android:color/black"
                android:dividerHeight="1.0sp"
                android:textColor="#000000" />
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

header_history

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/button" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/button"
        android:gravity="left|center_vertical"
        android:text="   History"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

我正在尝试使我的列表视图可滚动,但它已变为可滚动,但我无法找到列表视图的详细信息。请帮助我。朋友们,我也更新了 header_history 文件。

【问题讨论】:

    标签: android


    【解决方案1】:

    我只回答了一个类似的问题。这里我解释一下为什么它不滚动?

    只需从布局中删除 ScrollView。并且,将 LinearLayout 设为父级。并且,尝试运行您的应用程序。它会滚动列表。

    因为 ListView 类实现了自己的滚动并且它只是不接收手势,因为它们都由父 ScrollView 处理,我强烈建议您以某种方式简化布局。例如,您可以将想要滚动到 ListView 的视图添加为页眉或页脚。

    看看this

    查看我现有的answer

    【讨论】:

    • 嘿,伙计,但它的滚动,但上面代码中包含的头文件不可滚动,我也需要使其可滚动。
    • @onkar 此处的头文件,您包含的布局?
    • 是的
    • @onkar header_story 布局包含什么?
    • 嘿伙计,我也添加了 header_histroy 文件
    【解决方案2】:

    您不应该将 ListView 放在 ScrollView 中,因为 ListView 类实现了自己的滚动并且它只是不接收手势,因为它们都由父 ScrollView 处理。

    使 TextView 可滚动。

    只需设置

    android:maxLines = "AN_INTEGER"
    
    android:scrollbars = "vertical"
    

    布局的 xml 文件中 TextView 的属性。

    然后使用:

    yourTextView.setMovementMethod(new ScrollingMovementMethod())
    

    在您的代码中。

    【讨论】:

    • 我已经在其中添加了标题,我还需要使标题可滚动它就像一个常量部分一样
    • @onkar 把 Listview 放到 ScrollView 外面就好了。
    • 仍然不是我要找的结果。
    • @onkar 如果你想 textview 滚动然后不要使用 scrollbar 。检查我的答案。
    【解决方案3】:

    问题是您将ListView 放在ScrollView 中。 Android API 说你永远不应该把一个可滚动的元素放在另一个可滚动的元素中。

    如果您仍然需要这样做,您可以通过继承 ListView 并覆盖 onMeasure() 方法来解决问题。

    编辑:您使用以下代码创建一个完全展开的列表。另外值得注意的是,由于性能原因,这对于大型列表来说可能是一个可怕的想法。

    public class FullExpandedListView extends ListView {
    
        public FullExpandedListView(Context context) {
            super(context);
        }
    
        @Override
        protected void onMeasure(int a, int b) {
            super.onMeasure(a, b);
    
            setListViewHeightBasedOnChildren();
        }
    
        private void setListViewHeightBasedOnChildren() {
    
            SwfListAdapter listAdapter = (SwfListAdapter) getAdapter();
    
            if (listAdapter == null) {
    
                return;
            }
    
            int totalHeight = 0;
            int desiredWidth = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST);
    
            for (int i = 0; i < listAdapter.getCount(); i++) {
    
                View listItem = listAdapter.getView(i, null, listControl);
                listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams params = listControl.getLayoutParams();
            params.height = totalHeight + (listControl.getDividerHeight() * (listAdapter.getCount() - 1));
            setLayoutParams(params);
        }
    }
    

    【讨论】:

    • 我已经在其中添加了标题,我还需要使标题可滚动它就像一个常量部分一样
    • 通过覆盖列表视图的onMeasure 方法,您可以强制列表视图始终展开到其完整大小。结果,滚动视图将再次正确处理滚动事件,并应同时滚动您的列表和标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多