在Android中滚动水平RecyclerView时如何更改背景图像

时间:2018-11-20 11:04:24

标签: java android android-studio android-layout android-recyclerview

我想基于[recyclerview的更改来更改布局的背景图像。当我滚动recyclerview时,在更改状态下,我想像https://www.pinterest.com/pin/860891284992684260/

一样平滑更改布局的背景图像

这是我的代码:

//更新后的代码

 @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            int firstPos = HorizontalLayout.findFirstVisibleItemPosition();
            int lastPos = HorizontalLayout.findLastVisibleItemPosition();
            int middle = Math.abs(lastPos - firstPos) / 2 + firstPos;
            View centerView = snapHelper.findSnapView(HorizontalLayout);
            final int pos = HorizontalLayout.getPosition(centerView);
            AllStories myStory = Stories.get(HorizontalLayout.getPosition(centerView));
           // int selectedPos = -1;
            for (int i = 0; i < RecyclerViewHorizontalAdapter.getItemCount(); i++) {
                if (i == middle) {
                    if (myStory != null) {
                        if(dx>5)
                        { background.setBackgroundResource(myStory.BackGroundImage);
                            Animation fadeOut = new AlphaAnimation(1, 0);
                            fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
                            fadeOut.setStartOffset(1000);
                            fadeOut.setDuration(1000);

                            AnimationSet animation = new AnimationSet(false); //change to false
                            animation.addAnimation(fadeOut);
                            background.setAnimation(animation);
                        }
                        background.setBackgroundResource(myStory.BackGroundImage);
                        //selectedPos = i;
                        LockStories(pos);
                    }
                } else {

                }
            }



        }
    });

1 个答案:

答案 0 :(得分:0)

更改图像时,请尝试使用fadeIn和fadeout动画。