使用android.support.v7.widget.RecyclerView的自定义实现时没有预览

时间:2019-01-15 15:28:26

标签: android android-recyclerview android-support-library

如果我在布局中添加了RecyclerView属性的tools:listitem,则可以在Android Studio中获得正确的预览。

一旦包含派生的RecyclerView(下面的示例),我将无法再获得预览。 我尝试禁用所有自定义代码,因此自定义实现仅充当支持库中RecyclerView的包装,即使这样,预览也不可用。

有没有办法让此自定义RecyclerView实现的预览工作正常?

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

import com.sandbox.myapp.R;

/**
 * Custom implementation of the {@link RecyclerView}, which allows us to change the scrollbar
 * color at runtime.
 */
public final class CustomRecyclerView extends RecyclerView {

    private int scrollBarColor;

    public CustomRecyclerView(Context context) {
        super(context);
        scrollBarColor = ContextCompat.getColor(context, R.color.blue);
    }

    public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        scrollBarColor = ContextCompat.getColor(context, R.color.blue);
    }

    public CustomRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        scrollBarColor = ContextCompat.getColor(context, R.color.blue);
    }

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

    /**
     * Called by Android {@link android.view.View#onDrawScrollBars(Canvas)}
     */
    @SuppressWarnings("unused")
    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)}
     */
    @SuppressWarnings("unused")
    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);
    }
}

活动布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_grey"
    android:orientation="vertical">

    <include layout="@layout/layout_top_bar" />

    <com.sandbox.myapp.custom.views.CustomNestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/ok_button"
        android:layout_below="@id/top_bar"
        android:layout_margin="@dimen/question_margin"
        android:scrollbars="vertical">

        <RelativeLayout
            android:id="@+id/scrollViewContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include layout="@layout/question"/>

            <com.sandbox.myapp.custom.views.CustomRecyclerView
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/recyclerView"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/questionTextView"
                tools:itemCount="5"
                tools:listitem="@layout/row_hyperlink"/>

            <include layout="@layout/question_error"
                android:layout_below="@id/recyclerView"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content" />

        </RelativeLayout>

    </com.sandbox.myapp.custom.views.CustomNestedScrollView>

    <include layout="@layout/ok_button"/>

</RelativeLayout>

Recyclerview项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/view_distance"
    android:layout_marginTop="@dimen/view_distance"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/shareIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@drawable/url_browser" />

    <com.sandbox.myapp.custom.views.CustomTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_toRightOf="@id/shareIcon"
        android:textColor="@color/defaultTheme"
        android:text="Hyperlink"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试一下:

public CustomRecyclerView(Context context) {
        super(context);
    if (isInEditMode()) {
        return;
    }
    crollBarColor = ContextCompat.getColor(context, R.color.blue);
}