Skip to content

Commit

Permalink
- Add getGroups function to Section
Browse files Browse the repository at this point in the history
- Add an example of Draggable reorderable items in the main example
  • Loading branch information
ValCanBuild committed Apr 1, 2020
1 parent b8d4067 commit 0910269
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public boolean onItemLongClick(Item item, View view) {

private TouchCallback touchCallback = new SwipeTouchCallback(gray) {
@Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
return true;
}

@Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
Expand Down
1 change: 1 addition & 0 deletions example-shared/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="expanding_group_subtitle">Tap on the header to expand or contract the group.</string>
<string name="carousel_subtitle">Swipe this section horizontally to scroll its items.</string>
<string name="swipe_to_delete">Swipe to delete</string>
<string name="drag_to_reorder">Drag to reorder</string>
<string name="updating_group">Updating group</string>
<string name="updating_group_subtitle">Click the shuffle icon to update the list with a random set of changes.</string>
<string name="title_activity_settings">SettingsActivity</string>
Expand Down
30 changes: 17 additions & 13 deletions example/src/main/java/com/xwray/groupie/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@ import com.xwray.groupie.example.core.SettingsActivity
import com.xwray.groupie.example.core.decoration.CarouselItemDecoration
import com.xwray.groupie.example.core.decoration.DebugItemDecoration
import com.xwray.groupie.example.core.decoration.SwipeTouchCallback
import com.xwray.groupie.example.item.CardItem
import com.xwray.groupie.example.item.CarouselCardItem
import com.xwray.groupie.example.item.CarouselItem
import com.xwray.groupie.example.item.ColumnItem
import com.xwray.groupie.example.item.FAVORITE
import com.xwray.groupie.example.item.FullBleedCardItem
import com.xwray.groupie.example.item.HeaderItem
import com.xwray.groupie.example.item.HeartCardItem
import com.xwray.groupie.example.item.SmallCardItem
import com.xwray.groupie.example.item.SwipeToDeleteItem
import com.xwray.groupie.example.item.UpdatableItem
import com.xwray.groupie.example.item.*
import com.xwray.groupie.groupiex.plusAssign
import kotlinx.android.synthetic.main.activity_main.*

Expand All @@ -56,6 +46,7 @@ class MainActivity : AppCompatActivity() {

private val infiniteLoadingSection = Section(HeaderItem(R.string.infinite_loading))
private val swipeSection = Section(HeaderItem(R.string.swipe_to_delete))
private val dragSection = Section(HeaderItem(R.string.drag_to_reorder))

// Normally there's no need to hold onto a reference to this list, but for demonstration
// purposes, we'll shuffle this list and post an update periodically
Expand Down Expand Up @@ -190,10 +181,15 @@ class MainActivity : AppCompatActivity() {

// Swipe to delete (with add button in header)
for (i in 0..2) {
swipeSection += SwipeToDeleteItem(rainbow200[6])
swipeSection += SwipeToDeleteItem(rainbow200[i])
}
groupAdapter += swipeSection

for (i in 0..4) {
dragSection += DraggableItem(rainbow500[i])
}
groupAdapter += dragSection

// Horizontal carousel
groupAdapter += Section(HeaderItem(R.string.carousel, R.string.carousel_subtitle)).apply {
add(makeCarouselItem())
Expand Down Expand Up @@ -373,7 +369,15 @@ class MainActivity : AppCompatActivity() {
object : SwipeTouchCallback(gray) {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder): Boolean {
return false
val item = groupAdapter.getItem(viewHolder.adapterPosition)
val targetItem = groupAdapter.getItem(target.adapterPosition)

val dragItems = dragSection.groups
val targetIndex = dragItems.indexOf(targetItem)
dragItems.remove(item)
dragItems.add(targetIndex, item)
dragSection.update(dragItems)
return true
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.xwray.groupie.example.item

import androidx.annotation.ColorInt
import androidx.recyclerview.widget.ItemTouchHelper

class DraggableItem(@ColorInt colorInt: Int) : CardItem(colorInt) {

override fun getDragDirs(): Int {
return ItemTouchHelper.UP or ItemTouchHelper.DOWN
}
}
9 changes: 9 additions & 0 deletions library/src/main/java/com/xwray/groupie/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ public void removeAll(@NonNull Collection<? extends Group> groups) {
refreshEmptyState();
}

/**
* Get the list of all groups in this section, wrapped in a new {@link ArrayList}. This
* does <strong>not include headers, footers or placeholders</strong>.
* @return The list of all groups in this section, wrapped in a new {@link ArrayList}
*/
public List<Group> getGroups() {
return new ArrayList<>(this.children);
}

/**
* Remove all existing body content.
*/
Expand Down

0 comments on commit 0910269

Please sign in to comment.