2016-02-15 17 views
20

recyclerViewでスクロールバーの色を変更するにはどうすればよいですか?RecyclerViewのスクロールバーの色

私はスクロールバーを持っていますが、色を変えたいと思います。

<android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/recyclerView" 
    android:layout_below="@id/my_toolbar" 
    android:layout_above="@+id/progressBar" 
    android:scrollbars="vertical" 
    /> 

答えて

31

あなたのRecyclerviewに次のコード行を含めることで、これを行うことができます私の場合は描画可能ファイルがある

android:scrollbarThumbVertical="@drawable/yoursdrawablefile

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient android:startColor="#000" android:endColor="#000" 
     android:angle="45"/> 
    <corners android:radius="6dp" /> 
</shape> 
+0

これは動作します、ありがとう! – Yass

+0

ようこそ........ – Naresh

+0

awesome !!!!!!!! –

7
私recyclerViewは、このようなものです

スクロールバーのスタイルをさらに変更したい場合は、drawableフォルダに2つのdrawableリソースファイルを1として作成します。scrollbar_trackと2 。scrollbar_thumb

scrollbar_track.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <gradient 
     android:angle="0" 
     android:endColor="#9BA3C5" 
     android:startColor="#8388A4" /> 
     <corners android:radius="6dp" /> 
</shape> 

scrollbar_thumb.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

    <gradient 
     android:angle="0" 
     android:endColor="#b20111" 
     android:startColor="#cf1d2d" /> 

    <corners android:radius="6dp" /> 

</shape> 

、としてあなたのstyles.xmlファイルにscrollbar_styleという名前のスタイルを作成します。

<style name="scrollbar_style"> 
    <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item> 
    <item name="android:scrollbarStyle">outsideOverlay</item> 
    <item name="android:scrollbars">vertical</item> 
    <item name="android:fadeScrollbars">true</item> 
    <item name="android:scrollbarThumbVertical">@drawable/scrollbar_thumb</item> 
    <item name="android:scrollbarTrackVertical">@drawable/scrollbar_track</item> 
    <item name="android:scrollbarSize">8dp</item> 
    <item name="android:scrollbarFadeDuration">800</item> 
    <item name="android:scrollbarDefaultDelayBeforeFade">500</item> 
</style> 

最後に、があなたのrecyclerviewに
style="@style/scrollbar_style"
を追加し、あなたのrecyclerviewでスクロールバーにこのスタイルを適用します。あなたのケースでは

:あなたは、実行時に色を変更する必要がある場合

<android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/recyclerView" 
    style="@style/scrollbar_style" 
    android:layout_below="@id/my_toolbar" 
    android:layout_above="@+id/progressBar" 
    android:scrollbars="vertical" 
/> 
+1

に感謝kaaloraat、それも動作しますが、上記のソリューションは、初心者の方が良いです。 – Yass

+0

それほど複雑でもありません。 – kaaloraat

+0

よく詳しい答え! –

1

、これは方法です。同じprincibleはScrollViewとListViewコントロールのような他のビューで動作するはずですので

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.PorterDuff; 
import android.graphics.drawable.Drawable; 
import android.support.annotation.ColorInt; 
import android.support.v7.widget.RecyclerView; 
import android.util.AttributeSet; 

/** 
* Created on 22.3.2016. 
* 
* @author Bojan Kseneman 
* @description A recycler view that will draw the scroll bar with a different color 
*/ 
public class CustomScrollBarRecyclerView extends RecyclerView { 

    private int scrollBarColor = Color.RED; 

    public CustomScrollBarRecyclerView(Context context) { 
     super(context); 
    } 

    public CustomScrollBarRecyclerView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomScrollBarRecyclerView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void setScrollBarColor(@ColorInt int scrollBarColor) { 
     this.scrollBarColor = scrollBarColor; 
    } 

    /** 
    * Called by Android {@link android.view.View#onDrawScrollBars(Canvas)} 
    **/ 
    protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar, int l, int t, int r, int b) { 
     scrollBar.setColorFilter(scrollBarColor, PorterDuff.Mode.SRC_ATOP); 
     scrollBar.setBounds(l, t, r, b); 
     scrollBar.draw(canvas); 
    } 

    /** 
    * Called by Android {@link android.view.View#onDrawScrollBars(Canvas)} 
    **/ 
    protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar, int l, int t, int r, int b) { 
     scrollBar.setColorFilter(scrollBarColor, PorterDuff.Mode.SRC_ATOP); 
     scrollBar.setBounds(l, t, r, b); 
     scrollBar.draw(canvas); 
    } 
} 

これらのメソッドは、Viewクラスで定義されています。

+0

こんにちはそれは動作しますが、親指の色だけを変更します。スクロールバーのトラック色も変更できますか? – Shruti

関連する問題