简体   繁体   English

在RecyclerView中捕捉SmoothScroll的动画事件[Android]

[英]Catch Animation Event of SmoothScroll in RecyclerView [Android]

I have a GridLayout RecyclerView with a PageSnapHelper attached that essentially acts like a Vertical Linear RecyclerView (I inherited the code, not sure why this was done). 我有一个带有PageSnapHelper的GridLayout RecyclerView,它基本上就像一个Vertical Linear RecyclerView(我继承了代码,不知道为什么这样做)。 The goal is to highlight the item currently centered in the view. 目标是突出显示当前居中在视图中的项目。 Scrolling is done from code driven by two menu items to simulate going forward and backward from a remote. 滚动是由两个菜单项驱动的代码完成的,以模拟远程前进和后退。 Scrolling of the view works fine. 滚动视图效果很好。 The issue seems to be that when I listen for the end of scrolling event there is animation going on which messes up my code to draw the highlight around the item. 问题似乎是当我听到滚动事件的结束时,会出现动画,这会影响我的代码以在项目周围绘制突出显示。 Here's the code I use to scroll the view based on the menu item: 这是我用来根据菜单项滚动视图的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    //Get which arrow was pressed
    int id = item.getItemId();
   //Get count of items
    int itemCount = recyclerView.getAdapter().getItemCount();



    //Condition to moving down the list
    if (id == R.id.next) {
        //Make sure we're within bounds of the of the view from the top
        if(count>=itemCount-1) {
            return super.onOptionsItemSelected(item);
        }
        //Increment to count to next ViewHolder
        count++;
    //Condition for moving up list
    } else if (id == R.id.prev) {
        //Make sure we're within bounds
        if(count == 0){
            return super.onOptionsItemSelected(item);
        }
        //Decremennt to previous ViewHolder
        count--;
    }

    //Scroll the RecyclerView to the position we want
    layoutManager.setIsNext(id == R.id.next);
    recyclerView.smoothScrollToPosition(count);

    //If the item is already in view, then no scroll event is necessary and we can access the ViewHolder
    HighlightViewHolder(count);

    //In the event that the ViewHolder is off-screen listen for the end of the scrolling event to ensure the ViewHolder
    //is created and in correct position on the screen
    RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {

        //Add a listener to that looks out for when the scrolling is complete
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            Log.i("ScrollListener", "Scrolling has ended");
            HighlightViewHolder(count);
            recyclerView.removeOnScrollListener(this);

        }
    };

    recyclerView.addOnScrollListener(scrollListener);

    return super.onOptionsItemSelected(item);

When the app first begins and I the forward button, the first item is highlighted just fine: 当应用程序第一次开始并且我是前进按钮时,第一项突出显示正常: 在此输入图像描述

On the next selection, which engages a scrolling event is where I have an issue: 在接下来的选择中,参与滚动事件是我遇到问题的地方: 在此输入图像描述

What I would expect to happen is that when the scrolling event has ended, the item is in its centered position. 我期望发生的是当滚动事件结束时,项目处于其居中位置。 Based on what I'm seeing I think there's some animation event that's still happening that I should be looking for the completion of. 基于我所看到的,我认为有一些动画事件仍在发生,我应该寻找完成。 My highlighting code relies on the view in question to be in its expected location. 我的突出显示代码依赖于所讨论的视图位于其预期位置。 I'm new to android and my searches for RecyclerView animations brought me to ItemAnimator. 我是android的新手,我对RecyclerView动画的搜索把我带到了ItemAnimator。 But reading the docs I'm not sure if this is what I'm looking for. 但阅读文档,我不确定这是否是我正在寻找的。 Any ideas? 有任何想法吗?

I couldn't find any events related to what I was looking for so I went ahead and used a Handler to wait an arbitrary amount of time before drawing the highlight. 我找不到任何与我正在寻找的事件相关的事件所以我继续使用Handler等待任意时间,然后再绘制高亮显示。 I updated the Listener as so: 我更新了Listener,如下所示:

//Add a listener to that looks out for when the scrolling is complete
            @Override
            public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                Handler handler = new Handler();
                Runnable task = new Runnable() {
                    @Override
                    public void run() {
                        HighlightViewHolder(count);
                    }
                };

                handler.postDelayed(task, 500);
                recyclerView.removeOnScrollListener(this);
            }

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

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