我在 android 中有一个回收站视图。我已经附上了ItemTouchHelper
它。我只启用了从左到右的滑动。
因此,当我滑动 Recycler 视图的任何项目时,该项目开始向右移动,并在左侧绘制文本。这可以 。我的要求是,我希望只允许用户滑动到某个距离,当达到该距离并且用户释放触摸时,被滑动的项目应该回到它向左的位置。
问题是,当我从左向右滑动时,视图会完全滑出屏幕。我怎样才能限制它只能刷到一段距离?我该怎么做 ?
这是我的项目触摸回调代码:
private void initSwipe(){
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
ViewHolder viewHolder1 = (ViewHolder)viewHolder;
int position = viewHolder1.getAdapterPosition();
MyItem item = dataList.get(position);
if (item==null )return;
if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
View itemView = viewHolder.itemView;
float height = (float) itemView.getBottom() - (float) itemView.getTop();
float width = height / 3;
Paint p = new Paint();
Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(20);
if(dX > 0) {
p.setColor(Color.parseColor("#000000"));
RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX,(float) itemView.getBottom());
c.drawRect(background,p);
//RectF icon_dest = new RectF((float) itemView.getLeft() + width ,(float) itemView.getTop() + width,(float) itemView.getLeft()+ 2*width,(float)itemView.getBottom() - width);
//c.drawBitmap(icon,null,icon_dest,p);
c.drawText(item.getDate(),(float)(itemView.getLeft() + width),(float)(itemView.getBottom() - width), textPaint);
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(myRecycler);
}`