1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 *
16- * Last modified 2017-03-23 00:21:12
16+ * Last modified 2017-04-09 20:47:16
1717 *
1818 * GitHub: https://github.com/GcsSloop
1919 * Website: http://www.gcssloop.com
2424
2525import android .content .Context ;
2626import android .content .Intent ;
27- import android .support .v4 .util .ArrayMap ;
28- import android .support .v4 .widget .NestedScrollView ;
29- import android .support .v4 .widget .SwipeRefreshLayout ;
30- import android .support .v7 .widget .RecyclerView ;
27+ import android .support .v4 .app .FragmentManager ;
28+ import android .support .v4 .app .FragmentTransaction ;
3129import android .view .View ;
32- import android .widget .TextView ;
3330
3431import com .gcssloop .diycode .R ;
35- import com .gcssloop .diycode .adapter .TopicAdapter ;
3632import com .gcssloop .diycode .base .app .BaseActivity ;
3733import com .gcssloop .diycode .base .app .ViewHolder ;
38- import com .gcssloop .diycode .base .recyclerview .GcsAdapter ;
34+ import com .gcssloop .diycode .fragment .UserCollectionTopicFragment ;
35+ import com .gcssloop .diycode .fragment .UserCreateTopicFragment ;
3936import com .gcssloop .diycode .utils .DataCache ;
40- import com .gcssloop .diycode .utils .RecyclerViewUtil ;
4137import com .gcssloop .diycode_sdk .api .Diycode ;
42- import com .gcssloop .diycode_sdk .api .topic .bean .Topic ;
4338import com .gcssloop .diycode_sdk .api .user .bean .UserDetail ;
44- import com .gcssloop .diycode_sdk .api .user .event .GetUserCollectionTopicListEvent ;
45- import com .gcssloop .diycode_sdk .api .user .event .GetUserCreateTopicListEvent ;
46-
47- import org .greenrobot .eventbus .EventBus ;
48- import org .greenrobot .eventbus .Subscribe ;
49- import org .greenrobot .eventbus .ThreadMode ;
5039
5140import java .io .IOException ;
52- import java .util .List ;
5341
5442public class MyTopicActivity extends BaseActivity {
55- // 底部状态显示
56- private static final String FOOTER_LOADING = "loading..." ;
57- private static final String FOOTER_NORMAL = "-- end --" ;
58- private static final String FOOTER_ERROR = "-- 获取失败 --" ;
59- private static final String FOOTER_NOT_LOGIN = "-- 未登录 --" ;
60- private static final String FOOTER_NO_DATA = "-- 没有数据 --" ;
61- private TextView mFooter ;
62-
63- // 请求状态 - 下拉刷新 还是 加载更多
64- private static final String POST_LOAD_MORE = "load_more" ;
65- private static final String POST_REFRESH = "refresh" ;
66- private ArrayMap <String , String > mPostTypes = new ArrayMap <>(); // 请求类型
67-
68- // 当前状态
69- private static final int STATE_NORMAL = 0 ; // 正常
70- private static final int STATE_NO_MORE = 1 ; // 正在
71- private static final int STATE_LOADING = 2 ; // 加载
72- private static final int STATE_REFRESH = 3 ; // 刷新
73- private int mState = STATE_NORMAL ;
74-
75- // 分页加载
76- private int pageIndex = 0 ; // 当面页码
77- private int pageCount = 20 ; // 每页个数
78-
79- // 数据
80- private Diycode mDiycode ; // 在线(服务器)
81- private DataCache mDataCache ; // 缓存(本地)
82-
83- // View
84- private GcsAdapter <Topic > mAdapter ;
85- private SwipeRefreshLayout mRefreshLayout ;
43+ private DataCache mDataCache ;
8644
8745 // 数据类型
8846 enum InfoType {
@@ -99,11 +57,13 @@ public static void newInstance(Context context, InfoType type) {
9957
10058 @ Override
10159 protected int getLayoutId () {
102- return R .layout .activity_recycler_refresh ;
60+ return R .layout .activity_topic ;
10361 }
10462
10563 @ Override
10664 protected void initDatas () {
65+ mDiycode = Diycode .getSingleInstance ();
66+ mDataCache = new DataCache (this );
10767 Intent intent = getIntent ();
10868 InfoType type = (InfoType ) intent .getSerializableExtra ("type" );
10969 if (type != null ) {
@@ -115,26 +75,12 @@ protected void initDatas() {
11575
11676 @ Override
11777 protected void initViews (ViewHolder holder , View root ) {
118- mDiycode = Diycode .getSingleInstance ();
119- mDataCache = new DataCache (this );
120- mFooter = holder .get (R .id .footer );
121-
122- initRefreshLayout (holder );
123- initRecyclerView (this , holder );
124- initListener (holder );
125-
126- if (current_type == InfoType .MY_COLLECT ){
127- setTitle ("我的收藏" );
128- } else if (current_type == InfoType .MY_TOPIC ){
129- setTitle ("我的话题" );
130- }
131-
13278 if (!mDiycode .isLogin ()) {
13379 toastShort ("用户未登录" );
134- mRefreshLayout .setEnabled (false );
135- mFooter .setText (FOOTER_NOT_LOGIN );
13680 return ;
13781 }
82+
83+ // 获取用户名
13884 if (mDataCache .getMe () == null ) {
13985 try {
14086 UserDetail me = mDiycode .getMeNow ();
@@ -143,160 +89,21 @@ protected void initViews(ViewHolder holder, View root) {
14389 e .printStackTrace ();
14490 }
14591 }
146-
147- loadMore ();
148- mFooter .setText (FOOTER_LOADING );
149- }
150-
151- private void initRefreshLayout (ViewHolder holder ) {
152- mRefreshLayout = holder .get (R .id .refresh_layout );
153- mRefreshLayout .setProgressViewOffset (false , -20 , 80 );
154- mRefreshLayout .setColorSchemeColors (getResources ().getColor (R .color .diy_red ));
155- mRefreshLayout .setEnabled (false );
156- }
157-
158- private void initRecyclerView (final Context context , ViewHolder holder ) {
159- mAdapter = new TopicAdapter (context , mDataCache );
160- RecyclerView recyclerView = holder .get (R .id .recycler_view );
161- RecyclerViewUtil .init (context , recyclerView , mAdapter );
162- }
163-
164- private void initListener (ViewHolder holder ) {
165- // 监听 RefreshLayout 下拉刷新
166- mRefreshLayout .setOnRefreshListener (new SwipeRefreshLayout .OnRefreshListener () {
167- @ Override
168- public void onRefresh () {
169- refresh ();
170- }
171- });
172- // 监听 scrollView 加载更多
173- NestedScrollView scrollView = holder .get (R .id .scroll_view );
174- scrollView .setOnScrollChangeListener (new NestedScrollView .OnScrollChangeListener () {
175- @ Override
176- public void onScrollChange (NestedScrollView v , int scrollX , int scrollY , int oldScrollX , int oldScrollY ) {
177- View childView = v .getChildAt (0 );
178- if (scrollY == (childView .getHeight () - v .getHeight ()) && mState == STATE_NORMAL ) { //滑动到底部 && 正常模式
179- loadMore ();
180- }
181- }
182- });
183- }
184-
185- // 刷新
186- private void refresh () {
187- if (!mDiycode .isLogin ())
188- return ;
189- pageIndex = 0 ;
190- String uuid = getData ();
191- mPostTypes .put (uuid , POST_REFRESH );
192- mState = STATE_REFRESH ;
193- }
194-
195- // 加载更多
196- private void loadMore () {
197- if (!mDiycode .isLogin ())
198- return ;
199- String uuid = getData ();
200- mPostTypes .put (uuid , POST_LOAD_MORE );
201- mState = STATE_LOADING ;
202- mFooter .setText (FOOTER_LOADING );
203- }
204-
205- private String getData () {
206- String uuid = "uuid" ;
20792 String username = mDataCache .getMe ().getLogin ();
208- switch (current_type ) {
209- case MY_TOPIC :
210- uuid = mDiycode .getUserCreateTopicList (username , null , pageIndex * pageCount , pageCount );
211- break ;
212- case MY_COLLECT :
213- uuid = mDiycode .getUserCollectionTopicList (username , pageIndex * pageCount , pageCount );
214- break ;
215- default :
216- toastShort ("类型错误" );
217- }
218- pageIndex ++;
219- return uuid ;
220- }
221-
222- private void onRefresh (List <Topic > topics ) {
223- mState = STATE_NORMAL ;
224- mRefreshLayout .setRefreshing (false );
225- mAdapter .clearDatas ();
226- mAdapter .addDatas (topics );
227- toastShort ("数据刷新成功" );
228- if (topics .size () == 0 ) {
229- mFooter .setText (FOOTER_NO_DATA );
230- }
231- }
232-
233- private void onLoadMore (List <Topic > topics ) {
234- if (topics .size () == 0 ) {
235- mState = STATE_NORMAL ;
236- mFooter .setText (FOOTER_NO_DATA );
237- return ;
238- } else if (topics .size () < pageCount ) {
239- mState = STATE_NO_MORE ;
240- mFooter .setText (FOOTER_NORMAL );
241- } else {
242- mState = STATE_NORMAL ;
243- mFooter .setText (FOOTER_NORMAL );
244- }
245- mAdapter .addDatas (topics );
246- mRefreshLayout .setEnabled (true );
247- }
248-
249- // 数据加载出现异常
250- private void onError (String postType ) {
251- mState = STATE_NORMAL ; // 状态重置为正常,以便可以重试,否则进入异常状态后无法再变为正常状态
252- if (postType .equals (POST_LOAD_MORE )) {
253- mFooter .setText (FOOTER_ERROR );
254- toastShort ("加载更多失败" );
255- } else if (postType .equals (POST_REFRESH )) {
256- mRefreshLayout .setRefreshing (false );
257- toastShort ("刷新数据失败" );
258- }
259- }
26093
261- @ Subscribe (threadMode = ThreadMode .MAIN )
262- public void onUserCreateTopicList (GetUserCreateTopicListEvent event ) {
263- String postType = mPostTypes .get (event .getUUID ());
264- if (event .isOk ()) {
265- if (postType .equals (POST_LOAD_MORE )) {
266- onLoadMore (event .getBean ());
267- } else if (postType .equals (POST_REFRESH )) {
268- onRefresh (event .getBean ());
269- }
270- } else {
271- onError (postType );
272- }
273- mPostTypes .remove (event .getUUID ());
274- }
94+ // 初始化 Fragment
95+ FragmentManager fragmentManager = getSupportFragmentManager ();
96+ FragmentTransaction transaction = fragmentManager .beginTransaction ();
27597
276- @ Subscribe (threadMode = ThreadMode .MAIN )
277- public void onUserCollectTopicList (GetUserCollectionTopicListEvent event ) {
278- String postType = mPostTypes .get (event .getUUID ());
279- if (event .isOk ()) {
280- if (postType .equals (POST_LOAD_MORE )) {
281- onLoadMore (event .getBean ());
282- } else if (postType .equals (POST_REFRESH )) {
283- onRefresh (event .getBean ());
284- }
285- } else {
286- onError (postType );
98+ if (current_type == InfoType .MY_COLLECT ){
99+ setTitle ("我的收藏" );
100+ UserCollectionTopicFragment fragment1 = UserCollectionTopicFragment .newInstance (username );
101+ transaction .add (R .id .fragment , fragment1 );
102+ } else if (current_type == InfoType .MY_TOPIC ){
103+ setTitle ("我的话题" );
104+ UserCreateTopicFragment fragment2 = UserCreateTopicFragment .newInstance (username );
105+ transaction .add (R .id .fragment , fragment2 );
287106 }
288- mPostTypes .remove (event .getUUID ());
289- }
290-
291- @ Override
292- public void onStart () {
293- super .onStart ();
294- EventBus .getDefault ().register (this );
295- }
296-
297- @ Override
298- public void onStop () {
299- super .onStop ();
300- EventBus .getDefault ().unregister (this );
107+ transaction .commit ();
301108 }
302109}
0 commit comments