简体   繁体   English

如何在RecyclerView的适配器的onBindViewHolder中使用滚动侦听器?

[英]How to use scroll listener in onBindViewHolder in Adapter of a RecyclerView?

I have a long itemView in RecyclerView adapter and what I want is a listener to check if a view in itemView is visible or not, while scrolling recyclerView. 我在RecyclerView适配器中有一个很长的itemView,我想要的是一个侦听器,以在滚动recyclerView时检查itemView中的视图是否可见。 What I need is somthing like "onRecyclerViewScrolled()" in this example code: 在此示例代码中,我需要的是“ onRecyclerViewScrolled()”之类的东西:

    public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {


    @Override
    public void onBindViewHolder(@NonNull final ViewHolder myViewHolder, int
            position) {

      onRecyclerViewScrolled() {
        if (!isVisible(myViewHolder.myView)) {
          //do something
        }
      }

    }

  }

  public static boolean isVisible(final View view) {
    if (view == null) {
      return false;
    }
    if (!view.isShown()) {
      return false;
    }
    final Rect actualPosition = new Rect();
    view.getGlobalVisibleRect(actualPosition);
    final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight());
    return actualPosition.intersect(screen);
  }

  public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
  }

  public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;
  }

You can set addOnScrollListener on your RecyclerView to get notified when the user scrolls, and you can get the first/last visible views from LinearLayoutMangar or GridLayoutManager : 您可以在RecyclerView上设置addOnScrollListener以便在用户滚动时得到通知,并且可以从LinearLayoutMangarGridLayoutManager获取第一个/最后一个可见视图:

        RecyclerView recycler = findViewById(R.id.recycler_view);
        reycler.setAdapter(YOUR_ADAPTER);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        recycler.setLayoutManager(layoutManager);

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

                //First visible item position.
                int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
                //Last visible item position.
                int lastVisiblePosition = layoutManager.findLastVisibleItemPosition();



               /*If you really need to access your visible views,
                 Loop through all the visible views*/.
                for (int position = firstVisiblePosition; position == lastVisiblePosition; position++) {
                    View lastVisibleView = layoutManager.getChildAt(firstVisiblePosition);
                    //do something
                }

                dataList.get(firstVisiblePosition);

            }
        });

More info from documentation RecyclerView.OnScrollListener have to functions: 文档RecyclerView.OnScrollListener更多信息必须具有以下功能:

onScrolled(RecyclerView recyclerView, int dx, int dy) Callback method to be invoked when the RecyclerView has been scrolled. onScrolled(RecyclerView recyclerView,int dx,int dy)滚动RecyclerView时要调用的回调方法。

onScrollStateChanged(RecyclerView recyclerView, int newState) Callback method to be invoked when RecyclerView's scroll state changes. onScrollStateChanged(RecyclerView recyclerView,int newState)在RecyclerView的滚动状态更改时要调用的回调方法。

Apparently, onScrolled is called when the scroll is completed. 显然,滚动完成时将调用onScrolled

For more info see Yigit answer`s 有关更多信息,请参见Yigit answer`s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM