Red*_*d M 4 java android android-scroll android-recyclerview linearlayoutmanager
我正在创建一个旋转木马卧式RecyclerView,其焦点位于从RecyclerView的第一项开始的焦点项上。
自定义CenterZoomLayoutManager的代码:
public class CenterZoomLayoutManager extends LinearLayoutManager {
private final float mShrinkAmount = 0.15f;
private final float mShrinkDistance = 0.9f;
public CenterZoomLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int orientation = getOrientation();
if (orientation == HORIZONTAL) {
int scrolled = super.scrollHorizontallyBy(dx, recycler, state);
float midpoint = getWidth() / 2.f;
float d0 = 0.f;
float d1 = mShrinkDistance * midpoint;
float s0 = 1.f;
float s1 = 1.f - mShrinkAmount;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
float childMidpoint =
(getDecoratedRight(child) + getDecoratedLeft(child)) / 2.f;
float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
child.setScaleX(scale);
child.setScaleY(scale);
}
return scrolled;
} else return 0;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
scrollHorizontallyBy(0, recycler, state);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的Fragment,我有以下内容:
private void onSetRecyclerView() {
recyclerView = fragmentView.findViewById(R.id.recyclerView);
if (recyclerView != null) {
RecyclerView.LayoutManager layoutManager = new CenterZoomLayoutManager(context, LinearLayoutManager.HORIZONTAL,
false);
recyclerView.scrollToPosition(storeList.size() / 2);
final SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
Run Code Online (Sandbox Code Playgroud)
我想从中心项目而不是从起始位置启动RecyclerView 。
罪魁祸首:
在我CenterZoomLayoutManager,我设置像素偏移开始0像素通过scrollHorizontallyBy(0, recycler, state)。
问题:
我找不到将所创建的偏移像素传递recyclerView.scrollToPosition(storeList.size() / 2)给的方法CenterZoomLayoutManager。我尝试搜索其他内置方法来获取X偏移,但到目前为止还没有运气。
解决的办法是更改何时LinearSnapHelper连接到RecyclerView。进行了以下重做onSetRecyclerView(),将的中心项对齐RecyclerView到屏幕的中心。请注意,LinearSnapHelper直到RecyclerView布局并适当滚动后,才附加。您无需进行任何滚动onLayoutChildren()。
private void onSetRecyclerView() {
recyclerView = findViewById(R.id.recyclerView);
CenterZoomLayoutManager layoutManager =
new CenterZoomLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
// Scroll to the position we want to snap to
layoutManager.scrollToPosition(storeList.size() / 2);
// Wait until the RecyclerView is laid out.
recyclerView.post(new Runnable() {
@Override
public void run() {
// Shift the view to snap near the center of the screen.
// This does not have to be precise.
int dx = (recyclerView.getWidth() - recyclerView.getChildAt(0).getWidth()) / 2;
recyclerView.scrollBy(-dx, 0);
// Assign the LinearSnapHelper that will initially snap the near-center view.
LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这就是我测试应用程序的初始屏幕的显示方式。有201家“商店”。
| 归档时间: |
|
| 查看次数: |
2673 次 |
| 最近记录: |