【问题标题】:Scrolling over listview items not working on device滚动列表视图项目在设备上不起作用
【发布时间】:2015-05-19 13:08:40
【问题描述】:

我想滚动屏幕中的一部分数据,为此我在布局中添加了滚动视图,但列表视图没有滚动,它在模拟器中工作正常。我阅读了所有相关的文章,其中说 listview 滚动是不可能的,并尝试了设计自定义滚动的替代方法,但只有部分数据没有滚动。为什么会这样?没有其他选择?

我的布局是这样的

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
   //few more layout items goes here
 <LinearLayout  android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ListView android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:divider="#00000000"
            android:dividerHeight="5dp"
            android:textStyle="bold"
            android:typeface="serif"
            />
        <LinearLayout  android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Total"
                android:textStyle="bold"
                android:textColor="#F5D8BA"
                android:layout_marginRight="30dp"
                android:layout_gravity="center"
                android:typeface="serif"
                />
       </LinearLayout>
 </LinearLayout>
</ScrollView>

listview里面的数据是这样的

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"
    >
<LinearLayout
    android:layout_width="75dp"
    android:layout_height="wrap_content" android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12dp"
        android:textColor="#000000"
        android:text="TextView"
        android:textStyle="bold"
        android:typeface="serif"
        />

    <TextView
        android:id="@+id/textViewStyle"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12dp"
        android:textColor="#000000"
        android:textStyle="bold"
        android:typeface="serif"
        />

</LinearLayout>
   </LinearLayout>
    </ScrollView>

【问题讨论】:

  • 从列表视图数据中移除滚动视图...
  • 您在使用 ListView 时不需要 ScrollView .. 进一步您可以向 ListView 添加页眉或页脚视图
  • 是的,它现在可以工作了,但是我在列表视图下面的元素很少,它不在视图中,我的意思是推到屏幕内,所以我添加了滚动视图,我将如何处理?我有几个按钮和其他部分。

标签: android android-layout scroll android-scrollview


【解决方案1】:

它背后的原因 - 你的列表视图在 ScrollView 内

请按照以下步骤解决您的问题 -

ListView lv = (ListView)findViewById(R.id.landHoldingList);  // your listview inside scrollview
lv.setOnTouchListener(new ListView.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });

这样做是禁用ScrollView 上的TouchEvents 并使ListView 拦截它们。它很简单,并且一直有效。

同时从ListView 数据中删除ScrollView

【讨论】:

  • 我又试了一次,但无法滚动,我尝试的代码是pastie.org/10196742
  • 您是否从 Listview 数据中删除了 Scrollview? @Annyy07
  • 是的,它现在可以工作了,但是我在列表视图下面有几个元素它不在视图中,我的意思是推到屏幕内,所以我添加了滚动视图,我将如何处理?
  • 你说的是哪个scrollview......."ScrollView inside Listview"还是"scrollview contains listview"?
  • 包含列表视图的滚动视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多