-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
327 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
library/src/main/java/dev/auxility/baseadapter/EndlessAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package dev.auxility.baseadapter; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.io.Serializable; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
|
||
import dev.auxility.baseadapter.item.Item; | ||
import dev.auxility.baseadapter.misc.function.Predicate; | ||
|
||
public class EndlessAdapter<V extends Item> extends AbstractAdapterDecorator<V> { | ||
|
||
private final int threshold; | ||
private final OnLoadMoreListener listener; | ||
private boolean inProgress = false; | ||
private boolean complete = false; | ||
|
||
public EndlessAdapter( | ||
@NonNull AbstractAdapter<V> decoratedAdapter, | ||
int threshold, | ||
OnLoadMoreListener listener | ||
) { | ||
super(decoratedAdapter); | ||
this.threshold = threshold; | ||
this.listener = listener; | ||
} | ||
|
||
public EndlessAdapter(int threshold, OnLoadMoreListener listener) { | ||
this(new BaseAdapter<V>(), threshold, listener); | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
int size = getAdapter().getSize(); | ||
if (size == 0) { | ||
checkThreshold(0); | ||
} | ||
return size; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public ListIterator<V> listIterator(int index) { | ||
return getAdapter().listIterator(index); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public V get(int index) { | ||
checkThreshold(index); | ||
return getAdapter().get(index); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public List<V> items() { | ||
return getAdapter().items(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public V remove(int index) { | ||
return getAdapter().remove(index); | ||
} | ||
|
||
@Override | ||
public boolean removeIf(@NonNull Predicate<V> predicate, boolean withDiffUtil) { | ||
return getAdapter().removeIf(predicate, withDiffUtil); | ||
} | ||
|
||
@Override | ||
public List<V> removeRange(int beginIndex, int endIndex) { | ||
return getAdapter().removeRange(beginIndex, endIndex); | ||
} | ||
|
||
@Override | ||
public void clear(boolean withDiffUtil) { | ||
getAdapter().clear(); | ||
} | ||
|
||
@Override | ||
public void add(int index, @NonNull V element) { | ||
getAdapter().add(index, element); | ||
} | ||
|
||
@Override | ||
public boolean addAll(int index, @NonNull Collection<? extends V> c) { | ||
return getAdapter().addAll(index, c); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public V set(int index, @NonNull V element) { | ||
return getAdapter().set(index, element); | ||
} | ||
|
||
@Override | ||
public void set(@NonNull Collection<? extends V> c, boolean withDiffUtil) { | ||
getAdapter().set(c, withDiffUtil); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Iterator<V> iterator() { | ||
return getAdapter().iterator(); | ||
} | ||
|
||
public int getThreshold() { | ||
return threshold; | ||
} | ||
|
||
public boolean isInProgress() { | ||
return inProgress; | ||
} | ||
|
||
public void setInProgress(boolean inProgress) { | ||
this.inProgress = inProgress; | ||
} | ||
|
||
public boolean isComplete() { | ||
return complete; | ||
} | ||
|
||
public void setComplete(boolean complete) { | ||
this.complete = complete; | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
getAdapter().refresh(); | ||
} | ||
|
||
private void checkThreshold(int index) { | ||
if (!isComplete() && !isInProgress() && index >= getAdapter().getSize() - threshold) { | ||
listener.onLoadMore(getAdapter().getSize()); | ||
} | ||
} | ||
|
||
public interface OnLoadMoreListener extends Serializable { | ||
|
||
void onLoadMore(int currentSize); | ||
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
sample/src/main/java/dev/auxility/baseadapter/sample/EndlessAdapterSampleFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.auxility.baseadapter.sample | ||
|
||
import androidx.lifecycle.ViewModelProviders | ||
|
||
class EndlessAdapterSampleFragment : TabbedFragment() { | ||
override val titleRes: Int = R.string.endless_adapter_sample | ||
|
||
override val viewModel: TabbedViewModel by lazy { | ||
ViewModelProviders.of(this) | ||
.get(EndlessAdapterViewModel::class.java) | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
sample/src/main/java/dev/auxility/baseadapter/sample/EndlessAdapterViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.auxility.baseadapter.sample | ||
|
||
import androidx.lifecycle.viewModelScope | ||
import dev.auxility.baseadapter.Adapter | ||
import dev.auxility.baseadapter.BaseAdapter | ||
import dev.auxility.baseadapter.item.TitledItem | ||
|
||
class EndlessAdapterViewModel : TabbedViewModel() { | ||
|
||
override val adapter: Adapter<TitledItem> = BaseAdapter( | ||
listOf(EndlessRVItem(viewModelScope)) | ||
) | ||
} |
40 changes: 40 additions & 0 deletions
40
sample/src/main/java/dev/auxility/baseadapter/sample/EndlessRVItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package dev.auxility.baseadapter.sample | ||
|
||
import android.util.Log | ||
import dev.auxility.baseadapter.EndlessAdapter | ||
import dev.auxility.baseadapter.item.Item | ||
import dev.auxility.baseadapter.item.TitledItem | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
const val THRESHOLD = 10 | ||
const val PAGE_SIZE = 20 | ||
const val TOTAL_COUNT = 100 | ||
|
||
class EndlessRVItem(private val scope: CoroutineScope) : TitledItem, EndlessAdapter.OnLoadMoreListener { | ||
|
||
val adapter: EndlessAdapter<Item> = EndlessAdapter(THRESHOLD, this) | ||
|
||
override fun onLoadMore(currentSize: Int) { | ||
Log.d("mytag", "loading $PAGE_SIZE items starting at $currentSize from $TOTAL_COUNT") | ||
if (currentSize < TOTAL_COUNT) { | ||
adapter.isInProgress = true | ||
scope.launch { | ||
adapter.add(ProgressItem()) | ||
delay(2000) | ||
adapter.remove(adapter.size - 1) | ||
adapter.addAll(List(PAGE_SIZE) { | ||
TestItem(currentSize + it) | ||
}) | ||
adapter.isInProgress = false | ||
} | ||
} else { | ||
adapter.isComplete = true | ||
} | ||
} | ||
|
||
override fun getLayoutId(): Int = R.layout.item_endless_rv | ||
|
||
override fun getTitle(): String = "RecyclerView" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
sample/src/main/java/dev/auxility/baseadapter/sample/ProgressItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dev.auxility.baseadapter.sample | ||
|
||
import dev.auxility.baseadapter.item.Item | ||
|
||
class ProgressItem : Item { | ||
override fun getLayoutId(): Int = R.layout.item_progress | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<data> | ||
|
||
<variable | ||
name="item" | ||
type="dev.auxility.baseadapter.sample.EndlessRVItem" /> | ||
</data> | ||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/recyclerView" | ||
adapter="@{item.adapter}" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" | ||
tools:listitem="@layout/item_rv_sample" /> | ||
|
||
</layout> |
Oops, something went wrong.