【问题标题】:Show scroll only on touch Listview仅在触摸 Listview 上显示滚动
【发布时间】:2017-08-15 08:24:19
【问题描述】:

我想在列表视图被触摸时显示滚动,并且希望在不被触摸时消失,而不管列表视图中的项目数量。

这就是我现在的 sn-p 的样子

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list_profile"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="#40ffffff"
        android:dividerHeight="1dp"
        android:fastScrollEnabled="true"
        android:scrollbarStyle="insideOverlay"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#B1FFFFFF" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="18dp"
        android:paddingLeft="35dp"
        android:paddingRight="35dp"
        android:paddingTop="18dp">

        <com.bleexample2.CustomView.MyEditText
            android:id="@+id/edit_search"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:background="@drawable/bg_edit_text"
            android:hint="성함을 입력해주세요"
            android:lines="1" />

        <ImageButton
            android:id="@+id/btn_search"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:scaleType="centerInside"
            android:src="@drawable/ic_search" />

        <ImageButton
            android:id="@+id/btn_add_profile"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:scaleType="centerInside"
            android:src="@drawable/ic_add" />
    </LinearLayout>
</LinearLayout>

我的 styles.xml 看起来像这样:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:textColor">#c6c6c6</item>
        <item name="android:editTextColor">@color/editTextTextColor</item>

        <item name="android:fastScrollTrackDrawable">@drawable/fast_scrollbar_track</item>
        <item name="android:fastScrollThumbDrawable">@drawable/ic_thumb</item>

    </style>

    <style name="AppTheme.CustomFastScrollBar" parent="AppTheme" >
        <item name="android:fastScrollThumbDrawable">@drawable/scroll_bar_vertical_thumb</item>
        <item name="android:fastScrollTrackDrawable">@drawable/scrollbar_vetical</item>
        <item name="android:fadeScrollbars">false</item>
    </style>

使用 fastScrollAlwaysVisible 我只能将其设置为始终显示或不显示。我怎样才能实现我想要的,即仅在触摸列表视图时显示。

【问题讨论】:

  • 只需删除fastScrollAlwaysVisible 属性。默认行为是在您不触摸 ListView 时自动隐藏快速滚动条。
  • 即使列表中有一个项目,我也想要这种行为。每当我触摸屏幕时,滚动应该会出现,当我不触摸时它应该隐藏或消失
  • 哦,我明白你的意思了。嗯,为什么?也就是说,你为什么要给用户一个视觉指示 ListView 不是可滚动的?
  • 从开发人员的角度来看似乎很奇怪,但这是我的设计要求
  • 啊,明白了。好吧,不幸的是,我不相信你能用标准属性做到这一点。我觉得它需要反思才能摆弄ListView 的内部结构。

标签: android listview


【解决方案1】:

在您的主题中进行以下覆盖:

<style name="Theme.App" parent="android:Theme.Light">
    <item name="android:fadeScrollbars">true</item>
 </style>

在这里看到第一个答案:

Autohide scrollbars when not scrolling in a ListView

注意:只有当你有很多项目(超过一个全屏)时才会出现淡入淡出的滚动条。另一种方法是相反 -> 滚动条始终可见 (How to always show scrollbar)

最后可能的解决方案是创建一个自定义列表视图(从 ListView 扩展)并以某种方式访问​​滚动条的机制

【讨论】:

  • 不工作。仅当列表中有许多要滚动的项目时才出现?即使列表中有一个项目,我也想要这种行为。
  • 试过似乎不起作用。我还有一个列表项。但我添加了更多代码细节。感谢您为帮助所做的努力
  • 我又加了一句。根据我的经验,这样的定制非常复杂,需要付出巨大的努力并且总体上会受到很多伤害。对于 android,它主要是:“选择 A 或 B,C 在概念上是可能的,……祝你好运,你的 android”:)
猜你喜欢
  • 2014-10-15
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 2016-01-28
  • 2014-10-22
  • 1970-01-01
相关资源
最近更新 更多