我正在尝试使用不同ViewHolders的RecyclerView。其中一些可能是刷卡而其他人没有。 问题是我有一个ViewHolder里面有一个ViewPager(就像一个图片的旋转木马)而且我不想刷卡(RecyclerView刷卡),我想保留它自己的刷卡以便在页面之间滑动(图片)
¿在这些情况下,如何将轻扫手势从RecyclerView传播到ViewHolder?
我一直在尝试识别刷过的ViewHolder是否是ViewPager如何传播滑动手势,但我找不到方法。
这是我的RecyclerView布局
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_height="match_parent"
android:layout_width="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
这是我的ItemTouchHelperCallback
public class SwipeHelperCallback extends ItemTouchHelper.Callback {
public interface ItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder, int direction);
}
Context context;
ItemTouchHelperListener mListener;
public SwipeHelperCallback(Context context, ItemTouchHelperListener mListener) {
this.context = context;
this.mListener = mListener;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(0, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
if (viewHolder instanceof CardMiniViewHolder ||
viewHolder instanceof CardBlogViewHolder) {
mListener.onSwiped(viewHolder, direction);
}
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState== ItemTouchHelper.ACTION_STATE_SWIPE) {
View v = viewHolder.itemView;
if (viewHolder instanceof CardMiniViewHolder ||
viewHolder instanceof CardBlogViewHolder) {
v.findViewById(R.id.foreground).setTranslationX(dX);
}
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
if (viewHolder instanceof CardMiniViewHolder ||
viewHolder instanceof CardBlogViewHolder) {
viewHolder.itemView.findViewById(R.id.foreground).setTranslationX(0);
}
}
}
这是我的ViewHolder和我的轮播行(ViewPager)的布局
(布局)
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:text="text"
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:textColor="@color/colorTextDark"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"/>
</RelativeLayout>
(ViewHolder)
public class CardAlbumViewHolder extends RecyclerView.ViewHolder implements AlbumPagerAdapter.OnViewPagerClickListener {
ViewPager viewPager;
TextView imageIndicatorText;
Album album;
Context context;
int total;
public CardAlbumViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
viewPager = (ViewPager) itemView.findViewById(R.id.view_pager);
imageIndicatorText = (TextView) itemView.findViewById(R.id.text_view);
}
public void bind(Album album) {
this.album = album;
total = album.getImageUrlsArrayList().size()+1;
viewPager.setAdapter(new AlbumPagerAdapter(viewPager.getContext(), album, this));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
imageIndicatorText.setText(String.format("%d/%d", position + 1, total));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
imageIndicatorText.setText(String.format("%d/%d", 1, total));
}
@Override
public void onPageClick(int position) {
Log.i("click", "album");
Intent intent = new Intent(context, AlbumActivity.class);
intent.putExtra("album", album);
context.startActivity(intent);
}
}