我在我的主要活动布局中使用了多个RecyclerView,并且从此处https://github.com/rubensousa/RecyclerViewSnap/使用捕捉来水平或垂直滚动显示recyclerview。很好。
但是,当我水平滚动时,最后一个Recycleview本身会滚动回到项目或位置之一,而在此上方的其他或其他Recycleview则不是这种情况。
如何防止这种情况。 这是我的助手类,用于处理滚动行为
public class GravitySnapHelper extends LinearSnapHelper {
private OrientationHelper verticalHelper;
private OrientationHelper horizontalHelper;
private int gravity;
private boolean isSupportRtL;
@SuppressLint("RtlHardcoded")
public GravitySnapHelper(int gravity) {
this.gravity = gravity;
if (this.gravity == Gravity.LEFT) {
this.gravity = Gravity.START;
} else if (this.gravity == Gravity.RIGHT) {
this.gravity = Gravity.END;
}
}
@Override
public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
throws IllegalStateException {
if (recyclerView != null) {
isSupportRtL = recyclerView.getContext().getResources().getBoolean(R.bool.is_rtl);
}
super.attachToRecyclerView(recyclerView);
}
@Override
public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager,
@NonNull View targetView) {
int[] out = new int[2];
if (layoutManager.canScrollHorizontally()) {
if (gravity == Gravity.START) {
out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));
} else { // END
out[0] = distanceToEnd(targetView, getHorizontalHelper(layoutManager));
}
} else {
out[0] = 0;
}
if (layoutManager.canScrollVertically()) {
if (gravity == Gravity.TOP) {
out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager));
} else { // BOTTOM
out[1] = distanceToEnd(targetView, getVerticalHelper(layoutManager));
}
} else {
out[1] = 0;
}
return out;
}
@Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof LinearLayoutManager) {
switch (gravity) {
case Gravity.START:
return findStartView(layoutManager, getHorizontalHelper(layoutManager));
case Gravity.TOP:
return findStartView(layoutManager, getVerticalHelper(layoutManager));
case Gravity.END:
return findEndView(layoutManager, getHorizontalHelper(layoutManager));
case Gravity.BOTTOM:
return findEndView(layoutManager, getVerticalHelper(layoutManager));
}
}
return super.findSnapView(layoutManager);
}
private int distanceToStart(View targetView, OrientationHelper helper) {
if (isSupportRtL) {
return distanceToEnd(targetView, helper);
}
return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}
private int distanceToEnd(View targetView, OrientationHelper helper) {
if (isSupportRtL) {
return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}
return helper.getDecoratedEnd(targetView) - helper.getEndAfterPadding();
}
private View findStartView(RecyclerView.LayoutManager layoutManager,
OrientationHelper helper) {
if (layoutManager instanceof LinearLayoutManager) {
int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
if (firstChild == RecyclerView.NO_POSITION) {
return null;
}
View child = layoutManager.findViewByPosition(firstChild);
if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
&& helper.getDecoratedEnd(child) > 0) {
return child;
} else {
if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
== layoutManager.getItemCount() - 1) {
return null;
} else {
return layoutManager.findViewByPosition(firstChild + 1);
}
}
}
return super.findSnapView(layoutManager);
}
private View findEndView(RecyclerView.LayoutManager layoutManager,
OrientationHelper helper) {
if (layoutManager instanceof LinearLayoutManager) {
int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
if (lastChild == RecyclerView.NO_POSITION) {
return null;
}
View child = layoutManager.findViewByPosition(lastChild);
if (helper.getDecoratedStart(child) + helper.getDecoratedMeasurement(child) / 2
<= helper.getTotalSpace()) {
return child;
} else {
if (((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition()
== 0) {
return null;
} else {
return layoutManager.findViewByPosition(lastChild - 1);
}
}
}
return super.findSnapView(layoutManager);
}
private OrientationHelper getVerticalHelper(RecyclerView.LayoutManager layoutManager) {
if (verticalHelper == null) {
verticalHelper = OrientationHelper.createVerticalHelper(layoutManager);
}
return verticalHelper;
}
private OrientationHelper getHorizontalHelper(RecyclerView.LayoutManager layoutManager) {
if (horizontalHelper == null) {
horizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
}
return horizontalHelper;
}
}
我的主要活动是
public class MainActivity extends AppCompatActivity {
private ArrayList<Item> items;
private RecyclerView science, engineering;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
science = (RecyclerView) findViewById(R.id.science);
engineering = (RecyclerView) findViewById(R.id.engineering);
createApps();
/**
* Center snapping
*/
//SnapHelper snapHelper = new LinearSnapHelper();
//snapHelper.attachToRecyclerView(recyclerView);
/**
* Start snapping
*/
//SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
//snapHelper.attachToRecyclerView(recyclerView);
/**
* End snapping
*/
//SnapHelper snapHelper = new GravitySnapHelper(Gravity.END);
//snapHelper.attachToRecyclerView(recyclerView);
/**
* Top snapping
*/
//SnapHelper snapHelper = new GravitySnapHelper(Gravity.TOP);
//snapHelper.attachToRecyclerView(recyclerView);
/**
* Bottom snapping
*/
SnapHelper snapHelper = new GravitySnapHelper(Gravity.CENTER);
snapHelper.attachToRecyclerView(science);
snapHelper.attachToRecyclerView(medical);
snapHelper.attachToRecyclerView(engineering);
snapHelper.attachToRecyclerView(account);
snapHelper.attachToRecyclerView(others);
// HORIZONTAL for Gravity START/END and VERTICAL for TOP/BOTTOM SCIENCE
science.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
science.setHasFixedSize(true);
SnapRecyclerAdapter adapter = new SnapRecyclerAdapter(this, items);
science.setAdapter(adapter);
//Engineering
engineering.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
engineering.setHasFixedSize(true);
SnapRecyclerAdapter engi = new SnapRecyclerAdapter(this, items);
engineering.setAdapter(engi);
}
private void createApps() {
items = new ArrayList<>();
items.add(new Item("Google+", R.drawable.google_plus));
items.add(new Item("Facebook", R.drawable.facebook));
items.add(new Item("LinkedIn", R.drawable.linkedin));
items.add(new Item("Youtube", R.drawable.youtube));
items.add(new Item("Instagram", R.drawable.instagram));
items.add(new Item("Skype", R.drawable.skype));
items.add(new Item("Twitter", R.drawable.twitter));
items.add(new Item("Wikipedia", R.drawable.wikipedia));
items.add(new Item("Whats app", R.drawable.what_apps));
items.add(new Item("Pokemon Go", R.drawable.pokemon_go));
}
}
如果有人需要的话,这里是上述教程的文章 http://www.devexchanges.info/2016/09/android-tip-recyclerview-snapping-with.html