简体   繁体   English

如何检测特定项目何时到达Android中的RecyclerView?

[英]How do I detect when a specific item reaches top of a RecyclerView in Android?

I'm creating an app with a list of questions in a RecyclerView. 我正在使用RecyclerView创建一个包含问题列表的应用程序。 In the same parent layout, I have a static TextView above the RecyclerView that displays instruction for a set of questions. 在相同的父布局中,我在RecyclerView上方有一个静态TextView,它显示一组问题的说明。 What I want is to change the text in the TextView whenever a specific question reaches the top of the RecyclerView when scrolled. 我想要的是在滚动时每当特定问题到达RecyclerView的顶部时更改TextView中的文本。 How can I accomplish this? 我怎么能做到这一点?

You would need to listen to the scrolling of your RecyclerView and get the first item displayed onto the screen. 您需要收听RecyclerView的滚动并将第一个项目显示在屏幕上。

Thanksfully, Android let us set a custom ScrollListener to a RecyclerView and with your LayoutManager you are able to know which item is shown first : 谢天谢地,Android让我们将自定义ScrollListener设置为RecyclerView,使用LayoutManager,您可以知道首先显示哪个项目:

// initialize your RecyclerView and your LayoutManager
mLinearLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLinearLayoutManager);

// set a custom ScrollListner to your RecyclerView
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
           // Get the first visible item
           int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();

           //Now you can use this index to manipulate your TextView

        }
    });

EDIT : added Mateus Gondim clearance about the LayoutManager 编辑:添加关于LayoutManager的Mateus Gondim清除

You should override the default Recycler.OnScrollListener. 您应该覆盖默认的Recycler.OnScrollListener。 Have a look at this Code : https://gist.github.com/mipreamble/b6d4b3d65b0b4775a22e 看看这个代码: https//gist.github.com/mipreamble/b6d4b3d65b0b4775a22e

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

相关问题 Android:添加项目后如何返回到RecyclerView的顶部? - Android: how do I return to to top of RecyclerView after adding item? 如何检测用户何时滚动到RecyclerView中的最顶层项目 - How to detect when user have scrolled to the top most item in a RecyclerView 如何检测android RecyclerView中的OverScroll? - How do I detect OverScroll in android RecyclerView? 如何在向下滚动时扩展AppBarLayout到达RecyclerView的顶部 - How to expand AppBarLayout when scrolling down reaches at top of the RecyclerView 用户到达终点时如何实现 RecyclerView 项目拉伸效果? - How to achieve RecyclerView item stretch effect when user reaches end? 在顶部进行第一项装饰后,如何裁剪recyclerView - How do I clip recyclerView after first item decoration at the top Android - 检测 RecyclerView 中的最后一个项目何时可见 - Android - Detect when the last item in a RecyclerView is visible 如何检测列表视图的特定项目到达屏幕顶部? - How can I detect specific item of listview reach the top of the screen? 如何更改 RecyclerView 中特定项目 onClick 的背景? - How do I change the background of a specific item onClick in a RecyclerView? 如何以编程方式检测项目何时在 RecyclerView 中可见? - How to programmatically detect when an item is visible in a RecyclerView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM