I want to show 2 sections under recyclerview swipe with icon and text.
This is my code
@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 itemView = viewHolder.itemView;
float action_item_width = viewHolder.itemView.getWidth() / 5;
Paint p = new Paint();
if (dX > 0) {
Log.i("actionState", dX + " " + action_item_width);
dX = (dX-1 > action_item_width) ? action_item_width : dX;
p.setColor(getResources().getColor(R.color.colorAccent));
c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
(float) itemView.getBottom(), p);
} else {
dX = 0;
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
It only creates a section with color. I need to add an icon and text.
It might be easier to achieve what you need using a different approach.
You could implement two views (foreground and background) for the recycler view item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg">
<android.support.constraint.ConstraintLayout
android:id="@+id/icon_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg1">
<!--icon and text layout (background)-->
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg2">
<!--recyclerview item layout (foreground)-->
</android.support.constraint.ConstraintLayout>
</FrameLayout>
and in the view holder of the recyclerview:
class MyViewHolder extends RecyclerView.ViewHolder {
private ConstraintLayout mItemLayout;
private ConstraintLayout mIconTextLayout;
MyViewHolder(View v) {
super(v);
mItemLayout = (ConstraintLayout) v.findViewById(R.id.item_layout);
mIconTextLayout = (ConstraintLayout) v.findViewById(R.id.icon_text_layout);
}
View getViewToSwipe() {
return mItemLayout;
}
}
Finally, attach to the recyclerview the ItemToucHelper with these callbacks:
ItemTouchHelper.SimpleCallback mItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
// override methods
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof MyViewHolder) {
int swipeFlags = ItemTouchHelper.RIGHT;
return makeMovementFlags(0, swipeFlags);
} else {
return 0;
}
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
return false;
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
getDefaultUIUtil().clearView(((MyViewHolder) viewHolder)
.getViewToSwipe());
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null) {
getDefaultUIUtil().onSelected(((MyViewHolder) viewHolder)
.getViewToSwipe());
}
}
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
getDefaultUIUtil().onDraw(c, recyclerView, ((MyViewHolder) viewHolder)
.getViewToSwipe(), dX, dY, actionState, isCurrentlyActive);
}
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
getDefaultUIUtil().onDrawOver(c, recyclerView, ((MyViewHolder) viewHolder)
.getViewToSwipe(), dX, dY, actionState, isCurrentlyActive);
}
};
ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(mItemTouchCallback);
With this approach, you will be flexible to put anything in the background layout, fi an icon with a textview as you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.