Skip to content

Commit

Permalink
TaskNotes: add drag icon for list items
Browse files Browse the repository at this point in the history
Re-add list item long click action same as
single click action. Now only touching the
drag icon it will start dragging.

- zhanghai/AndroidFastScroll#53
  • Loading branch information
bczhc committed Dec 25, 2022
1 parent 82940e1 commit 86b9048
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TaskNotesMainActivity : BaseActivity() {
private val listItems = Records()
private val database by lazy { Database.database }
private lateinit var listAdapter: ListAdapter
private lateinit var itemTouchHelper: ItemTouchHelper
private lateinit var onRecordAddedReceiver: OnRecordAddedReceiver

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -35,12 +36,13 @@ class TaskNotesMainActivity : BaseActivity() {

val recyclerView = bindings.recyclerView
queryAndSetListItems()
listAdapter = ListAdapter(this, listItems)
listAdapter = ListAdapter()
itemTouchHelper = ItemTouchHelper(ListTouchHelperCallback(listAdapter, database))
recyclerView.apply {
adapter = listAdapter
setLinearLayoutManager()
setUpFastScroll(this@TaskNotesMainActivity)
ItemTouchHelper(ListTouchHelperCallback(listAdapter, database)).attachToRecyclerView(this)
itemTouchHelper.attachToRecyclerView(this)
}

listAdapter.setOnItemClickListener { position, view ->
Expand All @@ -63,6 +65,7 @@ class TaskNotesMainActivity : BaseActivity() {
}
}.show()
}
listAdapter.setOnItemLongClickListener(listAdapter.getOnItemClickListener())

showNotification()

Expand Down Expand Up @@ -194,13 +197,27 @@ class TaskNotesMainActivity : BaseActivity() {
return timestamp in start until end
}

private class ListAdapter(private val context: Context, val records: Records) :
inner class ListAdapter :
AdapterWithClickListener<ListAdapter.MyViewHolder>() {
class MyViewHolder(view: View) : ViewHolder(view) {
private val context = this@TaskNotesMainActivity as Context
val records = this@TaskNotesMainActivity.listItems

@SuppressLint("ClickableViewAccessibility")
inner class MyViewHolder(view: View) : ViewHolder(view) {
private val bindings = TaskNotesListItemBinding.bind(view)
val descriptionTV = bindings.descriptionTv
val taskMarkTV = bindings.taskMarkTv
val timeTV = bindings.timeTv
private val dragIcon = bindings.dragIcon

init {
dragIcon.setOnTouchListener { _, event ->
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
itemTouchHelper.startDrag(this)
}
false
}
}
}

override fun onCreateViewHolder(parent: ViewGroup): MyViewHolder {
Expand Down Expand Up @@ -235,7 +252,8 @@ class TaskNotesMainActivity : BaseActivity() {
ItemTouchHelper.Callback() {
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: ViewHolder): Int {
val dragFlag = ItemTouchHelper.UP xor ItemTouchHelper.DOWN
return makeMovementFlags(dragFlag, 0)
return makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, 0)
.xor(makeFlag(ItemTouchHelper.ACTION_STATE_DRAG, dragFlag))
}

override fun onMove(recyclerView: RecyclerView, viewHolder: ViewHolder, target: ViewHolder): Boolean {
Expand All @@ -252,9 +270,14 @@ class TaskNotesMainActivity : BaseActivity() {
override fun clearView(recyclerView: RecyclerView, viewHolder: ViewHolder) {
database.reorderRecords(listAdapter.records)
}

override fun isLongPressDragEnabled() = false
}

companion object {
/**
* randomly assigned
*/
private const val NOTIFICATION_ID = 1955794286
}
}
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/drag_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="512"
android:viewportHeight="512">
<path
android:fillColor="#FF000000"
android:pathData="M80,304h352v16h-352z"/>
<path
android:fillColor="#FF000000"
android:pathData="M80,248h352v16h-352z"/>
<path
android:fillColor="#FF000000"
android:pathData="M80,192h352v16h-352z"/>
</vector>
11 changes: 10 additions & 1 deletion app/src/main/res/layout/task_notes_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
>

<ImageView android:layout_width="40dp" android:layout_height="wrap_content"
android:src="@drawable/drag_icon"
android:id="@+id/drag_icon"
android:layout_alignTop="@id/description_tv"
android:layout_alignBottom="@id/description_tv"
android:layout_centerVertical="true"
android:contentDescription="Drag"
tools:ignore="HardcodedText"
/>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_toStartOf="@id/right_ll"
android:layout_toEndOf="@id/drag_icon"
tools:text="Task Description"
android:gravity="center_vertical"
android:textAppearance="@style/TextAppearance.Material3.TitleLarge"
Expand Down

0 comments on commit 86b9048

Please sign in to comment.