在使用 2 个“删除”和“取消”按钮滑动后,我会显示隐藏的视图。在我的 xml itemrow 中,我有 2 个内部 Relativelayouts
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<include
android:id="@+id/item_container"
layout="@layout/layout_back" />
<RelativeLayout
android:id="@+id/front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/quest_background_blue"
android:orientation="horizontal"
android:tag="front"
android:focusableInTouchMode="true">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:padding="8dp" />
<include
android:id="@+id/contprice"
style="@style/wrap"
layout="@layout/quest_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<LinearLayout
style="@style/wrap.linear"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="@id/contprice"
android:layout_toRightOf="@id/icon"
android:layout_weight="0.4">
<TextView
android:id="@+id/title"
style="@style/quest_title"
android:text="Рассказать друзьям" />
<TextView
android:id="@+id/details"
style="@style/quest_detail" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
带有 id front 的 RelativeLayout 是可滑动的,在带有 id item_container 的包含布局上,其中有两个按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:background="@android:color/transparent">
<TextView
android:id="@+id/cancel"
android:textColor="#0eb0a0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:padding="10dp"
android:text="Отмена"
android:textStyle="bold"/>
<TextView
android:id="@+id/delete"
android:textColor="#e6007e"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:padding="10dp"
android:text="Скрыть"
android:textStyle="bold"/>
</RelativeLayout>
问题是当带有按钮的隐藏布局显示按钮不可点击时
这里 ItemTouchHelper 实现
public class MyItemTouchHelper extends ItemTouchHelper.SimpleCallback {
private ItemAdapter itemAdapter;
private RecyclerView recyclerView;
public MyItemTouchHelper(RecyclerView mRecyclerView, ItemAdapter itemAdapter) {
super(0, ItemTouchHelper.START | ItemTouchHelper.END);
this.itemAdapter = itemAdapter;
this.recyclerView = mRecyclerView;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(0, swipeFlags);
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
Log.d("SWIPE", " direction = " + direction);
final ItemAdapter.ItemViewHolder holder = (ItemAdapter.ItemViewHolder) viewHolder;
holder.itemView.setOnClickListener(null);
((ItemAdapter.ItemViewHolder) viewHolder).itemContainer.bringToFront();
((ItemAdapter.ItemViewHolder) viewHolder).itemContainer.invalidate();
((ItemAdapter.ItemViewHolder) viewHolder).itemCancel.setEnabled(true);
((ItemAdapter.ItemViewHolder) viewHolder).itemDelete.setEnabled(true);
((ItemAdapter.ItemViewHolder) viewHolder).itemContainer.setVisibility(View.VISIBLE);
((ItemAdapter.ItemViewHolder) viewHolder).itemDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "DELETE " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
itemAdapter.removeItem(viewHolder.getAdapterPosition());
recyclerView.getAdapter().notifyItemRemoved(viewHolder.getAdapterPosition());
}
});
((ItemAdapter.ItemViewHolder) viewHolder).itemCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "CANCEL " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
((ItemAdapter.ItemViewHolder) viewHolder).itemContainer.setVisibility(View.INVISIBLE);
holder.itemView.setOnClickListener(ItemAdapter.itemAdapterClickListener(viewHolder.getAdapterPosition()));
recyclerView.getAdapter().notifyItemChanged(viewHolder.getAdapterPosition());
clearView(recyclerView, viewHolder);
((ItemAdapter.ItemViewHolder) viewHolder).itemCancel.setEnabled(false);
((ItemAdapter.ItemViewHolder) viewHolder).itemDelete.setEnabled(false);
((ItemAdapter.ItemViewHolder) viewHolder).itemContext.setVisibility(View.VISIBLE);
((ItemAdapter.ItemViewHolder) viewHolder).itemContext.bringToFront();
((ItemAdapter.ItemViewHolder) viewHolder).itemContext.invalidate();
}
});
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (viewHolder.getAdapterPosition() == -1) {
return;
}
ItemAdapter.ItemViewHolder holder = (ItemAdapter.ItemViewHolder) viewHolder;
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
if (dX > 0 || dX < 0) {
holder.itemContext.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);
View foreground = ((ItemAdapter.ItemViewHolder) viewHolder).getSwipableView();
getDefaultUIUtil().clearView(foreground);
}
}
和这里的观众
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView itemText;
RelativeLayout itemContainer;
RelativeLayout itemContext;
TextView itemDelete;
TextView itemCancel;
public ItemViewHolder(View itemView) {
super(itemView);
itemText = (TextView) itemView.findViewById(R.id.details);
itemDelete = (TextView) itemView.findViewById(R.id.delete);
itemCancel = (TextView) itemView.findViewById(R.id.cancel);
itemContainer = (RelativeLayout) itemView.findViewById(R.id.item_container);
itemContext = (RelativeLayout) itemView.findViewById(R.id.front);
}
public ViewGroup getSwipableView() {
return itemContext;
}
public ViewGroup getItemContainer() {
return itemContainer;
}
}
如果是更正确的按钮点击但不是在滑动后首先尝试更新,然后点击项目它就像仍然在滑动模式和通过日志它显示前 3 次点击是滑动方向
onSwipe Log.d("SWIPE", "direction = " + 方向);
D/SWIPE: direction = 16
D/SWIPE: direction = 16
D/SWIPE: direction = 16
然后单击按钮取消或删除