Как мога да намеря индекса на прихванат елемент? Опитах да използвам:
recyclerView.setOnFlingListener(new RecyclerView.OnFlingListener() {
@Override
public boolean onFling(int velocityX, int velocityY) {
Log.i(TAG, "initRecyclerView: SNAPPING TO:" +snapHelper.findTargetSnapPosition(recyclerView.getLayoutManager(),velocityX,velocityY)%songs.size());
return false;
}
});
но наистина не е точно и понякога връща -1.
Опитах :
snapHelper.findSnapView(recyclerView.getLayoutManager());
но връща изглед и не мисля, че мога да получа индекса на изглед вътре в recyclerview. (Греша ли?)
Нямам предпочитание кога методът да връща индекса (преди хвърлянето да приключи или след това).
ps: Моят recyclerview има персонализиран ZoomingLayoutManager (за ефект на въртележка). Може би променя размера на изгледа и това прави snapHelper дефектен? Знам, че snapHelper работи със средната височина и ширина на видимите childViews.
public class ZoomingLayoutManager extends LinearLayoutManager {
private final float mShrinkAmount = 0.5f;
private final float mShrinkDistance = 0.9f;
public ZoomingLayoutManager(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 int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
int orientation = getOrientation();
if (orientation == VERTICAL) {
int scrolled = super.scrollVerticallyBy(dy, recycler, state);
float midpoint = getHeight() / 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 =
(getDecoratedBottom(child) + getDecoratedTop(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;
}
}
}
Ако е вярно, как мога да отменя методите на snapHelper, за да намеря по-точната позиция? Или просто пропускам нещо очевидно!?