This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 468
/
BaseActivity.java
125 lines (101 loc) · 4.13 KB
/
BaseActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.malinskiy.superrecyclerview.sample;
import com.malinskiy.superrecyclerview.OnMoreListener;
import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.malinskiy.superrecyclerview.swipe.SparseItemRemoveAnimator;
import com.malinskiy.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import java.util.ArrayList;
public abstract class BaseActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener, OnMoreListener, SwipeDismissRecyclerViewTouchListener.DismissCallbacks {
private SuperRecyclerView mRecycler;
private StringListAdapter mAdapter;
private SparseItemRemoveAnimator mSparseAnimator;
private RecyclerView.LayoutManager mLayoutManager;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
ArrayList<String> list = new ArrayList<>();
mAdapter = new StringListAdapter(list);
mRecycler = (SuperRecyclerView) findViewById(R.id.list);
mLayoutManager = getLayoutManager();
mRecycler.setLayoutManager(mLayoutManager);
mRecycler.addItemDecoration(new PaddingItemDecoration());
boolean dismissEnabled = isSwipeToDismissEnabled();
if (dismissEnabled) {
mRecycler.setupSwipeToDismiss(this);
mSparseAnimator = new SparseItemRemoveAnimator();
mRecycler.getRecyclerView().setItemAnimator(mSparseAnimator);
}
mHandler = new Handler(Looper.getMainLooper());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mRecycler.setAdapter(mAdapter);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.addAll(new String[]{"More stuff", "More stuff", "More stuff"});
}
});
}
});
thread.start();
mRecycler.setRefreshListener(this);
mRecycler.setRefreshingColorResources(android.R.color.holo_orange_light, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_red_light);
mRecycler.setupMoreListener(this, 1);
}
protected abstract int getLayoutId();
protected abstract boolean isSwipeToDismissEnabled();
protected abstract RecyclerView.LayoutManager getLayoutManager();
@Override
public void onRefresh() {
Toast.makeText(this, "Refresh", Toast.LENGTH_LONG).show();
mHandler.postDelayed(new Runnable() {
public void run() {
mAdapter.add("New stuff");
}
}, 2000);
}
@Override
public void onMoreAsked(int numberOfItems, int numberBeforeMore, int currentItemPos) {
Toast.makeText(this, "More", Toast.LENGTH_LONG).show();
mHandler.postDelayed(new Runnable() {
public void run() {
mAdapter.add("More asked, more served");
}
}, 300);
}
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mSparseAnimator.setSkipNext(true);
mAdapter.remove(position);
}
}
}