为了提升用户体验,使用Recyclerview时,会增加一些拖拽或者删除的效果,此篇内容为:实现拖拽效果,点击位置返回正确。
先上布局代码,很简单的列表布局:
activity_move_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="50"
tools:listitem="@layout/item_rv" />
</LinearLayout>
item_rv.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/WholeView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:background="@android:color/holo_blue_dark"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/white"
android:textSize="18sp"
tools:text="@tools:sample/cities" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/white&#