Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drawer swipe [#905] #907

Merged
merged 7 commits into from
Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<activity
android:name="com.mikepenz.fastadapter.app.SwipeListActivity"
android:label="@string/sample_swipe_list" />
<activity
android:name="com.mikepenz.fastadapter.app.SwipeDrawerListActivity"
android:label="@string/sample_swipe_drawer_list" />
<activity
android:name="com.mikepenz.fastadapter.app.ImageListActivity"
android:label="@string/sample_image_list" />
Expand Down
35 changes: 19 additions & 16 deletions app/src/main/java/com/mikepenz/fastadapter/app/SampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SampleActivity : AppCompatActivity() {
//our `SelectExtension`
private lateinit var selectExtension: SelectExtension<SimpleImageItem>

@Suppress("deprecation")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand Down Expand Up @@ -77,13 +78,14 @@ class SampleActivity : AppCompatActivity() {
PrimaryDrawerItem().withName(R.string.sample_checkbox_item).withDescription(R.string.sample_checkbox_item_descr).withSelectable(false).withIdentifier(10).withIcon(CommunityMaterial.Icon.cmd_checkbox_marked),
PrimaryDrawerItem().withName(R.string.sample_radiobutton_item).withDescription(R.string.sample_radiobutton_item_descr).withSelectable(false).withIdentifier(11).withIcon(CommunityMaterial.Icon2.cmd_radiobox_marked),
PrimaryDrawerItem().withName(R.string.sample_swipe_list).withDescription(R.string.sample_swipe_list_descr).withSelectable(false).withIdentifier(12).withIcon(MaterialDesignIconic.Icon.gmi_format_align_left),
PrimaryDrawerItem().withName(R.string.sample_endless_scroll_list).withDescription(R.string.sample_endless_scroll_list_descr).withSelectable(false).withIdentifier(13).withIcon(MaterialDesignIconic.Icon.gmi_long_arrow_down),
PrimaryDrawerItem().withName(R.string.sample_sort).withDescription(R.string.sample_sort_descr).withSelectable(false).withIdentifier(14).withIcon(MaterialDesignIconic.Icon.gmi_sort_by_alpha),
PrimaryDrawerItem().withName(R.string.sample_mopub).withDescription(R.string.sample_mopub_descr).withSelectable(false).withIdentifier(15).withIcon(MaterialDesignIconic.Icon.gmi_accounts_list),
PrimaryDrawerItem().withName(R.string.sample_realm_list).withDescription(R.string.sample_realm_list_descr).withSelectable(false).withIdentifier(16).withIcon(MaterialDesignIconic.Icon.gmi_format_color_text),
PrimaryDrawerItem().withName(R.string.sample_collapsible_multi_select_delete).withDescription(R.string.sample_collapsible_multi_select_delete_descr).withSelectable(false).withIdentifier(17).withIcon(MaterialDesignIconic.Icon.gmi_check_all),
PrimaryDrawerItem().withName(R.string.sample_sticky_header_mopub).withDescription(R.string.sample_sticky_header_mopub_descr).withSelectable(false).withIdentifier(18).withIcon(MaterialDesignIconic.Icon.gmi_accounts_list),
PrimaryDrawerItem().withName(R.string.sample_diff_util).withDescription(R.string.sample_diff_util_descr).withSelectable(false).withIdentifier(19).withIcon(MaterialDesignIconic.Icon.gmi_refresh),
PrimaryDrawerItem().withName(R.string.sample_swipe_drawer_list).withDescription(R.string.sample_swipe_drawer_list_descr).withSelectable(false).withIdentifier(13).withIcon(MaterialDesignIconic.Icon.gmi_format_align_left),
PrimaryDrawerItem().withName(R.string.sample_endless_scroll_list).withDescription(R.string.sample_endless_scroll_list_descr).withSelectable(false).withIdentifier(14).withIcon(MaterialDesignIconic.Icon.gmi_long_arrow_down),
PrimaryDrawerItem().withName(R.string.sample_sort).withDescription(R.string.sample_sort_descr).withSelectable(false).withIdentifier(15).withIcon(MaterialDesignIconic.Icon.gmi_sort_by_alpha),
PrimaryDrawerItem().withName(R.string.sample_mopub).withDescription(R.string.sample_mopub_descr).withSelectable(false).withIdentifier(16).withIcon(MaterialDesignIconic.Icon.gmi_accounts_list),
PrimaryDrawerItem().withName(R.string.sample_realm_list).withDescription(R.string.sample_realm_list_descr).withSelectable(false).withIdentifier(17).withIcon(MaterialDesignIconic.Icon.gmi_format_color_text),
PrimaryDrawerItem().withName(R.string.sample_collapsible_multi_select_delete).withDescription(R.string.sample_collapsible_multi_select_delete_descr).withSelectable(false).withIdentifier(18).withIcon(MaterialDesignIconic.Icon.gmi_check_all),
PrimaryDrawerItem().withName(R.string.sample_sticky_header_mopub).withDescription(R.string.sample_sticky_header_mopub_descr).withSelectable(false).withIdentifier(19).withIcon(MaterialDesignIconic.Icon.gmi_accounts_list),
PrimaryDrawerItem().withName(R.string.sample_diff_util).withDescription(R.string.sample_diff_util_descr).withSelectable(false).withIdentifier(20).withIcon(MaterialDesignIconic.Icon.gmi_refresh),
DividerDrawerItem(),
PrimaryDrawerItem().withName(R.string.open_source).withSelectable(false).withIdentifier(100).withIcon(MaterialDesignIconic.Icon.gmi_github)
)
Expand All @@ -101,14 +103,15 @@ class SampleActivity : AppCompatActivity() {
10L -> Intent(this@SampleActivity, CheckBoxSampleActivity::class.java)
11L -> Intent(this@SampleActivity, RadioButtonSampleActivity::class.java)
12L -> Intent(this@SampleActivity, SwipeListActivity::class.java)
13L -> Intent(this@SampleActivity, EndlessScrollListActivity::class.java)
14L -> Intent(this@SampleActivity, SortActivity::class.java)
15L -> Intent(this@SampleActivity, MopubAdsActivity::class.java)
16L -> Intent(this@SampleActivity, RealmActivity::class.java)
17L -> Intent(this@SampleActivity, ExpandableMultiselectDeleteSampleActivity::class.java)
18L -> Intent(this@SampleActivity, StickyHeaderMopubAdsActivity::class.java)
19L -> Intent(this@SampleActivity, DiffUtilActivity::class.java)
20L -> Intent(this@SampleActivity, PagedActivity::class.java)
13L -> Intent(this@SampleActivity, SwipeDrawerListActivity::class.java)
14L -> Intent(this@SampleActivity, EndlessScrollListActivity::class.java)
15L -> Intent(this@SampleActivity, SortActivity::class.java)
16L -> Intent(this@SampleActivity, MopubAdsActivity::class.java)
17L -> Intent(this@SampleActivity, RealmActivity::class.java)
18L -> Intent(this@SampleActivity, ExpandableMultiselectDeleteSampleActivity::class.java)
19L -> Intent(this@SampleActivity, StickyHeaderMopubAdsActivity::class.java)
20L -> Intent(this@SampleActivity, DiffUtilActivity::class.java)
21L -> Intent(this@SampleActivity, PagedActivity::class.java)
100L -> LibsBuilder()
.withFields(R.string::class.java.fields)
.withActivityTitle(getString(R.string.open_source))
Expand All @@ -125,7 +128,7 @@ class SampleActivity : AppCompatActivity() {
false
}
selectedItemPosition = RecyclerView.NO_POSITION
withSavedInstance(savedInstanceState)
setSavedInstance(savedInstanceState)
}

//create our ItemAdapter which will host our items
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package com.mikepenz.fastadapter.app

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mikepenz.fastadapter.adapters.FastItemAdapter
import com.mikepenz.fastadapter.app.adapters.IDraggableViewHolder
import com.mikepenz.fastadapter.app.items.SwipeableDrawerItem
import com.mikepenz.fastadapter.drag.ItemTouchCallback
import com.mikepenz.fastadapter.drag.SimpleDragCallback
import com.mikepenz.fastadapter.swipe_drag.SimpleSwipeDrawerDragCallback
import com.mikepenz.fastadapter.utils.DragDropUtil
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.materialdesigniconic.MaterialDesignIconic
import com.mikepenz.iconics.utils.actionBar
import com.mikepenz.iconics.utils.colorInt
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_sample.*
import java.util.*

class SwipeDrawerListActivity : AppCompatActivity(), ItemTouchCallback {

//save our FastAdapter
private lateinit var fastItemDrawerAdapter: FastItemAdapter<SwipeableDrawerItem>

//drag & drop
private lateinit var touchCallback: SimpleDragCallback
private lateinit var touchHelper: ItemTouchHelper


private fun delete(item: SwipeableDrawerItem) {
item.deleteAction = null
val position12 = fastItemDrawerAdapter.getAdapterPosition(item)
if (position12 != RecyclerView.NO_POSITION) {
//this sample uses a filter. If a filter is used we should use the methods provided by the filter (to make sure filter and normal state is updated)
fastItemDrawerAdapter.itemFilter.remove(position12)
Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show()
}
}

private fun archive(item: SwipeableDrawerItem) {
item.archiveAction = null
val position12 = fastItemDrawerAdapter.getAdapterPosition(item)
if (position12 != RecyclerView.NO_POSITION) {
// Do something intelligent here
Toast.makeText(this, "Archived", Toast.LENGTH_SHORT).show()
}
}

private fun share(item: SwipeableDrawerItem) {
item.shareAction = null
val position12 = fastItemDrawerAdapter.getAdapterPosition(item)
if (position12 != RecyclerView.NO_POSITION) {
// Do something intelligent here
Toast.makeText(this, "Shared", Toast.LENGTH_SHORT).show()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sample)

// Handle Toolbar
setSupportActionBar(toolbar)

//create our FastAdapter which will manage everything
fastItemDrawerAdapter = FastItemAdapter()
//configure the itemAdapter
fastItemDrawerAdapter.itemFilter.filterPredicate = { item: SwipeableDrawerItem, constraint: CharSequence? ->
item.name?.textString.toString().contains(constraint.toString(), ignoreCase = true)
}

//get our recyclerView and do basic setup
rv.layoutManager = LinearLayoutManager(this)
rv.itemAnimator = DefaultItemAnimator()
rv.adapter = fastItemDrawerAdapter

//fill with some sample data
var x = 0
val items = ArrayList<SwipeableDrawerItem>()
for (s in ALPHABET) {
val count = Random().nextInt(20)
for (i in 1..count) {
val swipeableItem = SwipeableDrawerItem().withName("$s Test $x")
swipeableItem.identifier = (100 + x).toLong()
swipeableItem.withIsSwipeable(i % 5 != 0)
swipeableItem.withIsDraggable(i % 5 != 0)
swipeableItem.deleteAction = Consumer { item -> delete(item) }
swipeableItem.archiveAction = Consumer { item -> archive(item) }
swipeableItem.shareAction = Consumer { item -> share(item) }
items.add(swipeableItem)
x++
}
}
fastItemDrawerAdapter.add(items)


//add drag and drop for item
//and add swipe as well
touchCallback = SimpleSwipeDrawerDragCallback(
this,
ItemTouchHelper.LEFT)
.withNotifyAllDrops(true)
.withSwipeLeft(80) // Width of delete button
.withSwipeRight(160) // Width of archive and share buttons
.withSensitivity(10f)

touchHelper = ItemTouchHelper(touchCallback) // Create ItemTouchHelper and pass with parameter the SimpleDragCallback
touchHelper.attachToRecyclerView(rv) // Attach ItemTouchHelper to RecyclerView

//restore selections (this has to be done after the items were added)
fastItemDrawerAdapter.withSavedInstanceState(savedInstanceState)

//set the back arrow in the toolbar
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(false)
}

override fun onSaveInstanceState(_outState: Bundle) {
var outState = _outState
//add the values which need to be saved from the adapter to the bundle
outState = fastItemDrawerAdapter.saveInstanceState(outState)
super.onSaveInstanceState(outState)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
//handle the click on the back arrow click
return when (item.itemId) {
android.R.id.home -> {
onBackPressed()
true
}
else -> super.onOptionsItemSelected(item)
}
}


override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu items for use in the action bar
val inflater = menuInflater
inflater.inflate(R.menu.search, menu)

//search icon
menu.findItem(R.id.search).icon = IconicsDrawable(this, MaterialDesignIconic.Icon.gmi_search).apply { colorInt = Color.BLACK; actionBar() }

val searchView = menu.findItem(R.id.search).actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(s: String): Boolean {
touchCallback.setIsDragEnabled(false)
fastItemDrawerAdapter.filter(s)
return true
}


override fun onQueryTextChange(s: String): Boolean {
fastItemDrawerAdapter.filter(s)
touchCallback.setIsDragEnabled(TextUtils.isEmpty(s))
return true
}
})

return super.onCreateOptionsMenu(menu)
}

override fun itemTouchOnMove(oldPosition: Int, newPosition: Int): Boolean {
DragDropUtil.onMove(fastItemDrawerAdapter.itemAdapter, oldPosition, newPosition) // change position
return true
}

override fun itemTouchDropped(oldPosition: Int, newPosition: Int) {
val vh: RecyclerView.ViewHolder? = rv.findViewHolderForAdapterPosition(newPosition)
if (vh is IDraggableViewHolder) {
(vh as IDraggableViewHolder).onDropped()
}
// save the new item order, i.e. in your database
}

override fun itemTouchStartDrag(viewHolder: RecyclerView.ViewHolder) {
if (viewHolder is IDraggableViewHolder) {
(viewHolder as IDraggableViewHolder).onDragged()
}
}

companion object {
private val ALPHABET = arrayOf("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
}
}
Loading