我通过Recycler
实施了ItemTouchHelper.SimpleCallback
滑动效果,并在后视图上放置了一些按钮,这些按钮在翻转后显示。有趣的问题是当我点击按钮时不起作用。我应该按两次,在第三次尝试他们的工作!这是一个gif:
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT ) {
@Override
public int getMovementFlags(RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder) {
int dragFlags = 0;
int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public boolean isLongPressDragEnabled() {
return false;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
((InvoiceItemListAdapter.ViewHolder)viewHolder).lunchSwipeLeftEffect(dX);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
}
};
recentInvoicesItemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
recentInvoicesItemTouchHelper.attachToRecyclerView(recentInvoiceRecyclerView);
这是我的RecyclerView.ViewHolder
:
private class RecentInvoiceViewHolder extends RecyclerView.ViewHolder{
private TextView price;
private TextView hours;
private TextView minutes;
private TextView desc;
private View color;
private View sent;
private View swipeActionsView;
private View frontView;
private Button print;
private Button cancelSwipe;
boolean swiped = false;
public RecentInvoiceViewHolder(View view) {
super(view);
price = (TextView) view.findViewById(R.id.invoice_review_in_day_price);
hours = (TextView) view.findViewById(R.id.invoice_review_in_day_hours);
minutes = (TextView) view.findViewById(R.id.invoice_review_in_day_minutes);
desc = (TextView) view.findViewById(R.id.invoice_review_in_day_decs);
color = view.findViewById(R.id.invoice_review_in_day_color);
sent = view.findViewById(R.id.invoice_review_in_day_sent_check);
swipeActionsView = view.findViewById(R.id.swipe_actions);
frontView = view.findViewById(R.id.front_view);
print = (Button) view.findViewById(R.id.print_recent_invoice);
cancelSwipe = (Button) view.findViewById(R.id.cancel_swipe);
cancelSwipe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
frontView.setTranslationX(0);
frontView.setVisibility(View.VISIBLE);
swiped = false;
}
});
}
public void lunchSwipeEffect(float dx){
if(!swiped)
frontView.setTranslationX(dx);//TranslationX(dx);
if(Math.abs(dx)> frontView.getMeasuredWidth()) {
swiped = true;
frontView.setVisibility(View.GONE);
}
}
}
最后这是我的ViewHolder
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/archived_right_one_day_invoice_item_height"
android:layout_margin="@dimen/archived_right_one_day_item_margin"
android:clipChildren="false"
android:clipToPadding="false">
<TableLayout
android:id="@+id/swipe_actions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1,2"
android:clickable="false">
<TableRow
android:layout_weight="1"
>
<ir.masna.hetab.views.IranFontButton
android:id="@+id/print_recent_invoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/archived_right_one_day_item_time_font"
android:gravity="center"
android:text="print"
style="@style/CustomFontStyleYekan"
android:layout_column="0"/>
<ir.masna.hetab.views.IranFontButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/archived_right_one_day_item_time_font"
android:gravity="center"
android:text="print"
style="@style/CustomFontStyleYekan"
android:layout_column="1"/>
<ir.masna.hetab.views.IranFontButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/archived_right_one_day_item_time_font"
android:gravity="center"
android:text="print"
style="@style/CustomFontStyleYekan"
android:layout_column="2"/>
</TableRow>
<TableRow
android:layout_weight="1">
<ir.masna.hetab.views.IranFontButton
android:id="@+id/cancel_swipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/archived_right_one_day_item_time_font"
android:gravity="center"
android:text="cancel"
style="@style/CustomFontStyleYekan"
android:layout_column="0"/>
<ir.masna.hetab.views.IranFontButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/archived_right_one_day_item_time_font"
android:gravity="center"
android:text="print"
style="@style/CustomFontStyleYekan"
android:layout_column="1"/>
<ir.masna.hetab.views.IranFontButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/archived_right_one_day_item_time_font"
android:gravity="center"
android:text="print"
style="@style/CustomFontStyleYekan"
android:layout_column="2"/>
</TableRow>
</TableLayout>
<RelativeLayout
android:id="@+id/front_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:clickable="true"
>
<View
android:id="@+id/invoice_review_in_day_color"
android:layout_width="@dimen/archived_right_one_day_item_colored_view_width"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@drawable/invoice_review_box_radius_right_shape"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/invoice_review_in_day_color"
android:layout_alignParentLeft="true"
android:background="@drawable/invoice_review_main_box_radius_shape"
android:padding="@dimen/archived_right_one_day_item_border">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="@integer/archived_right_one_day_item_top_invoice_section_weight"
android:orientation="horizontal"
android:background="@drawable/invoice_review_box_radius_top_left_shape"
android:padding="@dimen/archived_right_one_day_item_top_section_padding">
<LinearLayout
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ir.masna.hetab.views.IranFontTextView
android:id="@+id/invoice_review_in_day_hours"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="@dimen/archived_right_one_day_item_time_margin"
android:textSize="@dimen/archived_right_one_day_item_time_font"
android:textColor="@color/archived_right_one_day_item_time_text_color"
android:gravity="center"
android:background="@drawable/clock_background"
style="@style/CustomFontStyleYekan"/>
<ir.masna.hetab.views.IranFontTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="@dimen/archived_right_one_day_item_time_separator_font"
android:textColor="@color/archived_right_one_day_item_time_text_color"
android:text=":"
android:layout_marginTop="@dimen/archived_right_one_day_item_time_separator_margin_top"/>
<ir.masna.hetab.views.IranFontTextView
android:id="@+id/invoice_review_in_day_minutes"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="@dimen/archived_right_one_day_item_time_margin"
android:textSize="@dimen/archived_right_one_day_item_time_font"
android:textColor="@color/archived_right_one_day_item_time_text_color"
android:gravity="center"
android:background="@drawable/clock_background"
style="@style/CustomFontStyleYekan"/>
</LinearLayout>
<LinearLayout
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ir.masna.hetab.views.IranFontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/archived_right_one_day_item_total_title"
android:textColor="#6d6d6d"
android:textSize="@dimen/archived_right_one_day_item_price_title_font"
android:gravity="bottom|right"
android:includeFontPadding="false"
android:layout_alignParentRight="true"
style="@style/CustomFontStyleYekan"/>
<ir.masna.hetab.views.AutoResize.AutoResizeTextView
android:id="@+id/invoice_review_in_day_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/archived_right_one_day_item_price_font"
android:gravity="top|right"
android:includeFontPadding="false"
android:maxLines="1"
style="@style/CustomFontStyleYekan"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="@integer/archived_right_one_day_item_bottom_invoice_section_weight"
android:background="@drawable/invoice_review_box_radius_bottom_left_shape">
<ir.masna.hetab.views.IranFontTextView
android:id="@+id/invoice_review_in_day_decs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|right"
android:textColor="@color/archived_right_one_day_item_time_text_color"
android:textSize="@dimen/archived_right_one_day_item_desc_font"
android:padding="@dimen/archived_right_one_day_item_desc_padding"
android:layout_toRightOf="@+id/invoice_review_in_day_sent_check"
android:ellipsize="end"
android:maxLines="1"
style="@style/CustomFontStyleYekan" />
<View
android:id="@+id/invoice_review_in_day_sent_check"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:background="@drawable/tick"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
并且recyclerView位于NestedScrollView
,具有折叠工具栏效果
哪个视图消耗两次首次点击?!!
答案 0 :(得分:0)
ItemTouchHelper
抓住了RecyclerView
的压力