Skip to content

Commit 2b126c7

Browse files
committed
重构 News
1 parent cd10b41 commit 2b126c7

4 files changed

Lines changed: 124 additions & 25 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2017 GcsSloop
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Last modified 2017-04-09 05:15:40
17+
*
18+
* GitHub: https://github.com/GcsSloop
19+
* Website: http://www.gcssloop.com
20+
* Weibo: http://weibo.com/GcsSloop
21+
*/
22+
23+
package com.gcssloop.diycode.fragment;
24+
25+
import android.content.Context;
26+
import android.support.annotation.NonNull;
27+
import android.support.v7.widget.RecyclerView;
28+
29+
import com.gcssloop.diycode.base.recyclerview.SpeedyLinearLayoutManager;
30+
import com.gcssloop.diycode.fragment.provider.NewsProvider;
31+
import com.gcssloop.diycode_sdk.api.base.event.BaseEvent;
32+
import com.gcssloop.diycode_sdk.api.news.bean.New;
33+
import com.gcssloop.recyclerview.adapter.multitype.HeaderFooterAdapter;
34+
35+
public class NewsListFragment2 extends RefreshRecyclerFragment {
36+
37+
@Override public void initData(HeaderFooterAdapter adapter) {
38+
39+
}
40+
41+
@Override protected void setRecyclerViewAdapter(Context context, RecyclerView recyclerView,
42+
HeaderFooterAdapter adapter) {
43+
adapter.register(New.class, new NewsProvider(getContext()));
44+
}
45+
46+
@NonNull @Override protected RecyclerView.LayoutManager getRecyclerViewLayoutManager() {
47+
return new SpeedyLinearLayoutManager(getContext());
48+
}
49+
50+
@NonNull @Override protected String request(int offset, int limit) {
51+
return mDiycode.getNewsList(null, offset,limit);
52+
}
53+
54+
@Override protected void onLoadMore(BaseEvent event, HeaderFooterAdapter adapter) {
55+
56+
}
57+
58+
@Override protected void onRefresh(BaseEvent event, HeaderFooterAdapter adapter) {
59+
60+
}
61+
62+
@Override protected void onError(BaseEvent event, String postType) {
63+
64+
}
65+
}

diycode-app/src/main/java/com/gcssloop/diycode/fragment/TopicListFragment.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import android.support.v7.widget.RecyclerView;
2929
import android.view.View;
3030

31-
import com.gcssloop.diycode.R;
3231
import com.gcssloop.diycode.base.recyclerview.SpeedyLinearLayoutManager;
3332
import com.gcssloop.diycode.fragment.provider.TopicProvider;
3433
import com.gcssloop.diycode_sdk.api.topic.bean.Topic;
@@ -50,8 +49,7 @@ public static TopicListFragment newInstance() {
5049
return fragment;
5150
}
5251

53-
@Override
54-
public void initData(HeaderFooterAdapter adapter) {
52+
@Override public void initData(HeaderFooterAdapter adapter) {
5553
List<Topic> topics = mDataCache.getTopicsList();
5654
if (null != topics && topics.size() > 0) {
5755
Logger.e("topics : " + topics.size());
@@ -68,43 +66,34 @@ public void initData(HeaderFooterAdapter adapter) {
6866
}
6967
}
7068

71-
@Override
72-
protected void setRecyclerViewAdapter(Context context, RecyclerView recyclerView,
73-
HeaderFooterAdapter adapter) {
74-
TopicProvider topicProvider = new TopicProvider(getContext(), R.layout.item_topic);
75-
adapter.register(Topic.class, topicProvider);
69+
@Override protected void setRecyclerViewAdapter(Context context, RecyclerView recyclerView,
70+
HeaderFooterAdapter adapter) {
71+
adapter.register(Topic.class, new TopicProvider(getContext()));
7672
}
7773

78-
@NonNull
79-
@Override
80-
protected RecyclerView.LayoutManager getRecyclerViewLayoutManager() {
74+
@NonNull @Override protected RecyclerView.LayoutManager getRecyclerViewLayoutManager() {
8175
return new SpeedyLinearLayoutManager(getContext());
8276
}
8377

84-
@NonNull
85-
@Override
86-
protected String request(int offset, int limit) {
78+
@NonNull @Override protected String request(int offset, int limit) {
8779
return mDiycode.getTopicsList(null, null, offset, limit);
8880
}
8981

90-
@Override
91-
protected void onRefresh(GetTopicsListEvent event, HeaderFooterAdapter adapter) {
82+
@Override protected void onRefresh(GetTopicsListEvent event, HeaderFooterAdapter adapter) {
9283
adapter.clearDatas();
9384
adapter.addDatas(event.getBean());
9485
toast("刷新成功");
9586
mDataCache.saveTopicsList(convert(adapter.getDatas()));
9687
}
9788

98-
@Override
99-
protected void onLoadMore(GetTopicsListEvent event, HeaderFooterAdapter adapter) {
89+
@Override protected void onLoadMore(GetTopicsListEvent event, HeaderFooterAdapter adapter) {
10090
// TODO 排除重复数据
10191
adapter.addDatas(event.getBean());
10292
toast("加载更多成功");
10393
mDataCache.saveTopicsList(convert(adapter.getDatas()));
10494
}
10595

106-
@Override
107-
protected void onError(GetTopicsListEvent event, String postType) {
96+
@Override protected void onError(GetTopicsListEvent event, String postType) {
10897
if (postType.equals(POST_LOAD_MORE)) {
10998
toast("加载更多失败");
11099
} else if (postType.equals(POST_REFRESH)) {
@@ -121,8 +110,7 @@ public ArrayList<Topic> convert(List<Object> objects) {
121110
return topics;
122111
}
123112

124-
@Override
125-
public void onDestroyView() {
113+
@Override public void onDestroyView() {
126114
super.onDestroyView();
127115
saveState();
128116
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2017 GcsSloop
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Last modified 2017-04-09 05:13:31
17+
*
18+
* GitHub: https://github.com/GcsSloop
19+
* Website: http://www.gcssloop.com
20+
* Weibo: http://weibo.com/GcsSloop
21+
*/
22+
23+
package com.gcssloop.diycode.fragment.provider;
24+
25+
import android.content.Context;
26+
import android.support.annotation.NonNull;
27+
28+
import com.gcssloop.diycode.R;
29+
import com.gcssloop.diycode_sdk.api.news.bean.New;
30+
import com.gcssloop.recyclerview.adapter.base.RecyclerViewHolder;
31+
import com.gcssloop.recyclerview.adapter.multitype.BaseViewProvider;
32+
33+
public class NewsProvider extends BaseViewProvider<New> {
34+
public NewsProvider(@NonNull Context context) {
35+
super(context, R.layout.item_news);
36+
}
37+
38+
/**
39+
* 在绑定数据时调用,需要用户自己处理
40+
*
41+
* @param holder ViewHolder
42+
* @param bean 数据
43+
*/
44+
@Override public void onBindView(RecyclerViewHolder holder, New bean) {
45+
46+
}
47+
}

diycode-app/src/main/java/com/gcssloop/diycode/fragment/provider/TopicProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import android.content.Context;
2626
import android.content.Intent;
27-
import android.support.annotation.LayoutRes;
2827
import android.support.annotation.NonNull;
2928
import android.view.View;
3029
import android.widget.ImageView;
@@ -43,8 +42,8 @@
4342
public class TopicProvider extends BaseViewProvider<Topic> {
4443
private Context mContext;
4544

46-
public TopicProvider(@NonNull Context context, @NonNull @LayoutRes int layout_id) {
47-
super(context, layout_id);
45+
public TopicProvider(@NonNull Context context) {
46+
super(context, R.layout.item_topic);
4847
mContext = context;
4948
}
5049

0 commit comments

Comments
 (0)