我有一个用SnapHelper实现的水平Recycler视图,以产生旋转效果。在我的ViewHolder中,我有一个为Recycler项目中的视图设置动画的功能。因此,每当在SnapHelper中将某个项目滚动到中心(选定位置)时,我都需要启动动画。我尝试了以下代码,但无法正常工作。有时我将viewholder设置为null。
widgetScrollView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(final RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
int pos = ((LinearLayoutManager) widgetScrollView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
ResultTallyAdapter.ViewHolder viewHolder = (ResultTallyAdapter.ViewHolder) recyclerView.findViewHolderForAdapterPosition(pos);
viewHolder.startAnimation(MainActivity.this, pos);
}
}, 1000);
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});