如何在片段中实现OnScrollistener

时间:2015-08-07 19:59:00

标签: android android-recyclerview onscrolllistener

我似乎无法为我的recyclerview实现/扩展OnScrollistener以了解recyclelerview何时触及底部

public class UpcomingFragment extends Fragment 

如果符合以下条件我如何实施:

implements RecyclerView.OnScrollListener

implements OnScrollListener

给我一​​个“预期接口”错误

我的构建gradle模块:

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "company.com.movieapp"
    minSdkVersion 15
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
 }
}

构建成绩项目

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

}

2 个答案:

答案 0 :(得分:4)

基于文档:

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.OnScrollListener.html

RecyclerView.OnScrollListenerabstract

使用这样的监听器:

private final RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
        // code
    }

    @Override
    public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
        // code
    }
};

添加到RecyclerView

recyclerView.addOnScrollListener(onScrollListener);

答案 1 :(得分:0)

private final RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
    // code
}

@Override
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
    // code
}
};

然后将onScrollListener添加到方法addOnScrollListener中

recyclerView.addOnScrollListener(onScrollListener);