![](/img/trans.png)
[英]Horizontal recyclerview with snaphelper, how to center first and last element?
[英]How to change background color of center element using Snaphelper
我想更改居中位置元素的背景顏色。 我使用了垂直 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;
}
};
}
您可以將滾動偵聽器注冊到RecyclerView
,並在滾動結束時(即處於SCROLL_STATE_IDLE
狀態); snapHelper.findSnapView()
不應返回null
值; 所以你可以設置它的顏色。
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));
}
}
});
科特林:
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))
}
}
})
更新:
有一種比遍歷所有 RV 加載的項目更好的方法來重置為默認顏色; 只需將之前的 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;
}
}
});
科特林:
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
}
}
})
要設置視圖的背景色,可以使用多種方法進行檢查
根據您的代碼,您正在設置視圖的背景顏色,但僅在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.