|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | * |
16 | | - * Last modified 2017-03-26 05:13:03 |
| 16 | + * Last modified 2017-04-09 21:38:26 |
17 | 17 | * |
18 | 18 | * GitHub: https://github.com/GcsSloop |
19 | 19 | * Website: http://www.gcssloop.com |
|
22 | 22 |
|
23 | 23 | package com.gcssloop.diycode.activity; |
24 | 24 |
|
25 | | -import android.content.Context; |
26 | | -import android.support.v4.util.ArrayMap; |
27 | | -import android.support.v4.widget.NestedScrollView; |
28 | | -import android.support.v4.widget.SwipeRefreshLayout; |
29 | | -import android.support.v7.widget.RecyclerView; |
| 25 | +import android.support.v4.app.FragmentManager; |
| 26 | +import android.support.v4.app.FragmentTransaction; |
30 | 27 | import android.view.View; |
31 | | -import android.widget.TextView; |
32 | 28 |
|
33 | 29 | import com.gcssloop.diycode.R; |
34 | | -import com.gcssloop.diycode.adapter.NotificationAdapter; |
35 | 30 | import com.gcssloop.diycode.base.app.BaseActivity; |
36 | 31 | import com.gcssloop.diycode.base.app.ViewHolder; |
37 | | -import com.gcssloop.diycode.utils.DataCache; |
38 | | -import com.gcssloop.diycode.utils.RecyclerViewUtil; |
39 | | -import com.gcssloop.diycode_sdk.api.Diycode; |
40 | | -import com.gcssloop.diycode_sdk.api.notifications.bean.Notification; |
41 | | -import com.gcssloop.diycode_sdk.api.notifications.event.GetNotificationsListEvent; |
42 | | - |
43 | | -import org.greenrobot.eventbus.EventBus; |
44 | | -import org.greenrobot.eventbus.Subscribe; |
45 | | -import org.greenrobot.eventbus.ThreadMode; |
46 | | - |
47 | | -import java.util.List; |
| 32 | +import com.gcssloop.diycode.fragment.NotificationsFragment; |
48 | 33 |
|
| 34 | +/** |
| 35 | + * 查看通知 |
| 36 | + */ |
49 | 37 | public class NotificationActivity extends BaseActivity { |
50 | 38 |
|
51 | | - // 底部状态显示 |
52 | | - private static final String FOOTER_LOADING = "loading..."; |
53 | | - private static final String FOOTER_NORMAL = "-- end --"; |
54 | | - private static final String FOOTER_ERROR = "-- 获取失败 --"; |
55 | | - private static final String FOOTER_NOT_LOGIN = "-- 未登录 --"; |
56 | | - private static final String FOOTER_NO_DATA = "-- 没有数据 --"; |
57 | | - private TextView mFooter; |
58 | | - |
59 | | - // 请求状态 - 下拉刷新 还是 加载更多 |
60 | | - private static final String POST_LOAD_MORE = "load_more"; |
61 | | - private static final String POST_REFRESH = "refresh"; |
62 | | - private ArrayMap<String, String> mPostTypes = new ArrayMap<>(); // 请求类型 |
63 | | - |
64 | | - // 当前状态 |
65 | | - private static final int STATE_NORMAL = 0; // 正常 |
66 | | - private static final int STATE_NO_MORE = 1; // 正在 |
67 | | - private static final int STATE_LOADING = 2; // 加载 |
68 | | - private static final int STATE_REFRESH = 3; // 刷新 |
69 | | - private int mState = STATE_NORMAL; |
70 | | - |
71 | | - // 分页加载 |
72 | | - private int pageIndex = 0; // 当面页码 |
73 | | - private int pageCount = 20; // 每页个数 |
74 | | - |
75 | | - // 数据 |
76 | | - private Diycode mDiycode; // 在线(服务器) |
77 | | - private DataCache mDataCache; // 缓存(本地) |
78 | | - |
79 | | - // View |
80 | | - private NotificationAdapter mAdapter; |
81 | | - private SwipeRefreshLayout mRefreshLayout; |
82 | | - |
83 | | - @Override |
84 | | - protected int getLayoutId() { |
85 | | - return R.layout.activity_recycler_refresh; |
86 | | - } |
87 | | - |
88 | | - @Override |
89 | | - protected void initViews(ViewHolder holder, View root) { |
90 | | - setTitle("通知"); |
91 | | - mDiycode = Diycode.getSingleInstance(); |
92 | | - mDataCache = new DataCache(this); |
93 | | - mFooter = holder.get(R.id.footer); |
94 | | - |
95 | | - initRefreshLayout(holder); |
96 | | - initRecyclerView(this, holder); |
97 | | - initListener(holder); |
98 | | - |
99 | | - if (!mDiycode.isLogin()) { |
100 | | - toastShort("用户未登录"); |
101 | | - mRefreshLayout.setEnabled(false); |
102 | | - mFooter.setText(FOOTER_NOT_LOGIN); |
103 | | - openActivity(LoginActivity.class); |
104 | | - finish(); |
105 | | - } |
106 | | - loadMore(); |
107 | | - } |
108 | | - |
109 | | - private void initRefreshLayout(ViewHolder holder) { |
110 | | - mRefreshLayout = holder.get(R.id.refresh_layout); |
111 | | - mRefreshLayout.setProgressViewOffset(false, -20, 80); |
112 | | - mRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.diy_red)); |
113 | | - mRefreshLayout.setEnabled(false); |
114 | | - } |
115 | | - |
116 | | - private void initRecyclerView(final Context context, ViewHolder holder) { |
117 | | - mAdapter = new NotificationAdapter(context); |
118 | | - RecyclerView recyclerView = holder.get(R.id.recycler_view); |
119 | | - RecyclerViewUtil.init(context, recyclerView, mAdapter); |
120 | | - } |
121 | | - |
122 | | - private void initListener(ViewHolder holder) { |
123 | | - // 监听 RefreshLayout 下拉刷新 |
124 | | - mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { |
125 | | - @Override |
126 | | - public void onRefresh() { |
127 | | - refresh(); |
128 | | - } |
129 | | - }); |
130 | | - // 监听 scrollView 加载更多 |
131 | | - NestedScrollView scrollView = holder.get(R.id.scroll_view); |
132 | | - scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { |
133 | | - @Override |
134 | | - public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { |
135 | | - View childView = v.getChildAt(0); |
136 | | - if (scrollY == (childView.getHeight() - v.getHeight()) && mState == STATE_NORMAL) { //滑动到底部 && 正常模式 |
137 | | - loadMore(); |
138 | | - } |
139 | | - } |
140 | | - }); |
141 | | - } |
142 | | - |
143 | | - // 刷新 |
144 | | - private void refresh() { |
145 | | - pageIndex = 0; |
146 | | - String uuid = mDiycode.getNotificationsList(pageIndex * pageCount, pageCount); |
147 | | - pageIndex++; |
148 | | - mPostTypes.put(uuid, POST_REFRESH); |
149 | | - mState = STATE_REFRESH; |
150 | | - } |
151 | | - |
152 | | - // 加载更多 |
153 | | - private void loadMore() { |
154 | | - String uuid = mDiycode.getNotificationsList(pageIndex * pageCount, pageCount); |
155 | | - pageIndex++; |
156 | | - mPostTypes.put(uuid, POST_LOAD_MORE); |
157 | | - mState = STATE_LOADING; |
158 | | - mFooter.setText(FOOTER_LOADING); |
159 | | - } |
160 | | - |
161 | | - private void onRefresh(List<Notification> beans) { |
162 | | - mState = STATE_NORMAL; |
163 | | - mRefreshLayout.setRefreshing(false); |
164 | | - mAdapter.clearDatas(); |
165 | | - mAdapter.addDatas(beans); |
166 | | - toastShort("数据刷新成功"); |
167 | | - if (beans.size() == 0) { |
168 | | - mFooter.setText(FOOTER_NO_DATA); |
169 | | - } |
170 | | - } |
171 | | - |
172 | | - private void onLoadMore(List<Notification> beans) { |
173 | | - if (beans.size() == 0) { |
174 | | - mState = STATE_NORMAL; |
175 | | - mFooter.setText(FOOTER_NO_DATA); |
176 | | - return; |
177 | | - } else if (beans.size() < pageCount) { |
178 | | - mState = STATE_NO_MORE; |
179 | | - mFooter.setText(FOOTER_NORMAL); |
180 | | - } else { |
181 | | - mState = STATE_NORMAL; |
182 | | - mFooter.setText(FOOTER_NORMAL); |
183 | | - } |
184 | | - mAdapter.addDatas(beans); |
185 | | - mRefreshLayout.setEnabled(true); |
186 | | - } |
187 | | - |
188 | | - // 数据加载出现异常 |
189 | | - private void onError(String postType, String errorType) { |
190 | | - mState = STATE_NORMAL; // 状态重置为正常,以便可以重试,否则进入异常状态后无法再变为正常状态 |
191 | | - |
192 | | - if (postType.equals(POST_LOAD_MORE)) { |
193 | | - mFooter.setText(FOOTER_ERROR); |
194 | | - toastShort("加载更多失败: " + errorType); |
195 | | - } else if (postType.equals(POST_REFRESH)) { |
196 | | - mRefreshLayout.setRefreshing(false); |
197 | | - toastShort("刷新数据失败: " + errorType); |
198 | | - } |
199 | | - } |
200 | | - |
201 | | - @Subscribe(threadMode = ThreadMode.MAIN) |
202 | | - public void onNotificationList(GetNotificationsListEvent event) { |
203 | | - String postType = mPostTypes.get(event.getUUID()); |
204 | | - if (event.isOk()) { |
205 | | - if (postType.equals(POST_LOAD_MORE)) { |
206 | | - onLoadMore(event.getBean()); |
207 | | - } else if (postType.equals(POST_REFRESH)) { |
208 | | - onRefresh(event.getBean()); |
209 | | - } |
210 | | - } else { |
211 | | - onError(postType, event.getCodeDescribe()); |
212 | | - } |
213 | | - mPostTypes.remove(event.getUUID()); |
214 | | - } |
215 | | - |
216 | | - @Override |
217 | | - public void onStart() { |
218 | | - super.onStart(); |
219 | | - EventBus.getDefault().register(this); |
| 39 | + @Override protected int getLayoutId() { |
| 40 | + return R.layout.activity_fragment; |
220 | 41 | } |
221 | 42 |
|
222 | | - @Override |
223 | | - public void onStop() { |
224 | | - super.onStop(); |
225 | | - EventBus.getDefault().unregister(this); |
| 43 | + @Override protected void initViews(ViewHolder holder, View root) { |
| 44 | + NotificationsFragment fragment = NotificationsFragment.newInstance(); |
| 45 | + FragmentManager fragmentManager = getSupportFragmentManager(); |
| 46 | + FragmentTransaction transaction = fragmentManager.beginTransaction(); |
| 47 | + transaction.add(R.id.fragment, fragment); |
| 48 | + transaction.commit(); |
226 | 49 | } |
227 | 50 | } |
0 commit comments