【问题标题】:Android listview extra space at the end when scroll滚动时最后的Android listview额外空间
【发布时间】:2017-03-02 10:35:22
【问题描述】:

我在 listview 上平滑滚动时遇到了奇怪的行为。 我在列表中有 30 项。当我通过

平滑滚动时
listview.smoothScrollToPositionFromTop(29, 0);

屏幕末尾会有一些额外的空间。实际上 listview 向上移动了一点。图片附在下面。但是如果我手动滚动就没有空间了。可以做些什么来解决这种行为?

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_light">

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

【问题讨论】:

  • 在布局中:android:padding="0px" android:margin="0px"
  • 只需删除 listview.smoothScrollToPositionFromTop(29, 0);
  • @LunarWatcher 问题仍然存在,填充和边距为零
  • @JaiminModi 我故意想要平滑滚动到第 29 项。
  • 嗨,我也有同样的问题。

标签: android listview scroll


【解决方案1】:

正如您所说,我认为这是一个 Android 问题。它似乎是过度滚动动画的一部分,在完成动画之前似乎“卡住”了。我对SwipeToRefresh 有过类似的体验,刷新后进度指示器不会完全消失。

无论如何,您可以通过关闭动画来解决问题

    listView.setOverScrollMode(View.OVER_SCROLL_NEVER);

不幸的是,即使手动滚动也会丢失动画。如果你能忍受,我想这会解决你的问题。

AbsListView#setOverScrollMode(int)

编辑:(在原始答案被接受后)

我还找到了另一种解决方法。这个保留了过度滚动动画。不幸的是,它还有另一个缺点。如果用户在做任何其他事情之前自动滚动,则存在底部边框的相同问题。如果用户手动滚动到底部 1,从那时起就没有问题了。如果用户自动滚动并获得边框,然后手动滚动,就没有问题了。所以,这是另一种方法。

    Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.invisible_footer, null);
    listView.setOverscrollFooter(drawable);

在drawable文件夹中,“invisible_footer.xml”

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="2dp"
    android:width="10dp"

    android:viewportWidth="10"
    android:viewportHeight="2">

    <!-- a horizontal line -->
    <path android:pathData="M 1 1 H 10"
        android:strokeColor="#00000000" android:strokeWidth="1" />

</vector>

ListView.html#setOverscrollHeader()

这提供了选择:

  1. 滚动动画失败
  2. 保留动画,但有用户看到底部边框 1 次的风险

【讨论】:

    【解决方案2】:

    您是否尝试将 ListView 的高度设置为 wrap_content 而不是 match_parent

    即使在 [本教程] 中,所有属性都设置为 wrap_content。因此,将宽度和高度上的 ListView 属性替换为:

    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    【讨论】:

    • 你有没有尝试在 0px 处包装内容和边距/填充
    【解决方案3】:

    我已经实现了。但是,在最初的第一次它将填充保持在底部,但是从第二次开始它工作正常。

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.socialintegration.ListAct">
    <ListView
        android:id="@+id/listTemp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
    
    </RelativeLayout>
    

    代码:

    public class ListAct extends AppCompatActivity {
    ListView l;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        String str[] = new String[50];
        for (int i = 0; i < 50; i++) {
            str[i] = "poistion" + i;
        }
        ArrayAdapter adpt = new ArrayAdapter(ListAct.this, android.R.layout.simple_dropdown_item_1line, str);
        l = (ListView) findViewById(R.id.listTemp);
        l.setAdapter(adpt);
    
        l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                l.smoothScrollToPositionFromTop(29, 0);
            }
        });
    }
    }
    

    【讨论】:

    • 兄弟,现在我带了 30 件物品,同样的问题。
    • 我认为这可能是android或listview的默认行为。
    • 这个问题只是第一次......上面的代码。
    • 我认为这是 Android 问题,但找不到任何未解决或已关闭的问题。
    • @JaiminModi 你应该编辑你的答案。与其说你修好了,不如写下如何。你做了什么和原来的代码不一样?
    【解决方案4】:

    还有另一个 SO 帖子 link 描述了类似的问题。我想那篇文章中的解决方案应该可以解决您的问题。

    【讨论】:

      【解决方案5】:

      在你的 xml 中试试这个:

      <ListView
      android:id="@+id/listview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="0dp"
      android:clipToPadding="false"/>
      

      【讨论】:

        【解决方案6】:
        <ListView>
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior/>
        

        好的,请尝试: app:layout_behavior="@string/appbar_scrolling_view_behavior"

        【讨论】:

        • 是的,并且还从您的主要父布局中删除填充。
        • 这与我的布局相同,没有填充
        【解决方案7】:

        我坚信要转到RecyclerView。 如果不想动。 这是解决方法。只需将此方法添加到您的活动中,然后调用它。

        private void smoothScrollToTop(final int pos) {
            listView.smoothScrollToPositionFromTop(pos, 0);
            listView.post(new Runnable() {
                @Override
                public void run() {
                    listView.smoothScrollToPosition(pos);
                }
            });
        }
        

        这是经过充分测试的。并为我工作。这是输出。

        【讨论】:

        • 该解决方案处理空白,但它有点跳转到该位置而不是平滑滚动。
        • @Alock Leo 不,它首先会平滑滚动,然后会跳转。所以,它看起来像一个平滑的卷轴
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多