2016-02-15 20 views
20

Làm cách nào để thay đổi màu của thanh cuộn trong recyclerView?Màu thanh cuộn trong RecyclerView

Tôi có thanh cuộn nhưng tôi muốn thay đổi màu của nó. recyclerView của tôi là như thế này:

<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" 
    /> 

Trả lời

31

Bạn có thể làm điều này bằng cách đưa vào dòng mã sau đây trong Recyclerview bạn

android:scrollbarThumbVertical="@drawable/yoursdrawablefile

Các tập tin có thể vẽ được trong trường hợp của tôi là:

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

hoạt động, cảm ơn rất nhiều! – Yass

+0

chào mừng ........ – Naresh

+0

tuyệt vời !!!!!!!! –

7

Trong trường hợp bạn muốn thêm kiểu dáng thanh cuộn của mình, hãy tạo hai tệp tài nguyên có thể kéo trong thư mục drawable của bạn dưới dạng 1. scrollbar_track và 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> 

Bây giờ, tạo ra một phong cách đặt tên scrollbar_style trong tập tin styles.xml của bạn như:

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

Cuối cùng, để áp dụng phong cách này để thanh cuộn trong recyclerview của bạn, thêm
style="@style/scrollbar_style"
để recyclerview của bạn.

Trong trường hợp của bạn:

<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

cảm ơn kaaloraat, nó hoạt động quá nhưng giải pháp trên là tốt hơn cho người mới bắt đầu. – Yass

+0

OK. Nó không phức tạp quá. – kaaloraat

+0

Câu trả lời chi tiết! –

1

Nếu bạn cần thay đổi màu sắc trong thời gian chạy, đây là đường đi.

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

Các phương pháp này được xác định trong lớp Chế độ xem, vì vậy cùng một nguyên tắc sẽ hoạt động với các chế độ xem khác như ScrollView và ListView.

+0

Xin chào nó hoạt động nhưng chỉ thay đổi màu ngón tay cái, chúng ta có thể thay đổi màu sắc của thanh cuộn không? – Shruti

Các vấn đề liên quan