2016-02-15 19 views
20

मैं अपने स्क्रॉलबार का रंग पुनर्नवीनीकरण में कैसे बदल सकता हूं?रीसाइक्लर में स्क्रॉलबार रंग देखें

मेरे पास स्क्रॉलबार है लेकिन मैं इसका रंग बदलना चाहता हूं।

<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

मेरे मामले में drawable फ़ाइल है::

<?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

कमाल !!!!!!!! –

7
मेरे recyclerView इस तरह है

यदि आप अपने स्क्रॉलबार को और स्टाइल करना चाहते हैं, तो अपने ड्रॉइंग फ़ोल्डर में दो ड्रॉइंग संसाधन फ़ाइल बनाएं 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 में स्क्रॉलबार को लागू अपने recyclerview को
style="@style/scrollbar_style"
जोड़ने के लिए।

आपके मामले में:

<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

आप क्रम में रंग बदलने के लिए की जरूरत है, इस तरह से है।

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); 
    } 
} 

इन विधियों देखें कक्षा में परिभाषित कर रहे हैं तो एक ही princible scrollview और ListView जैसे अन्य विचारों का काम करना चाहिए।

+0

हैलो यह काम करता है लेकिन केवल अंगूठे रंग बदलता है, क्या हम स्क्रॉलबार ट्रैक रंग भी बदल सकते हैं? – Shruti

संबंधित मुद्दे