【问题标题】:Custom overscroll glow/edge color for ListView?ListView 的自定义过度滚动发光/边缘颜色?
【发布时间】:2012-08-19 14:05:59
【问题描述】:

我有一个ListView,还有一个自定义样式,基本上看起来就像 Holo,但带有黄色而不是蓝色。

当我滚动到列表的底部或顶部时,我会看到不合适的蓝色。我为overscroll_glow.pngoverscroll_edge.png 制作了自定义drawable,但我不知道如何使用它们。

如何让我的ListView 使用这些可绘制对象而不是系统的绘制对象?

OverScroller 课程会有帮助吗?

【问题讨论】:

    标签: android android-layout android-widget android-listview


    【解决方案1】:

    您可以使用类似反射的解决方案来改变这种发光颜色:

    int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
    Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
    androidGlow.setColorFilter(brandColor, PorterDuff.Mode.MULTIPLY);
    

    我遇到了同样的问题,这似乎是一个很好的解决方案,至少在 Google 添加用于更改颜色(或标记颜色)的 API 之前:http://evendanan.net/android/branding/2013/12/09/branding-edge-effect/

    【讨论】:

    • 乘法不好,因为它会将你的颜色与默认的蓝色混合。您应该使用 SrcAtop。它将完全取代旧颜色。
    • 就 ScrollView 而言,这个代码在哪里调用?
    • 嘿,Menny,谢谢你的帖子我似乎无法让它工作我有这样的上下文设置Context context = this;
    【解决方案2】:

    只需使用这个库。在没有过度滚动的设备(HTC One X/Sense 4.5)上,我只遇到了在横向弹出菜单上包装上下文的问题。像魅力一样工作!

    https://github.com/AndroidAlliance/EdgeEffectOverride

    【讨论】:

      【解决方案3】:

      试过了吗? setOverscrollFooter(Drawable)?

      更新 1

      哎呀。没关系。你可能已经看过this discussion

      发光行为包含在EdgeEffect class 中。您可以通过在 ListView 上调用 setOverScrollMode(OVER_SCROLL_NEVER) 来禁用默认边缘效果,然后滚动您自己的 EdgeEffect 类(此时我无法了解其详细信息)。

      更新 2

      哎哟。在 AbsListView 中创建的 EdgeEffect 对象的调用被深深地隐藏在滚动逻辑中。可能很艰难。一个简单的解决方法是向 Android 团队提出功能请求,要求在 AbsListView 中使用 setEdgeEffect(EdgeEffect) 方法...

      【讨论】:

      • 试过页脚,不是我需要的。所以我这样做的唯一方法是创建一个自定义AbsListView 类?太老套了,我不喜欢:(
      • 好吧,我现在确信它可以通过继承 ListView 并覆盖一系列方法来完成(onOverScrolled() 是重要的方法),但我不确定它是否值得。 :(
      【解决方案4】:

      这里是你的解决方案:

      public static void ChangeEdgeEffect(Context cxt, View list, int color){
      
          if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      
              EdgeEffect edgeEffectTop = new EdgeEffect(cxt);
              edgeEffectTop.setColor(color);
              EdgeEffect edgeEffectBottom = new EdgeEffect(cxt);
              edgeEffectBottom.setColor(color);
      
              try {
                  Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop");
                  f1.setAccessible(true);
                  f1.set(list, edgeEffectTop);
      
                  Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
                  f2.setAccessible(true);
                  f2.set(list, edgeEffectBottom);
              } catch (Exception e) {
                  e.printStackTrace();
              }
      
          }else{
              int glowDrawableId = cxt.getResources().getIdentifier("overscroll_glow", "drawable", "android");
              Drawable androidGlow = cxt.getResources().getDrawable(glowDrawableId);
              assert androidGlow != null;
              androidGlow.setColorFilter(cxt.getResources().getColor(color), PorterDuff.Mode.SRC_ATOP);
          }
      }
      

      【讨论】:

        【解决方案5】:

        请不要忘记更改滚动边缘线:

                final int edgeDrawableId = res.getIdentifier("overscroll_edge", "drawable", "android");
                final Drawable overscrollEdge = res.getDrawable(edgeDrawableId);
                overscrollEdge.setColorFilter(res.getColor(colorID), android.graphics.PorterDuff.Mode.SRC_ATOP);
        

        【讨论】:

          【解决方案6】:

          我添加此答案是因为这是该主题的最佳结果之一,而最佳答案不再正确。 好吧,它实际上是用于棒棒糖之前的版本,但正如预期的那样,当你使用 java 反射时,你会被咬回来,这就是发生的事情,如果你使用代码

          int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
          Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
          androidGlow.setColorFilter(context.getResources().getColor(R.color.gm_color), PorterDuff.Mode.MULTIPLY);
          

          这将在 Lollipop + 设备中崩溃,因为该资源不再存在。所以对于 android 5.+ 你应该使用:

                 <item name="android:colorEdgeEffect">@color/custom_color</item>
          

          在您的主题中,如果您的目标不是最低 21 版本,则在 values-v21/themes.xml 文件中覆盖

          【讨论】:

            猜你喜欢
            • 2015-02-05
            • 1970-01-01
            • 2020-12-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-23
            相关资源
            最近更新 更多