I am trying to implement a horizontal recycleview with right and left arrow indicators. So what happens is if one clicks the right arrow next item should appear and if one clicks the left arrow the previous item should appear and also at the end of the list the left arrow should disappear. I have no Idea how ti implement this. can someone help me out? Below is my Horizontal Recyclerview adapter.
Environment.getExternalStorageDirectory().toString()
}
Thanks in advance.
答案 0 :(得分:5)
尝试以下
这里的img_LeftScroll是左侧imageview,而img_right_scroll是水平列表之间的右侧imageview,rv_horizontal是horizontallist视图
然后单击图像视图,执行以下操作,希望它可以正常工作
img_LeftScroll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (horizontalLayoutManagaer.findFirstVisibleItemPosition() > 0) {
rv_horizontal.smoothScrollToPosition(horizontalLayoutManagaer.findFirstVisibleItemPosition() - 1);
} else {
rv_horizontal.smoothScrollToPosition(0);
}
}
});
img_right_scroll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rv_horizontal.smoothScrollToPosition(horizontalLayoutManagaer.findLastVisibleItemPosition() + 1);
}
});
答案 1 :(得分:2)
带有左右箭头的水平循环器视图指示器 - 我已完成此操作通过使用recyclerVIew创建自定义布局和两个箭头图像 -
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
case SCROLL_STATE_DRAGGING:
//Indicated that user scrolled.
programaticallyScrolled = false;
break;
case SCROLL_STATE_IDLE:
if (!programaticallyScrolled) {
currentVisibleItem = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
handleWritingViewNavigationArrows(false);
}
break;
default:
break;
}
}
});
隐藏/显示导航箭头
/**
* Handles the arrows visibility based on current visible items and scrolls the
* current visibility item based on param scroll.
*
* Scroll - is False if User scrolls the Reycler Manually
* - is True means User used arrows to navigate
*
* @param scroll
*/
private void handleWritingViewNavigationArrows(boolean scroll) {
if (currentVisibleItem == (recyclerView.getAdapter().getItemCount() - 1)) {
rightArrow.setVisibility(View.GONE);
leftArrow.setVisibility(View.VISIBLE);
} else if (currentVisibleItem != 0) {
rightArrow.setVisibility(View.VISIBLE);
leftArrow.setVisibility(View.VISIBLE);
} else if (currentVisibleItem == 0) {
leftArrow.setVisibility(View.GONE);
rightArrow.setVisibility(View.VISIBLE);
}
if (scroll) {
recyclerView.smoothScrollToPosition(currentVisibleItem);
}
}
检查示例以获取参考希望它有助于你
https://github.com/RameshBhupathi/RecyclerViewWithLeftAndRightArrowsExample
答案 2 :(得分:0)
隐藏/显示箭头的更简单方法:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (manager.findFirstCompletelyVisibleItemPosition()==0){
leftArrow.setVisibility(View.INVISIBLE);
}else{
leftArrow.setVisibility(View.VISIBLE);
}
if (manager.findLastCompletelyVisibleItemPosition()==list.size()-1){
rightArrow.setVisibility(View.INVISIBLE);
}else
rightArrow.setVisibility(View.VISIBLE);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
答案 3 :(得分:-1)
此代码适用于我的情况:
btn_to_right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
p = linearLayoutManager.findFirstVisibleItemPosition() - 1;
recyclerview.smoothScrollToPosition( p);
checkVisibility();
}
});
btn_to_left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
p = linearLayoutManager.findLastVisibleItemPosition() + 1;
recyclerview.smoothScrollToPosition(p);
checkVisibility();
}
});
public void checkVisibility() {
if (p < 1) {
btn_to_right.setVisibility(View.GONE);
btn_to_left.setVisibility(View.VISIBLE);
} else if (p >= (recyclerview.getAdapter().getItemCount() - 1)) {
btn_to_right.setVisibility(View.VISIBLE);
btn_to_left.setVisibility(View.GONE);
} else {
btn_to_right.setVisibility(View.VISIBLE);
btn_to_left.setVisibility(View.VISIBLE);
}
}