java - Swipe left to show view at top of cardview in recycler view item
问题描述
On Swipe left from child item of recycler view to show an custom view
result_what_i_get
needed_result
I used the following code :
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
final int position = viewHolder.getAdapterPosition();
if (direction == ItemTouchHelper.LEFT) {
Toast.makeText(getApplicationContext(), "Left swipe at position :"+(position+1), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
try {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
View itemView = viewHolder.itemView;
Paint paint = new Paint();
paint.setColor(Color.parseColor("#D32F2F"));
RectF background = new RectF((float) itemView.getRight() + dX / 5, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
c.drawRect(background, paint);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
But i get the below result : result_what_i_get
But i need that result : needed_result
item xml file
the custom item file that i use in above code :
<android.support.v7.widget.CardView
android:id="@+id/cardV"
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"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="3dp"
app:cardElevation="4dp"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp">
<LinearLayout
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/card_view_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="15sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Swipe"
android:textColor="@android:color/darker_gray"
android:textSize="12sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
On swipe left to show a view just like i mention in screenshot
解决方案
推荐阅读
- javascript - 减少这个 JS 程序的资源使用?
- azure-devops - 将 Excel 连接到 Azure Devops 查询
- python - 如何使用 requests-html 获取网页上的所有链接
- c# - 如何在 C# 中正确使用绑定
- excel - 如何将单元格与一组单元格进行比较并获得相关值
- html - 在页面上移动了一个 div 项
- three.js - Three.js:渲染的节流/智能调用是否必要?
- python - Python 在关闭另一个窗口之前不会运行新窗口
- java - KeyAgreement.generateSecret() 中的本机方法错误
- java - Android监听器在活动中工作但不在片段中