我正在实施一个水平RecyclerView
,可以在滚动后将项目捕捉到中心,就像谷歌播放App水平列表一样。 http://nginx.org/en/docs/http/ngx_http_auth_request_module.html是一篇评论。
我的代码如下:
MainMenuAdapter mainMenuAdapter = new MainMenuAdapter(this, mDataset);
final LinearLayoutManager layoutManagaer = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView mainMenu = (RecyclerView) findViewById(R.id.main_menu);
mainMenu.setLayoutManager(layoutManagaer);
mainMenu.setAdapter(mainMenuAdapter);
final SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(mainMenu);
RecyclerView
将中心项目(位置)折叠到中心后如何获取中心项目(位置)?对此没有任何监听器实现吗?
此外,当触摸项目视图时,我想将其捕捉到中心。我怎么能这样做?
答案 0 :(得分:9)
如果您需要View,可以致电
View view = snapHelper.findSnapView(layoutManagaer);
获得视图后,您应该能够获得该视图的数据集上的位置。例如使用
mainMenu.getChildAdapterPosition(view)
答案 1 :(得分:7)
原帖:
即使您不打算使用SnapHelper
,也可以通过RecyclerView.OnScrollListener
获取中心元素位置。
MiddleItemFinder
课程复制到您的项目中。创建回调对象MiddleItemCallback
。
MiddleItemFinder.MiddleItemCallback callback =
new MiddleItemFinder.MiddleItemCallback() {
@Override
public void scrollFinished(int middleElement) {
// interaction with middle item
}
};
向您的RecyclerView
recyclerView.addOnScrollListener(
new MiddleItemFinder(getContext(), layoutManager,
callback, RecyclerView.SCROLL_STATE_IDLE));
最后一个参数或MiddleItemFinder
构造函数为scrollState
。
RecyclerView.SCROLL_STATE_IDLE
- RecyclerView目前不在
滚动。滚动完了。RecyclerView.SCROLL_STATE_DRAGGING
- RecyclerView目前正在进行中
被外部输入拖动,例如用户触摸输入。RecyclerView.SCROLL_STATE_SETTLING
- RecyclerView目前正在进行中
在不受外界控制的情况下动画到最终位置。MiddleItemFinder.ALL_STATES
- 所有州一起。RecyclerView.SCROLL_STATE_IDLE
作为最后一个构造函数参数而不是所有滚动结束,则回调对象将返回中间元素位置。public class MiddleItemFinder extends RecyclerView.OnScrollListener {
private
Context context;
private
LinearLayoutManager layoutManager;
private
MiddleItemCallback callback;
private
int controlState;
public
static final int ALL_STATES = 10;
public MiddleItemFinder(Context context, LinearLayoutManager layoutManager, MiddleItemCallback callback, int controlState) {
this.context = context;
this.layoutManager = layoutManager;
this.callback = callback;
this.controlState = controlState;
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (controlState == ALL_STATES || newState == controlState) {
int firstVisible = layoutManager.findFirstVisibleItemPosition();
int lastVisible = layoutManager.findLastVisibleItemPosition();
int itemsCount = lastVisible - firstVisible + 1;
int screenCenter = context.getResources().getDisplayMetrics().widthPixels / 2;
int minCenterOffset = Integer.MAX_VALUE;
int middleItemIndex = 0;
for (int index = 0; index < itemsCount; index++) {
View listItem = layoutManager.getChildAt(index);
if (listItem == null)
return;
int leftOffset = listItem.getLeft();
int rightOffset = listItem.getRight();
int centerOffset = Math.abs(leftOffset - screenCenter) + Math.abs(rightOffset - screenCenter);
if (minCenterOffset > centerOffset) {
minCenterOffset = centerOffset;
middleItemIndex = index + firstVisible;
}
}
callback.scrollFinished(middleItemIndex);
}
}
public interface MiddleItemCallback {
void scrollFinished(int middleElement);
}
}