出于某种原因,我无法从ItemTouchHelper获取滑动事件。我相信我错过了一些东西,但我不知道是什么。有什么想法吗?
这是我的代码:
final CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.HORIZONTAL);
layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener());
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycle_view);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(new ConciergeCarouselAdapter(DemoData.getTopMenu()));
recyclerView.addOnScrollListener(new CenterScrollListener());
ItemTouchHelper.SimpleCallback itemTouchCallback = new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(final RecyclerView recyclerView, final RecyclerView.ViewHolder viewHolder,
final RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, final int swipeDir) {
Out.o(viewHolder.getAdapterPosition());
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
更新
我在ItemTouchHelper.SimpleCallback:
中添加了这些代码行@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
如果我在行上放置一个断点“return true;”当我滑动程序停止时,但是当我在断开时放置一个断点没有任何反应。
更新2
我用LinearLayoutManager替换了CarouselLayoutManager,现在我能够捕获onSwiped事件。虽然我需要使用CarouselLayoutManager。