简体   繁体   中英

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). 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. 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. I updated the Listener as so:

//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);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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