繁体   English   中英

如何在 RecyclerView 中突出显示完全可见的项目视图

[英]How to Highlight the Competely visible item view in a RecyclerView

我在cardView 中有一个imageView 和一个textView。
cardView 的 alpha 设置为 .5f。
cardView 用于垂直 recyclerView。
我在这里尝试做的是,当用户滚动 reyclerView 时,完全可见的 cardView 的 alpha 应始终更改为 1f,而对于非完全可见的 cardViews,alpha 保持为 0.5f。
一次只有一个完全可见的 cardView。

这是我尝试过但不起作用的方法。

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

      int center = recyclerView.getHeight() / 2;
      View centerView = recyclerView.findChildViewUnder( recyclerView.getTop(), center);
      int centerPos = recyclerView.getChildAdapterPosition(centerView);

       if (prevCenterPos != centerPos) {
            // dehighlight the previously highlighted view
            View prevView = 
           recyclerView.getLayoutManager().findViewByPosition(prevCenterPos);
           if (prevView != null) {
               prevView.setAlpha(.5f);
           }

           // highlight view in the middle
           if (centerView != null) {
                prevView.setAlpha(1f);
           }

           prevCenterPos = centerPos;
        }

}

当你用这个找到中心点时

int center = recyclerView.getHeight() / 2;

这不是您应该使用的正确方法:

mLayoutManager.findFirstCompletelyVisibleItemPosition()

尝试这个:

val firstCompelteVisible = mLayoutManager.findFirstCompletelyVisibleItemPosition()
val centerView =
    recyclerView.layoutManager!!.findViewByPosition(firstCompelteVisible)
if (prevCenterPos != centerPos) {
    // dehighlight the previously highlighted view
    val prevView =
        recyclerView.layoutManager!!.findViewByPosition(prevCenterPos)
    if (prevView != null) {
        prevView.alpha = .5f
    }

    // highlight view in the middle
    if (centerView != null) {
        prevView!!.alpha = 1f
    }
    prevCenterPos = centerPos
}

我希望这会奏效。

暂无
暂无

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

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