我要禁用循环服务视图滚动。为此,我试图通过扩展MyRecyclerView来定义自定义RecyclerView
myrecyclerview.java

package com.example;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;

class MyRecyclerView extends RecyclerView {
    private boolean scrollable = true;

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

    public void setScrollingEnabled(boolean scrollable) {
        this.scrollable = scrollable;
    }

    public boolean isScrollable() {
        return scrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if we can scroll pass the event to the superclass
                if (scrollable) return super.onTouchEvent(ev);
                // only continue to handle the touch event if scrolling enabled
                return scrollable; // scrollable is always false at this point
            default:
                return super.onTouchEvent(ev);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Don't do anything with intercepted touch events if
        // we are not scrollable
        if (!scrollable) return false;
        else return super.onInterceptTouchEvent(ev);
    }
}

布局文件
<?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="match_parent"
    android:background="@color/final_gray_bg" >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.example.MyRecyclerView
            android:id="@+id/sblistview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null"
            android:fadingEdge="none"
            android:gravity="center"
            android:horizontalSpacing="@dimen/d1"
            android:verticalSpacing="@dimen/d1" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

错误日志
07-01 14:15:01.330: E/AndroidRuntime(27649): FATAL EXCEPTION: main
07-01 14:15:01.330: E/AndroidRuntime(27649): android.view.InflateException: Binary XML file line #12: Error inflating class com.example.MyRecyclerView
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.LayoutInflater.createView(LayoutInflater.java:603)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at miui.dexspy.DexspyInstaller.invokeOriginalMethodNative(Native Method)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at miui.dexspy.DexspyInstaller.invokeOriginalMethod(DexspyInstaller.java:491)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at miui.dexspy.DexspyInstaller.handleHookedMethod(DexspyInstaller.java:405)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.LayoutInflater.inflate(Native Method)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at com.example.SBNew.onCreateView(SBNew.java:59)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.View.measure(View.java:15891)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.View.measure(View.java:15891)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5035)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.View.measure(View.java:15891)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5035)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.View.measure(View.java:15891)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5035)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.View.measure(View.java:15891)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5035)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.View.measure(View.java:15891)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5035)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2208)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.View.measure(View.java:15891)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1957)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1156)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1336)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1056)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5542)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.Choreographer.doFrame(Choreographer.java:532)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.os.Handler.handleCallback(Handler.java:730)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.os.Looper.loop(Looper.java:137)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at android.app.ActivityThread.main(ActivityThread.java:5162)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 14:15:01.330: E/AndroidRuntime(27649):    at java.lang.reflect.Method.invoke(Metho

我做错什么了?
任何帮助都将不胜感激。谢谢

最佳答案

您还需要实现其他构造函数:

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

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

您正在通过XML创建MyRecyclerView,因此需要带有attributeSet参数的构造函数。

07-27 13:29