簡體   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