自定义RecyclerView渲染问题

时间:2015-07-10 10:05:26

标签: android android-recyclerview

我正在努力制作自定义弹性RecyclerView,所以我破解了一些代码......

package ...;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;

public class BounceRecyclerView extends RecyclerView {
    private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;

    private Context mContext;
    private int mMaxYOverscrollDistance;

    BounceRecyclerView(Context context){
        super(context);
        mContext = context;
        initBounceRecyclerView();
    }

    BounceRecyclerView(Context context, AttributeSet attrs){
        super(context, attrs);
        mContext = context;
        initBounceRecyclerView();
    }

    BounceRecyclerView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        mContext = context;
        initBounceRecyclerView();
    }

    private void initBounceRecyclerView()
    {
        //get the density of the screen and do some maths with it on the max overscroll distance
        //variable so that you get similar behaviors no matter what the screen size

        final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        final float density = metrics.density;

        mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE * 0.25);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY,
                                   int scrollRangeX, int scrollRangeY, int maxOverScrollX,
                                   int maxOverScrollY, boolean isTouchEvent)
    {
        //This is where the magic happens, we have replaced the incoming maxOverScrollY with
        // our own custom variable mMaxYOverscrollDistance;
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY,
                maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);
    }
}

当我尝试在布局文件中使用此类时,我收到此错误:

The following classes could not be instantiated:
- ....BounceRecyclerView (Open Class, Show Exception, Clear Cache)
 Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE

这是堆栈的主要部分:

Exception Details java.lang.NoSuchMethodException: ....BounceRecyclerView.<init>(android.content.Context, android.util.AttributeSet)

在我看来,像构造函数(android.content.Context,android.util.AttributeSet)参数找不到,但正如你可以看到它存在...

另外,当我在Android Studio中打开BounceRecyclerView.java时,构造函数是灰色和弯曲的下划线,表示它们没有被使用。

0 个答案:

没有答案