我正在尝试创建一个与图库视图完全相同的循环视图,当您横向滚动时,它会像在图库视图中一样锁定当前查看的项目吗?
这可能吗?
到目前为止,我已经创建了我的recycleView并使用了一个LinearLayoutManager设置为Horizontal,以便它可以在水平方向滚动(工作正常),但如何锁定当前项目,如在图库视图中?
recyclerView = (RecyclerView) view.findViewById(R.id.recycleView);
MyAdapter adapter = new MyMapAdapter();
recyclerView.setAdapter(adapter);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mLayoutManager);
答案 0 :(得分:0)
RecyclerView提供这些内置布局管理器:
来源:https://developer.android.com/training/material/lists-cards.html#RecyclerView
GridLayoutManager:
GridLayoutManager mLayoutManager = new GridLayoutManager(this, 4); // (Context context, int spanCount)
mRecyclerView.setLayoutManager(mLayoutManager);
StaggeredGridLayoutManager:
StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL); // (int spanCount, int orientation)
mRecyclerView.setLayoutManager(mLayoutManager);
答案 1 :(得分:0)
我有同样的问题。这就是我所做的,我在我的RecyclerView上添加了一个OnScrollListener:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
recyclerView.scrollToPosition(getScrolledPosition());
galleryAdapter.highlightItem(getMidPosition());
}
}
和scrollToPosition方法如下所示:
private int getScrolledPosition() {
return (int) ((double) recyclerView.computeHorizontalScrollOffset() / (double) galleryAdapter.getItemWidth());
}
getMidPosition:
private int getMidPosition() {
return getScrolledPosition() + (GalleryAdapter.ITEM_NUM / 2);
}
GalleryAdapter是我的adpater,它从RecyclerView.Adapter扩展而来。通过这种方式,我突出了中心项目。问题是当我拖动或转动recyclerview时,当前的中心项目不会突出显示。仅当视图停止滚动时才有效。