繁体   English   中英

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

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

我有一个带有PageSnapHelper的GridLayout RecyclerView,它基本上就像一个Vertical Linear RecyclerView(我继承了代码,不知道为什么这样做)。 目标是突出显示当前居中在视图中的项目。 滚动是由两个菜单项驱动的代码完成的,以模拟远程前进和后退。 滚动视图效果很好。 问题似乎是当我听到滚动事件的结束时,会出现动画,这会影响我的代码以在项目周围绘制突出显示。 这是我用来根据菜单项滚动视图的代码:

@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);

当应用程序第一次开始并且我是前进按钮时,第一项突出显示正常: 在此输入图像描述

在接下来的选择中,参与滚动事件是我遇到问题的地方: 在此输入图像描述

我期望发生的是当滚动事件结束时,项目处于其居中位置。 基于我所看到的,我认为有一些动画事件仍在发生,我应该寻找完成。 我的突出显示代码依赖于所讨论的视图位于其预期位置。 我是android的新手,我对RecyclerView动画的搜索把我带到了ItemAnimator。 但阅读文档,我不确定这是否是我正在寻找的。 有任何想法吗?

我找不到任何与我正在寻找的事件相关的事件所以我继续使用Handler等待任意时间,然后再绘制高亮显示。 我更新了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