本文介绍了如何在Android RecyclerView中检测OverScroll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试覆盖onOverScrolled(),但未触发:

I have tried to overwrite onOverScrolled() but it is not triggered:

public class MyRecyclerView extends RecyclerView {
    public MyRecyclerView(@NonNull Context context) {
        super(context);
    }

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

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

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        Toast.makeText(getContext(), "overScrolled", Toast.LENGTH_SHORT).show();
    }
}

我的RecyclerView具有 recyclerView.setOverScrollMode(View.OVER_SCROLL_ALWAYS);

My RecyclerView has recyclerView.setOverScrollMode(View.OVER_SCROLL_ALWAYS);

推荐答案

尝试使用此方法查找 底部过度滚动 顶部过度滚动

Try this to find bottom overscroll and top overscroll

import android.os.Bundle;
import java.util.ArrayList;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {

    RecyclerView myRecyclerView;
    ArrayList<String> arrayList = new ArrayList<>();
    DataAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myRecyclerView = findViewById(R.id.myRecyclerView);

        LinearLayoutManager linearLayoutManager= new LinearLayoutManager(this){
            @Override
            public int scrollVerticallyBy ( int dx, RecyclerView.Recycler recycler, RecyclerView.State state ) {
                int scrollRange = super.scrollVerticallyBy(dx, recycler, state);
                int overScroll = dx - scrollRange;
                if (overScroll > 0) {
                    Utils.printLog("NILU_PILU :-> BOTTOM OVERSCROLL");
                } else if (overScroll < 0) {
                    Utils.printLog("NILU_PILU :-> TOP OVERSCROLL");
                }
                return scrollRange;
            }
        };
        myRecyclerView.setLayoutManager(linearLayoutManager);
        myRecyclerView.setHasFixedSize(true);

        addDataToList();

        adapter = new DataAdapter(this, arrayList);
        myRecyclerView.setAdapter(adapter);

    }

    private void addDataToList() {
        for (int i = 0; i < 50; i++) {
            arrayList.add("NILU_PILU :-> " + i);
        }
    }

}
 myRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (dy > 0) {

                int pos = linearLayoutManager.findLastVisibleItemPosition();
                int numItems = myRecyclerView.getAdapter().getItemCount();

                if (pos >= numItems - 1 ) {
                    Utils.printLog("NILU_PILU :-> BOTTOM OVERSCROLL");
                }
            }
        }
    });

这篇关于如何在Android RecyclerView中检测OverScroll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:25