简体   繁体   English

如何使用 Snaphelper 更改中心元素的背景颜色

[英]How to change background color of center element using Snaphelper

I want to change the background color of the centered position element.我想更改居中位置元素的背景颜色。 I have used vertical snapHelper and my center element is also detected so I just want to add background color to the center element.我使用了垂直 snapHelper 并且我的中心元素也被检测到,所以我只想为中心元素添加背景颜色。

LinearSnapHelper snapHelper = new LinearSnapHelper() {

        @Override
        public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
            View centerView = findSnapView(layoutManager);
            if (centerView == null)
                return RecyclerView.NO_POSITION;

            int position = layoutManager.getPosition(centerView);
            int targetPosition = -1;
            if (layoutManager.canScrollHorizontally()) {
                if (velocityX < 0) {
                    targetPosition = position - 1;

                } else {
                    targetPosition = position + 1;
                }
            }

            if (layoutManager.canScrollVertically()) {
                if (velocityY < 0) {
                    targetPosition = position - 1;
                } else {
                    targetPosition = position + 1;
                    centerView.getBackground().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.DARKEN);
                }
            }

            final int firstItem = 0;
            final int lastItem = layoutManager.getItemCount() - 1;
            targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem));
            return targetPosition;
        }
    };

}

You can register scroll listener to the RecyclerView , and when the scroll is over (ie it's in SCROLL_STATE_IDLE state);您可以将滚动侦听器注册到RecyclerView ,并在滚动结束时(即处于SCROLL_STATE_IDLE状态); the snapHelper.findSnapView() shouldn't return a null value; snapHelper.findSnapView()不应返回null值; so you can set its color.所以你可以设置它的颜色。

final LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        // Detect scroll end
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {

            // Resetting all the loaded items to the default color 
            int firstVisibleItemPosition =
                    layoutManager.findFirstVisibleItemPosition();
            int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
            for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
                RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(i);
                // Rest to the default color of other loaded items
                holder.itemView.setBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.defaultColor, null));
            }

            // Changing the background of the snaphelper centered item
            View centerView = snapHelper.findSnapView(layoutManager);
            if (centerView != null)
                centerView.setBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.centerColor, null));
        }

    }
});

Kotlin:科特林:

val layoutManager = recyclerView.layoutManager as LinearLayoutManager
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
        
        // Detect scroll end
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            val firstVisibleItemPosition =
                layoutManager.findFirstVisibleItemPosition()
            val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
            for (i in firstVisibleItemPosition..lastVisibleItemPosition) {
                val holder =
                    recyclerView.findViewHolderForAdapterPosition(i) 

                // Resetting all the loaded items to the default color 
                 holder!!.itemView!!.setBackgroundColor(ResourcesCompat.getColor(resources, R.color.defaultColor, null))
            }

            // Changing the background of the snaphelper centered item
            val centerView: View? = snapHelper.findSnapView(layoutManager)
            centerView.setBackgroundColor(ResourcesCompat.getColor(resources, R.color.centerColor, null))

        }

    }
})

UPDATE:更新:

There is a better approach than iterating over all the RV loaded items, to reset to the default color;有一种比遍历所有 RV 加载的项目更好的方法来重置为默认颜色; by just saving the previous centeredView into a local field, and update its color instead of updating the entire loaded items:只需将之前的 centralView 保存到本地字段中,并更新其颜色而不是更新整个加载的项目:

View previousCenteredView;
final LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        // Detect scroll end
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {

            // Changing the background of the snaphelper centered item
            View centerView = snapHelper.findSnapView(layoutManager);
            previousCenteredView = centerView;
            
            if (centerView != null)
                centerView.setBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.centerColor, null));

        } else if (previousCenteredView != null){
            // Resetting previous centered view to the default color 
            previousCenteredView.setBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.defaultColor, null));
            previousCenteredView = null;
        }

    }
});

Kotlin:科特林:

var previousCenteredView: View? = null
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
        
        // Detect scroll end
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {

            // Changing the background of the snaphelper centered item
            val centerView: View? = snapHelper.findSnapView(layoutManager)
            previousCenteredView = centerView
            centerView.setBackgroundColor(ResourcesCompat.getColor(resources, R.color.centerColor, null))
            
        } else if (previousCenteredView != null){
            // Resetting previous centered view to the default color 
            previousCenteredView.setBackgroundColor(ResourcesCompat.getColor(resources, R.color.defaultColor, null))
            previousCenteredView = null
        }


    }
})

for setting background color of a view, you can use various methods check this 要设置视图的背景色,可以使用多种方法进行检查

based on you code you are setting the background color of the view but only inside the if statement, so try extracting the line to be after the if . 根据您的代码,您正在设置视图的背景颜色,但仅在if语句内部,因此请尝试提取位于if之后的行。

...
if (layoutManager.canScrollVertically()) {
      if (velocityY < 0) {
            targetPosition = position - 1;
      } else {
            targetPosition = position + 1;
      }
}

centerView.getBackground().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.DARKEN);

final int firstItem = 0;
final int lastItem = layoutManager.getItemCount() - 1;
targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem));
return targetPosition;
...

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

相关问题 带有 snaphelper 的水平回收器视图,如何将第一个和最后一个元素居中? - Horizontal recyclerview with snaphelper, how to center first and last element? RecyclerView SnapHelper-如何更改捕捉和未捕捉项目的背景? - RecyclerView SnapHelper - how to change background of snapped and unsnapped items? MPAndroidChart - 如何更改PieChart中心背景的颜色? - MPAndroidChart - How to change the color of PieChart center Background? 如何使用ActionBarSherlock更改菜单的背景颜色 - how to change background color of menu using ActionBarSherlock 如何使用微调框更改xml的背景色 - How to change background color of xml by using spinner 如何在RecyclerView元素中更改标题背景颜色onClick? - How to change header background color onClick in a RecyclerView element? 如何在Android中更改通知栏的背景颜色,即“元素颜色和文本颜色”? - How to change the background color i.e. “element color and text color” of notification bar in Android…? 如何在 RecyclerView 中使用 SnapHelper - How to use SnapHelper with RecyclerView 如何更改FAB背景颜色 - How to change FAB background color 如何更改列表视图的背景颜色? - how to change the background color of a listview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM