searchHistories) throws Exception {
70 | GankBus.response(new Result(GankResultCode.HISTORY_LIST_QUERYED, searchHistories));
71 | }
72 | })
73 | );
74 | }
75 |
76 | @Override
77 | public void deleteAll() {
78 | getDataManager().deleteSearchHistory();
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/util/DataCleanManager.java:
--------------------------------------------------------------------------------
1 | package com.gank.util;
2 |
3 | import android.content.Context;
4 | import android.os.Environment;
5 |
6 | import java.io.File;
7 |
8 | /**
9 | * Created by Administrator on 2017/6/3 0003.
10 | */
11 |
12 | public class DataCleanManager {
13 | /** * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * @param context */
14 | public static void cleanInternalCache(Context context) {
15 | deleteFilesByDirectory(context.getCacheDir());
16 | }
17 |
18 | /** * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * @param context */
19 | public static void cleanDatabases(Context context) {
20 | deleteFilesByDirectory(new File("/data/data/"
21 | + context.getPackageName() + "/databases"));
22 | }
23 |
24 | /**
25 | * * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param
26 | * context
27 | */
28 | public static void cleanSharedPreference(Context context) {
29 | deleteFilesByDirectory(new File("/data/data/"
30 | + context.getPackageName() + "/shared_prefs"));
31 | }
32 |
33 | /** * 按名字清除本应用数据库 * * @param context * @param dbName */
34 | public static void cleanDatabaseByName(Context context, String dbName) {
35 | context.deleteDatabase(dbName);
36 | }
37 |
38 | /** * 清除/data/data/com.xxx.xxx/files下的内容 * * @param context */
39 | public static void cleanFiles(Context context) {
40 | deleteFilesByDirectory(context.getFilesDir());
41 | }
42 |
43 | /**
44 | * * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param
45 | * context
46 | */
47 | public static void cleanExternalCache(Context context) {
48 | if (Environment.getExternalStorageState().equals(
49 | Environment.MEDIA_MOUNTED)) {
50 | deleteFilesByDirectory(context.getExternalCacheDir());
51 | }
52 | }
53 |
54 | /** * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除 * * @param filePath */
55 | public static void cleanCustomCache(String filePath) {
56 | deleteFilesByDirectory(new File(filePath));
57 | }
58 |
59 | /** * 清除本应用所有的数据 * * @param context * @param filepath */
60 | public static void cleanApplicationData(Context context, String... filepath) {
61 | cleanInternalCache(context);
62 | cleanExternalCache(context);
63 | // cleanDatabases(context);
64 | cleanSharedPreference(context);
65 | cleanFiles(context);
66 | if(filepath!=null) {
67 | for (String filePath : filepath) {
68 | cleanCustomCache(filePath);
69 | }
70 | }
71 | }
72 |
73 | /** * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 * * @param directory */
74 | private static void deleteFilesByDirectory(File directory) {
75 | if (directory != null && directory.exists() && directory.isDirectory()) {
76 | for (File item : directory.listFiles()) {
77 | item.delete();
78 | }
79 | }
80 | }
81 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_search_first.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
16 |
17 |
21 |
22 |
30 |
31 |
32 |
37 |
38 |
39 |
43 |
44 |
52 |
53 |
62 |
63 |
64 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
11 |
15 |
16 |
21 |
22 |
31 |
32 |
43 |
44 |
45 |
46 |
47 |
48 |
53 |
54 |
55 |
67 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/util/ReavalUtils.java:
--------------------------------------------------------------------------------
1 | package com.gank.util;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.annotation.TargetApi;
6 | import android.content.Context;
7 | import android.os.Build;
8 | import android.support.annotation.ColorRes;
9 | import android.support.v4.content.ContextCompat;
10 | import android.view.View;
11 | import android.view.ViewAnimationUtils;
12 | import android.view.animation.AccelerateDecelerateInterpolator;
13 |
14 | /**
15 | * 动画效果
16 | *
17 | * Created by wangchenlong on 16/2/26.
18 | */
19 | public class ReavalUtils {
20 | public interface OnRevealAnimationListener {
21 | void onRevealHide();
22 |
23 | void onRevealShow();
24 |
25 | void onRevealStart();
26 | }
27 |
28 | // 圆圈爆炸效果显示
29 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
30 | public static void animateRevealShow(
31 | final Context context, final View view,
32 | final int startRadius, @ColorRes final int color,
33 | final OnRevealAnimationListener listener) {
34 | int cx = (view.getLeft() + view.getRight()) / 2;
35 | int cy = (view.getTop() + view.getBottom()) / 2;
36 |
37 | float finalRadius = (float) Math.hypot(view.getWidth(), view.getHeight());
38 |
39 | // 设置圆形显示动画
40 | Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, startRadius, finalRadius);
41 | anim.setDuration(500);
42 | anim.setInterpolator(new AccelerateDecelerateInterpolator());
43 | anim.addListener(new AnimatorListenerAdapter() {
44 | @Override public void onAnimationEnd(Animator animation) {
45 | super.onAnimationEnd(animation);
46 | view.setVisibility(View.VISIBLE);
47 | listener.onRevealShow();
48 | }
49 |
50 | @Override public void onAnimationStart(Animator animation) {
51 | super.onAnimationStart(animation);
52 | view.setBackgroundColor(ContextCompat.getColor(context, color));
53 | listener.onRevealStart();
54 | }
55 |
56 | });
57 |
58 | anim.start();
59 | }
60 |
61 | // 圆圈凝聚效果
62 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
63 | public static void animateRevealHide(
64 | final Context context, final View view,
65 | final int finalRadius, @ColorRes final int color,
66 | final OnRevealAnimationListener listener
67 | ) {
68 | int cx = (view.getLeft() + view.getRight()) / 2;
69 | int cy = (view.getTop() + view.getBottom()) / 2;
70 | int initialRadius = view.getWidth();
71 | // 与入场动画的区别就是圆圈起始和终止的半径相反
72 | Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, initialRadius, finalRadius);
73 | anim.setDuration(500);
74 | anim.setInterpolator(new AccelerateDecelerateInterpolator());
75 | anim.addListener(new AnimatorListenerAdapter() {
76 | @Override public void onAnimationStart(Animator animation) {
77 | super.onAnimationStart(animation);
78 | // view.setBackgroundColor(ContextCompat.getColor(context, color));
79 | }
80 |
81 | @Override public void onAnimationEnd(Animator animation) {
82 | super.onAnimationEnd(animation);
83 | listener.onRevealHide();
84 | view.setVisibility(View.INVISIBLE);
85 | }
86 | });
87 | anim.start();
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/ui/adapter/base/ItemViewDelegateManager.java:
--------------------------------------------------------------------------------
1 | package com.gank.ui.adapter.base;
2 |
3 | import android.support.v4.util.SparseArrayCompat;
4 |
5 |
6 | public class ItemViewDelegateManager {
7 | SparseArrayCompat> delegates = new SparseArrayCompat();
8 |
9 | public int getItemViewDelegateCount() {
10 | return delegates.size();
11 | }
12 |
13 | public ItemViewDelegateManager addDelegate(ItemViewDelegate delegate) {
14 | int viewType = delegates.size();
15 | if (delegate != null) {
16 | delegates.put(viewType, delegate);
17 | viewType++;
18 | }
19 | return this;
20 | }
21 |
22 | public ItemViewDelegateManager addDelegate(int viewType, ItemViewDelegate delegate) {
23 | if (delegates.get(viewType) != null) {
24 | throw new IllegalArgumentException(
25 | "An ItemViewDelegate is already registered for the viewType = "
26 | + viewType
27 | + ". Already registered ItemViewDelegate is "
28 | + delegates.get(viewType));
29 | }
30 | delegates.put(viewType, delegate);
31 | return this;
32 | }
33 |
34 | public ItemViewDelegateManager removeDelegate(ItemViewDelegate delegate) {
35 | if (delegate == null) {
36 | throw new NullPointerException("ItemViewDelegate is null");
37 | }
38 | int indexToRemove = delegates.indexOfValue(delegate);
39 |
40 | if (indexToRemove >= 0) {
41 | delegates.removeAt(indexToRemove);
42 | }
43 | return this;
44 | }
45 |
46 | public ItemViewDelegateManager removeDelegate(int itemType) {
47 | int indexToRemove = delegates.indexOfKey(itemType);
48 |
49 | if (indexToRemove >= 0) {
50 | delegates.removeAt(indexToRemove);
51 | }
52 | return this;
53 | }
54 |
55 | public int getItemViewType(T item, int position) {
56 | int delegatesCount = delegates.size();
57 | for (int i = delegatesCount - 1; i >= 0; i--) {
58 | ItemViewDelegate delegate = delegates.valueAt(i);
59 | if (delegate.isForViewType(item, position)) {
60 | return delegates.keyAt(i);
61 | }
62 | }
63 | throw new IllegalArgumentException(
64 | "No ItemViewDelegate added that matches position=" + position + " in data source");
65 | }
66 |
67 | public void convert(ViewHolder holder, T item, int position) {
68 | int delegatesCount = delegates.size();
69 | for (int i = 0; i < delegatesCount; i++) {
70 | ItemViewDelegate delegate = delegates.valueAt(i);
71 |
72 | if (delegate.isForViewType(item, position)) {
73 | delegate.convert(holder, item, position);
74 | return;
75 | }
76 | }
77 | throw new IllegalArgumentException(
78 | "No ItemViewDelegateManager added that matches position=" + position + " in data source");
79 | }
80 |
81 |
82 | public ItemViewDelegate getItemViewDelegate(int viewType) {
83 | return delegates.get(viewType);
84 | }
85 |
86 | public int getItemViewLayoutId(int viewType) {
87 | return getItemViewDelegate(viewType).getItemViewLayoutId();
88 | }
89 |
90 | public int getItemViewType(ItemViewDelegate itemViewDelegate) {
91 | return delegates.indexOfValue(itemViewDelegate);
92 | }
93 |
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_search.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
21 |
22 |
27 |
28 |
37 |
38 |
43 |
44 |
51 |
52 |
62 |
63 |
64 |
74 |
75 |
76 |
77 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/data/AppDataManager.java:
--------------------------------------------------------------------------------
1 | package com.gank.data;
2 |
3 | import android.content.Context;
4 |
5 | import com.gank.Constants;
6 | import com.gank.data.database.AppDbHelper;
7 | import com.gank.data.database.DbHelper;
8 | import com.gank.data.database.entity.Image;
9 | import com.gank.data.network.ApiHelper;
10 | import com.gank.data.network.AppApiHelper;
11 | import com.gank.data.network.response.Result;
12 | import com.gank.data.network.response.ThemeResponse;
13 | import com.gank.data.preference.AppSharePreferences;
14 | import com.gank.data.preference.SharePreferenecesHelper;
15 |
16 | import java.util.List;
17 |
18 | import io.reactivex.Observable;
19 | import io.reactivex.disposables.CompositeDisposable;
20 |
21 | /**
22 | * Created by Administrator on 2017/3/29 0029.
23 | */
24 | public class AppDataManager implements DataManager {
25 |
26 | private ApiHelper apiHelper;
27 | private DbHelper dbHelper;
28 | private SharePreferenecesHelper sharePreferenecesHelper;
29 | private CompositeDisposable mCompositeDisposable;
30 |
31 | private static AppDataManager sAppDataManager;
32 |
33 | public static AppDataManager getInstance() {
34 | if (sAppDataManager == null) {
35 | sAppDataManager = new AppDataManager();
36 | }
37 | return sAppDataManager;
38 | }
39 |
40 | private AppDataManager() {
41 |
42 | }
43 |
44 | public void init(Context context) {
45 | dbHelper = new AppDbHelper(context, Constants.DB_NAME);
46 | apiHelper = new AppApiHelper(context);
47 | sharePreferenecesHelper = new AppSharePreferences(context, Constants.PREFERENCE_NAME);
48 | }
49 |
50 | @Override
51 | public Observable getThemeDataCall(String path) {
52 | return apiHelper.getThemeDataCall(path);
53 | }
54 |
55 | @Override
56 | public Observable getSearchDataCall(String content, String type, String page) {
57 | return apiHelper.getSearchDataCall(content, type, page);
58 | }
59 |
60 |
61 | @Override
62 | public Boolean getIsCollnection(String id) {
63 | return dbHelper.getIsCollnection(id);
64 | }
65 |
66 | @Override
67 | public void addConnection(Result result) {
68 | dbHelper.addConnection(result);
69 | }
70 |
71 | @Override
72 | public void addImage(Image img) {
73 | dbHelper.addImage(img);
74 | }
75 |
76 | @Override
77 | public void cancelCollection(String id) {
78 | dbHelper.cancelCollection(id);
79 | }
80 |
81 |
82 | @Override
83 | public Observable> queryForList(int offset) {
84 | return dbHelper.queryForList(offset);
85 | }
86 |
87 | @Override
88 | public void addSearchHistory(String content) {
89 | dbHelper.addSearchHistory(content);
90 | }
91 |
92 | @Override
93 | public Observable> querySearchHistory() {
94 | return dbHelper.querySearchHistory();
95 | }
96 |
97 | @Override
98 | public void deleteSearchHistory() {
99 | dbHelper.deleteSearchHistory();
100 | }
101 |
102 | @Override
103 | public void setOrder(String orderJsonStirng) {
104 | sharePreferenecesHelper.setOrder(orderJsonStirng);
105 | }
106 |
107 | @Override
108 | public String getOrderString() {
109 | return sharePreferenecesHelper.getOrderString();
110 | }
111 |
112 | @Override
113 | public void setTheme(boolean isNight) {
114 | sharePreferenecesHelper.setTheme(isNight);
115 | }
116 |
117 | @Override
118 | public boolean getTheme() {
119 | return sharePreferenecesHelper.getTheme();
120 | }
121 |
122 | public CompositeDisposable getCompositeDisposable() {
123 | if (mCompositeDisposable == null) {
124 | mCompositeDisposable = new CompositeDisposable();
125 | }
126 | return mCompositeDisposable;
127 | }
128 |
129 | }
130 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/util/TimeUtils.java:
--------------------------------------------------------------------------------
1 | package com.gank.util;
2 |
3 | import java.text.ParseException;
4 | import java.text.SimpleDateFormat;
5 | import java.util.Calendar;
6 | import java.util.Date;
7 |
8 | /**
9 | * Created by Administrator on 2017/4/20 0020.
10 | */
11 |
12 | public class TimeUtils {
13 | public static String convert(String sdate) {
14 | String day=sdate.split("T")[0];
15 | String time=sdate.split("T")[1].split("Z")[0];
16 | return day+" "+time;
17 | };
18 | private static ThreadLocal dateFormater2 = new ThreadLocal() {
19 | @Override
20 | protected SimpleDateFormat initialValue() {
21 | return new SimpleDateFormat("yyyy-MM-dd");
22 | }
23 | };
24 | public static String friendlyTimeFormat(String date) {
25 | String sdate=convert(date);
26 | Date time = toDate(sdate);
27 | if (time == null) {
28 | return "";
29 | }
30 | String ftime = "";
31 | Calendar cal = Calendar.getInstance();
32 |
33 | //判断是否是同一天
34 | String curDate = dateFormater2.get().format(cal.getTime());
35 | String paramDate = dateFormater2.get().format(time);
36 | if (curDate.equals(paramDate)) {
37 | int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
38 | if (hour == 0)
39 | ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前";
40 | else
41 | ftime = hour + "小时前";
42 | return ftime;
43 | }
44 |
45 |
46 | long lt = time.getTime() / 86400000;
47 | long ct = cal.getTimeInMillis() / 86400000;
48 | int days = (int) (ct - lt);
49 | if (days == 0) {
50 | int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
51 | if (hour == 0)
52 | ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前";
53 | else
54 | ftime = hour + "小时前";
55 | } else if (days == 1) {
56 | ftime = "昨天";
57 | } else if (days == 2) {
58 | ftime = "前天";
59 | }
60 |
61 | else if (days > 2 && days <= 30) {
62 | ftime = days + "天前";
63 | } else if (days > 30 && days <= 60) {
64 | ftime = "1个月前";
65 | } else if (days > 60 && days <= 90) {
66 | ftime = "2个月前";
67 | } else if (days > 90 && days <= 120) {
68 | ftime = "3个月前";
69 | } else if (days > 120 && days <= 150) {
70 | ftime = "4个月前";
71 | } else if (days > 150 && days <= 180) {
72 | ftime = "5个月前";
73 | } else if (days > 180 && days <= 210) {
74 | ftime = "6个月前";
75 | } else if (days > 210 && days <= 240) {
76 | ftime = "7个月前";
77 | } else if (days > 240 && days <= 270) {
78 | ftime = "8个月前";
79 | } else if (days > 270 && days <= 300) {
80 | ftime = "9个月前";
81 | } else if (days > 300 && days <= 330) {
82 | ftime = "10个月前";
83 | } else if (days > 330 && days <= 360) {
84 | ftime = "11个月前";
85 | } else if (days > 360 && days <= 720) {
86 | ftime = "一年前";
87 | } else if (days > 720 && days <= 1080) {
88 | ftime = "两年前";
89 | } else if (days > 1080) {
90 | ftime = dateFormater2.get().format(time);
91 | }
92 | return ftime;
93 | }
94 |
95 |
96 | /**
97 | * 将字符串转位日期类型
98 | *
99 | * @param sdate
100 | * @return
101 | */
102 | public static Date toDate(String sdate) {
103 | try {
104 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
105 | return sdf.parse(sdate);
106 | } catch (ParseException e) {
107 | return null;
108 | }
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/util/ItemDecoration.java:
--------------------------------------------------------------------------------
1 | package com.gank.util;
2 |
3 | /**
4 | * Created by Administrator on 2017/4/19 0019.
5 | */
6 |
7 | import android.content.Context;
8 | import android.content.res.TypedArray;
9 | import android.graphics.Canvas;
10 | import android.graphics.Rect;
11 | import android.graphics.drawable.Drawable;
12 | import android.support.v7.widget.LinearLayoutManager;
13 | import android.support.v7.widget.RecyclerView;
14 | import android.view.View;
15 |
16 | public class ItemDecoration extends RecyclerView.ItemDecoration {
17 |
18 | private Context mContext;
19 | private Drawable mDivider;
20 | private int mOrientation;
21 | public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
22 | public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
23 |
24 | //我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置
25 | public static final int[] ATRRS = new int[]{
26 | android.R.attr.listDivider
27 | };
28 |
29 | public ItemDecoration(Context context, int orientation) {
30 | this.mContext = context;
31 | final TypedArray ta = context.obtainStyledAttributes(ATRRS);
32 | this.mDivider = ta.getDrawable(0);
33 | ta.recycle();
34 | setOrientation(orientation);
35 | }
36 |
37 | //设置屏幕的方向
38 | public void setOrientation(int orientation) {
39 | if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
40 | throw new IllegalArgumentException("invalid orientation");
41 | }
42 | mOrientation = orientation;
43 | }
44 |
45 | @Override
46 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
47 | if (mOrientation == HORIZONTAL_LIST) {
48 | drawVerticalLine(c, parent, state);
49 | } else {
50 | drawHorizontalLine(c, parent, state);
51 | }
52 | }
53 |
54 | //画横线, 这里的parent其实是显示在屏幕显示的这部分
55 | public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
56 | int left = parent.getPaddingLeft();
57 | int right = parent.getWidth() - parent.getPaddingRight();
58 | final int childCount = parent.getChildCount();
59 | for (int i = 0; i < childCount; i++) {
60 | final View child = parent.getChildAt(i);
61 |
62 | //获得child的布局信息
63 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
64 | final int top = child.getBottom() + params.bottomMargin;
65 | final int bottom = top + mDivider.getIntrinsicHeight();
66 | mDivider.setBounds(left, top, right, bottom);
67 | mDivider.draw(c);
68 | //Log.d("wnw", left + " " + top + " "+right+" "+bottom+" "+i);
69 | }
70 | }
71 |
72 | //画竖线
73 | public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
74 | int top = parent.getPaddingTop();
75 | int bottom = parent.getHeight() - parent.getPaddingBottom();
76 | final int childCount = parent.getChildCount();
77 | for (int i = 0; i < childCount; i++) {
78 | final View child = parent.getChildAt(i);
79 |
80 | //获得child的布局信息
81 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
82 | final int left = child.getRight() + params.rightMargin;
83 | final int right = left + mDivider.getIntrinsicWidth();
84 | mDivider.setBounds(left, top, right, bottom);
85 | mDivider.draw(c);
86 | }
87 | }
88 |
89 | //由于Divider也有长宽高,每一个Item需要向下或者向右偏移
90 | @Override
91 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
92 | if (mOrientation == HORIZONTAL_LIST) {
93 | //画横线,就是往下偏移一个分割线的高度
94 | outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
95 | } else {
96 | //画竖线,就是往右偏移一个分割线的宽度
97 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
98 | }
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/ui/setting/SettingFragment.java:
--------------------------------------------------------------------------------
1 | package com.gank.ui.setting;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.design.widget.Snackbar;
7 | import android.util.TypedValue;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 | import android.widget.CompoundButton;
12 |
13 | import com.bumptech.glide.Glide;
14 | import com.gank.R;
15 | import com.gank.business.GankBus;
16 | import com.gank.databinding.FragmentSettingBinding;
17 | import com.gank.ui.base.LazyFragment;
18 | import com.gank.ui.base.RxBus;
19 | import com.gank.ui.collection.CollectionActivity;
20 | import com.gank.util.DataCleanManager;
21 |
22 | import static com.gank.Constants.isNight;
23 |
24 | /**
25 | * Created by Administrator on 2017/5/12 0012.
26 | */
27 |
28 | public class SettingFragment extends LazyFragment {
29 |
30 | private FragmentSettingBinding mBinding;
31 |
32 | @Nullable
33 | @Override
34 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
35 | View view = inflater.inflate(R.layout.fragment_setting, container, false);
36 | mBinding = FragmentSettingBinding.bind(view);
37 | return view;
38 |
39 | }
40 |
41 | @Override
42 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
43 | super.onViewCreated(view, savedInstanceState);
44 | mBinding.toolbar.setTitle("我的");
45 | mBinding.llCollection.setOnClickListener(new View.OnClickListener() {
46 | @Override
47 | public void onClick(View v) {
48 | startActivity(new Intent(getActivity(), CollectionActivity.class));
49 | }
50 | });
51 | //清除缓存
52 | mBinding.llClean.setOnClickListener(new View.OnClickListener() {
53 | @Override
54 | public void onClick(View v) {
55 | DataCleanManager.cleanApplicationData(getActivity(), new String[0]);
56 | Snackbar.make(mBinding.llClean, "清除缓存成功", Snackbar.LENGTH_SHORT).show();
57 | }
58 | });
59 | }
60 |
61 | @Override
62 | public void loadData() {
63 |
64 | if (isNight) {
65 | Glide.with(getActivity()).load(R.drawable.night1).crossFade().into(mBinding.ivBg);
66 | mBinding.switchCompat.setChecked(true);
67 | } else {
68 | Glide.with(getActivity()).load(R.drawable.day1).crossFade().into(mBinding.ivBg);
69 | mBinding.switchCompat.setChecked(false);
70 | }
71 | mBinding.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
72 | @Override
73 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
74 | if (isChecked) {
75 | isNight = true;
76 | getActivity().setTheme(R.style.NightTheme);
77 | Glide.with(getActivity()).load(R.drawable.night1).crossFade().into(mBinding.ivBg);
78 |
79 | } else {
80 | isNight = false;
81 | getActivity().setTheme(R.style.DayTheme);
82 | Glide.with(getActivity()).load(R.drawable.day1).crossFade().into(mBinding.ivBg);
83 | }
84 | GankBus.setting().setTheme(isNight);
85 | RxBus.getInstance().post(isNight);
86 |
87 | }
88 | });
89 |
90 | }
91 |
92 |
93 | @Override
94 | public void onDestroyView() {
95 | super.onDestroyView();
96 | }
97 |
98 | @Override
99 | protected void refreshUI() {
100 | refreshToolbar(mBinding.toolbar);
101 | TypedValue bottomline = new TypedValue();
102 | getActivity().getTheme().resolveAttribute(R.attr.bottomline, bottomline, true);
103 | mBinding.line1.setBackgroundResource(bottomline.resourceId);
104 | mBinding.line2.setBackgroundResource(bottomline.resourceId);
105 | mBinding.line3.setBackgroundResource(bottomline.resourceId);
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/versions.gradle:
--------------------------------------------------------------------------------
1 | ext.deps = [:]
2 | def versions = [:]
3 | versions.arcgis = "10.2.9"
4 |
5 | //versions.wcdb = "1.0.6"
6 | versions.leakcanary = "1.5.4"
7 | versions.arouter_api = "1.3.1"
8 | versions.arouter_compiler = "1.1.4"
9 |
10 | versions.room = "1.1.1"
11 | versions.lifecycle = "1.1.0"
12 |
13 | versions.constraint_layout = "1.0.2"
14 |
15 | versions.appcompat_v7 = "27.1.1"
16 | versions.support_v4 = "27.1.1"
17 | versions.recyclerview_v7 = "27.1.1"
18 | versions.cardview = "27.1.1"
19 | versions.design = "27.1.1"
20 |
21 | versions.android_gradle_plugin = "3.1.0"
22 | versions.multidex = "1.0.3"
23 | versions.rxjava = "2.0.1"
24 | versions.eventbus = "3.0.0"
25 |
26 | versions.imageLoaderVersion = "1.9.5"
27 | versions.photoviewVersion = "1.2.4"
28 | versions.recyclerview_swipe = "1.1.4"
29 | versions.tablayout = "2.1.2"
30 | versions.PhotoPicker = "0.9.5@aar"
31 | versions.Vlayout = "1.1.0@aar"
32 |
33 | versions.glide = "3.7.0"
34 | versions.fastjson = "1.1.68.android"
35 | versions.okgo = "3.0.4"
36 | versions.okhttp = "3.10.0"
37 |
38 | def deps = [:]
39 |
40 | def leakcanary = [:]
41 | leakcanary.debug = "com.squareup.leakcanary:leakcanary-android:$versions.leakcanary"
42 | leakcanary.release = "com.squareup.leakcanary:leakcanary-android-no-op:$versions.leakcanary"
43 | deps.leakcanary = leakcanary
44 |
45 |
46 | deps.arcgis = "com.esri.arcgis.android:arcgis-android:$versions.arcgis"
47 | deps.wcdb = "com.tencent.wcdb:wcdb-android:$versions.wcdb"
48 | deps.constraint_layout = "com.android.support.constraint:constraint-layout:$versions.constraint_layout"
49 | deps.appcompat_v7 = "com.android.support:appcompat-v7:$versions.appcompat_v7"
50 | deps.support_v4 = "com.android.support:support-v4:$versions.support_v4"
51 | deps.recyclerview_v7 = "com.android.support:recyclerview-v7:$versions.recyclerview_v7"
52 | deps.cardview = "com.android.support:cardview-v7:$versions.cardview"
53 | deps.design = "com.android.support:design:$versions.design"
54 | deps.android_gradle_plugin = "com.android.tools.build:gradle:$versions.android_gradle_plugin"
55 | deps.multidex = "com.android.support:multidex:$versions.multidex"
56 | deps.rxjava = "io.reactivex.rxjava2:rxandroid:$versions.rxjava"
57 | deps.eventbus = "org.greenrobot:eventbus:$versions.eventbus"
58 | deps.imageLoaderVersion = "com.nostra13.universalimageloader:universal-image-loader:$versions.imageLoaderVersion"
59 | deps.photoviewVersion = "com.github.chrisbanes.photoview:library:$versions.photoviewVersion"
60 | deps.recyclerview_swipe = "com.yanzhenjie:recyclerview-swipe:$versions.recyclerview_swipe"
61 | deps.tablayout = "com.flyco.tablayout:FlycoTabLayout_Lib:$versions.tablayout"
62 | deps.fastjson = "com.alibaba:fastjson:$versions.fastjson"
63 | deps.PhotoPicker = "me.iwf.photopicker:PhotoPicker:$versions.PhotoPicker"
64 | deps.Vlayout = "com.alibaba.android:vlayout:$versions.Vlayout" /*{
65 | transitive = true
66 | }*/
67 | deps.glide = "com.github.bumptech.glide:glide:$versions.glide"
68 | deps.okgo = "com.lzy.net:okgo:$versions.okgo"
69 | deps.okhttp = "com.squareup.okhttp3:okhttp:$versions.okhttp"
70 |
71 | def arouter = [:]
72 | arouter.compiler = "com.alibaba:arouter-compiler:$versions.arouter_compiler"
73 | arouter.api = "com.alibaba:arouter-api:$versions.arouter_api"
74 | deps.arouter = arouter
75 |
76 | def room = [:]
77 | room.runtime = "android.arch.persistence.room:runtime:$versions.room"
78 | room.compiler = "android.arch.persistence.room:compiler:$versions.room"
79 | room.rxjava = "android.arch.persistence.room:rxjava2:$versions.room"
80 | deps.room = room
81 |
82 | def lifecycle = [:]
83 | lifecycle.runtime = "android.arch.lifecycle:runtime:$versions.lifecycle"
84 | lifecycle.extensions = "android.arch.lifecycle:extensions:$versions.lifecycle"
85 | lifecycle.livedata = "android.arch.lifecycle:livedata:$versions.lifecycle"
86 | lifecycle.java8 = "android.arch.lifecycle:common-java8:$versions.lifecycle"
87 | lifecycle.compiler = "android.arch.lifecycle:compiler:$versions.lifecycle"
88 | deps.lifecycle = lifecycle
89 |
90 |
91 | deps.ffmpeg = '3.2.1-1.3'
92 |
93 | ext.deps = deps
94 |
95 |
96 |
97 | def build_versions = [:]
98 | build_versions.min_sdk = 21
99 | build_versions.target_sdk = 27
100 | build_versions.build_tools = "27.0.3"
101 | ext.build_versions = build_versions
102 |
103 |
104 | ext.addRepos = this.&addRepos
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/ui/adapter/CommonAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gank.ui.adapter;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.ImageView;
8 | import android.widget.TextView;
9 |
10 | import com.bumptech.glide.Glide;
11 | import com.gank.R;
12 | import com.gank.data.network.response.Result;
13 | import com.gank.util.TimeUtils;
14 |
15 | import java.util.List;
16 |
17 | /**
18 | * Created by Administrator on 2017/4/3 0003.
19 | */
20 |
21 | public class CommonAdapter extends RecyclerView.Adapter {
22 | private boolean fromCollection;
23 | private boolean fromSearch;
24 | private List list;
25 | private OnItemClickListener OnItemClickListener;
26 |
27 | public CommonAdapter() {
28 | }
29 |
30 | public void setData(List list) {
31 | this.list = list;
32 | }
33 |
34 | public void setData(List list, boolean fromCollection) {
35 | this.fromCollection = fromCollection;
36 | this.list = list;
37 | }
38 |
39 | public void setSearchData(List list, boolean fromSearch) {
40 | this.fromSearch = fromSearch;
41 | this.list = list;
42 | }
43 |
44 | public void notifyData(int startPosition, int count) {
45 | notifyItemRangeChanged(startPosition, count);
46 | }
47 |
48 | @Override
49 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
50 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
51 | ViewHolder viewHolder = new ViewHolder(view);
52 | return viewHolder;
53 | }
54 |
55 | @Override
56 | public void onBindViewHolder(ViewHolder holder, final int position) {
57 | Result result = list.get(position);
58 | holder.image.setVisibility(View.GONE);
59 | if (fromSearch) {
60 | holder.image.setVisibility(View.GONE);
61 | } else if (fromCollection) {
62 | if (result.getImg() != null && result.getImg().size() > 0) {
63 | holder.image.setVisibility(View.VISIBLE);
64 | Glide.with(holder.itemView.getContext()).load(result.getImg().get(0).getImageUrl())
65 | .asBitmap().centerCrop().into(holder.image);
66 | }
67 | } else {
68 | if (result.getImages() != null && result.getImages().size() > 0) {
69 | holder.image.setVisibility(View.VISIBLE);
70 | Glide.with(holder.itemView.getContext()).load(result.getImages().get(0))
71 | .asBitmap().centerCrop().into(holder.image);
72 | }
73 | }
74 |
75 | holder.text.setText(result.getDesc());
76 | if (fromSearch) {
77 | holder.author.setText(result.getType() + " · " + result.getWho() + " · " + TimeUtils.friendlyTimeFormat(result.getPublishedAt()));
78 | } else {
79 | holder.author.setText(result.getWho() + " · " + TimeUtils.friendlyTimeFormat(result.getPublishedAt()));
80 | }
81 | holder.itemView.setOnClickListener(new View.OnClickListener() {
82 | @Override
83 | public void onClick(View v) {
84 | onItemClickListener.OnItemClick(v, position);
85 | }
86 | });
87 |
88 | }
89 |
90 |
91 | @Override
92 | public int getItemCount() {
93 | return list == null ? 0 : list.size();
94 | }
95 |
96 |
97 | class ViewHolder extends RecyclerView.ViewHolder {
98 | TextView text;
99 | ImageView image;
100 | TextView author;
101 |
102 | public ViewHolder(View itemView) {
103 | super(itemView);
104 | text = (TextView) itemView.findViewById(R.id.text);
105 | image = (ImageView) itemView.findViewById(R.id.image);
106 | author = (TextView) itemView.findViewById(R.id.author);
107 | }
108 | }
109 |
110 | private OnItemClickListener onItemClickListener;
111 |
112 | public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
113 | this.onItemClickListener = onItemClickListener;
114 | }
115 |
116 |
117 | public interface OnItemClickListener {
118 | void OnItemClick(View v, int position);
119 | }
120 |
121 |
122 | }
123 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/ui/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.gank.ui;
2 |
3 |
4 | import android.content.res.Resources;
5 | import android.databinding.DataBindingUtil;
6 | import android.os.Bundle;
7 | import android.support.annotation.ColorInt;
8 | import android.support.annotation.ColorRes;
9 | import android.support.v4.app.Fragment;
10 | import android.support.v4.app.FragmentPagerAdapter;
11 | import android.support.v4.content.ContextCompat;
12 | import android.support.v4.view.PagerAdapter;
13 | import android.util.TypedValue;
14 | import android.view.KeyEvent;
15 | import android.widget.Toast;
16 |
17 | import com.gank.R;
18 | import com.gank.business.GankBus;
19 | import com.gank.business.GankBusiness;
20 | import com.gank.business.MeiziBusiness;
21 | import com.gank.business.SearchBusiness;
22 | import com.gank.business.SettingBusiness;
23 | import com.gank.data.AppDataManager;
24 | import com.gank.databinding.ActivityMainBinding;
25 | import com.gank.ui.base.BaseActivity;
26 | import com.gank.ui.base.BaseFragment;
27 | import com.gank.view.OnlyIconItemView;
28 |
29 | import java.util.ArrayList;
30 | import java.util.List;
31 |
32 | import me.majiajie.pagerbottomtabstrip.NavigationController;
33 | import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;
34 |
35 | public class MainActivity extends BaseActivity {
36 |
37 | private ActivityMainBinding mBinding;
38 |
39 | private List fragmentList = new ArrayList<>();
40 |
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
46 | setSupportActionBar(mBinding.toolbar);
47 | getSupportActionBar().setDisplayShowTitleEnabled(true);
48 |
49 |
50 | NavigationController navigationController = mBinding.tab.custom()
51 | .addItem(newItem(R.drawable.home, R.drawable.home_selected))
52 | .addItem(newItem(R.drawable.fuli, R.drawable.fuli_selected))
53 | .addItem(newItem(R.drawable.mine, R.drawable.mine_selected))
54 | .build();
55 | fragmentList.add(BaseFragment.newInstance(0));
56 | fragmentList.add(BaseFragment.newInstance(1));
57 | fragmentList.add(BaseFragment.newInstance(2));
58 | mBinding.viewpager.setOffscreenPageLimit(3);
59 | PagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
60 | @Override
61 | public Fragment getItem(int position) {
62 | return fragmentList.get(position);
63 | }
64 |
65 | @Override
66 | public int getCount() {
67 | return fragmentList.size();
68 | }
69 | };
70 | mBinding.viewpager.setAdapter(pagerAdapter);
71 |
72 | navigationController.setupWithViewPager(mBinding.viewpager);
73 |
74 | }
75 |
76 | @Override
77 | protected void refreshUI() {
78 | TypedValue bottomColor = new TypedValue();
79 | Resources.Theme theme = getTheme();
80 | theme.resolveAttribute(R.attr.bottomcolor, bottomColor, true);
81 | Resources resources = getResources();
82 | if (mBinding.tab != null) {
83 | mBinding.tab.setBackgroundColor(resources.getColor(bottomColor.resourceId));
84 | }
85 |
86 | }
87 |
88 |
89 | //创建一个Item
90 | private BaseTabItem newItem(int drawable, int checkedDrawable) {
91 | OnlyIconItemView onlyIconItemView = new OnlyIconItemView(this);
92 | onlyIconItemView.initialize(drawable, checkedDrawable);
93 | return onlyIconItemView;
94 | }
95 |
96 | @ColorInt
97 | private int color(@ColorRes int res) {
98 | return ContextCompat.getColor(this, res);
99 | }
100 |
101 | private long exitTime = 0;
102 |
103 | @Override
104 | public boolean onKeyDown(int keyCode, KeyEvent event) {
105 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
106 | if ((System.currentTimeMillis() - exitTime) > 2000) {
107 | Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
108 | exitTime = System.currentTimeMillis();
109 | } else {
110 | finish();
111 | // System.exit(0);
112 | }
113 | return true;
114 | }
115 | return super.onKeyDown(keyCode, event);
116 | }
117 |
118 | }
119 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #4e372f
4 | #4e372f
5 | #4e372f
6 |
7 | #F2F3F3
8 | #e75946
9 | #f4d227
10 | #4674e7
11 | #0ba62c
12 | #E6E6E6
13 | #DADCDC
14 | #FFAF8F84
15 | #FFCCB9B3
16 | #00000000
17 | #dcdbdb
18 | #e1e4e6
19 | #222222
20 | #252525
21 |
22 | #404040
23 | #262626
24 | #ACACAC
25 | #262626
26 | #8A8A8A
27 | #8A8A8A
28 |
29 | #FFFFFF
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 | #FF6347
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 | #D2B48C
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | #B8860B
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 | #8B0000
120 |
121 |
122 |
123 | #808080
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 | #000000
175 |
176 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_index.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
21 |
22 |
23 |
24 |
30 |
31 |
32 |
33 |
34 |
39 |
40 |
43 |
44 |
45 |
46 |
52 |
53 |
62 |
63 |
76 |
77 |
82 |
88 |
98 |
99 |
100 |
101 |
102 |
107 |
108 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
13 |
14 |
20 |
21 |
42 |
43 |
61 |
62 |
63 |
67 |
68 |
75 |
76 |
79 |
80 |
83 |
84 |
87 |
88 |
92 |
93 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/ui/adapter/base/MultiItemTypeAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gank.ui.adapter.base;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 |
8 | import java.util.List;
9 |
10 |
11 | public class MultiItemTypeAdapter extends RecyclerView.Adapter {
12 | protected Context mContext;
13 | protected List mDatas;
14 |
15 | protected ItemViewDelegateManager mItemViewDelegateManager;
16 | protected OnItemClickListener mOnItemClickListener;
17 |
18 |
19 | public MultiItemTypeAdapter(Context context, List datas) {
20 | mContext = context;
21 | mDatas = datas;
22 | mItemViewDelegateManager = new ItemViewDelegateManager();
23 | }
24 |
25 | @Override
26 | public int getItemViewType(int position) {
27 | if (!useItemViewDelegateManager()) return super.getItemViewType(position);
28 | return mItemViewDelegateManager.getItemViewType(mDatas.get(position), position);
29 | }
30 |
31 |
32 | @Override
33 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
34 | ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(viewType);
35 | int layoutId = itemViewDelegate.getItemViewLayoutId();
36 | ViewHolder holder = ViewHolder.createViewHolder(mContext, parent, layoutId);
37 | onViewHolderCreated(holder, holder.getConvertView());
38 | setListener(parent, holder, viewType);
39 | return holder;
40 | }
41 |
42 | public void onViewHolderCreated(ViewHolder holder, View itemView) {
43 |
44 | }
45 |
46 | public void convert(ViewHolder holder, T t) {
47 | mItemViewDelegateManager.convert(holder, t, holder.getAdapterPosition());
48 | }
49 |
50 | protected boolean isEnabled(int viewType) {
51 | return true;
52 | }
53 |
54 |
55 | protected void setListener(final ViewGroup parent, final ViewHolder viewHolder, int viewType) {
56 | if (!isEnabled(viewType)) return;
57 | viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
58 | @Override
59 | public void onClick(View v) {
60 | if (mOnItemClickListener != null) {
61 | int position = viewHolder.getAdapterPosition();
62 | mOnItemClickListener.onItemClick(v, viewHolder, position);
63 | }
64 | }
65 | });
66 |
67 | viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
68 | @Override
69 | public boolean onLongClick(View v) {
70 | if (mOnItemClickListener != null) {
71 | int position = viewHolder.getAdapterPosition();
72 | return mOnItemClickListener.onItemLongClick(v, viewHolder, position);
73 | }
74 | return false;
75 | }
76 | });
77 | }
78 |
79 | @Override
80 | public void onBindViewHolder(ViewHolder holder, int position) {
81 | convert(holder, mDatas.get(position));
82 | }
83 |
84 | @Override
85 | public int getItemCount() {
86 | int itemCount = mDatas.size();
87 | return itemCount;
88 | }
89 |
90 |
91 | public List getDatas() {
92 | return mDatas;
93 | }
94 |
95 | public MultiItemTypeAdapter addItemViewDelegate(ItemViewDelegate itemViewDelegate) {
96 | mItemViewDelegateManager.addDelegate(itemViewDelegate);
97 | return this;
98 | }
99 |
100 | public MultiItemTypeAdapter addItemViewDelegate(int viewType, ItemViewDelegate itemViewDelegate) {
101 | mItemViewDelegateManager.addDelegate(viewType, itemViewDelegate);
102 | return this;
103 | }
104 |
105 | protected boolean useItemViewDelegateManager() {
106 | return mItemViewDelegateManager.getItemViewDelegateCount() > 0;
107 | }
108 |
109 | public interface OnItemClickListener {
110 | void onItemClick(View view, RecyclerView.ViewHolder holder, int position);
111 |
112 | boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position);
113 | }
114 |
115 | public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
116 | this.mOnItemClickListener = onItemClickListener;
117 | }
118 |
119 |
120 | public void clearData() {
121 | int itemCount = mDatas.size();
122 | mDatas.clear();
123 | this.notifyItemRangeRemoved(0, itemCount);
124 | }
125 |
126 |
127 | public void addData(int position, List datas) {
128 | if (datas != null && datas.size() > 0) {
129 | this.mDatas.addAll(datas);
130 | this.notifyItemRangeChanged(position, datas.size());
131 | }
132 | }
133 |
134 | public void addData(List datas) {
135 | if (datas != null && datas.size() > 0) {
136 | int startPosition = mDatas.size();
137 | this.mDatas.addAll(datas);
138 | this.notifyItemRangeChanged(startPosition, datas.size());
139 | }
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_setting.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
11 |
12 |
17 |
18 |
21 |
22 |
27 |
28 |
29 |
30 |
37 |
38 |
43 |
44 |
52 |
53 |
54 |
60 |
61 |
68 |
69 |
74 |
75 |
83 |
84 |
85 |
91 |
92 |
98 |
99 |
104 |
105 |
113 |
114 |
120 |
121 |
122 |
128 |
129 |
130 |
131 |
132 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/ui/search/SearchEmptyFragment.java:
--------------------------------------------------------------------------------
1 | package com.gank.ui.search;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.TextView;
10 |
11 | import com.gank.R;
12 | import com.gank.business.GankBus;
13 | import com.gank.business.constant.GankResultCode;
14 | import com.gank.databinding.FragmentSearchFirstBinding;
15 | import com.gank.ui.base.BaseEnum;
16 | import com.gank.ui.base.BaseFragment;
17 | import com.gank.util.DensityUtil;
18 | import com.google.android.flexbox.FlexWrap;
19 | import com.google.android.flexbox.FlexboxLayout;
20 | import com.kunminx.architecture.business.bus.IResponse;
21 | import com.kunminx.architecture.business.bus.Result;
22 |
23 | import java.util.List;
24 |
25 | /**
26 | * Created by Administrator on 2017/4/27 0027.
27 | */
28 |
29 | public class SearchEmptyFragment extends BaseFragment implements IResponse {
30 |
31 |
32 | OnSelectListener listener;
33 |
34 | public static SearchEmptyFragment newInstance() {
35 | Bundle args = new Bundle();
36 | SearchEmptyFragment fragment = new SearchEmptyFragment();
37 | fragment.setArguments(args);
38 | return fragment;
39 | }
40 |
41 | @Override
42 | protected void refreshUI() {
43 |
44 | }
45 |
46 | @Override
47 | public void onAttach(Context context) {
48 | super.onAttach(context);
49 | if (context instanceof SearchActivity) {
50 | listener = (OnSelectListener) context;
51 | }
52 | }
53 |
54 | private FragmentSearchFirstBinding mBinding;
55 |
56 | @Nullable
57 | @Override
58 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
59 | View view = inflater.inflate(R.layout.fragment_search_first, container, false);
60 | mBinding = FragmentSearchFirstBinding.bind(view);
61 | GankBus.registerResponseObserver(this);
62 | return view;
63 | }
64 |
65 | @Override
66 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
67 | super.onViewCreated(view, savedInstanceState);
68 | initFlexbox();
69 | }
70 |
71 | private void initFlexbox() {
72 | mBinding.fblType.setFlexWrap(FlexWrap.WRAP);
73 | for (BaseEnum nameEnum : BaseEnum.values()) {
74 | final TextView tv_bq = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tv_bq, mBinding.fblType, false);
75 | mBinding.fblType.addView(tv_bq);
76 | FlexboxLayout.LayoutParams layoutParams = (FlexboxLayout.LayoutParams) tv_bq.getLayoutParams();
77 | layoutParams.setMargins(DensityUtil.dip2px(getActivity(), 10),
78 | DensityUtil.dip2px(getActivity(), 5), 0, DensityUtil.dip2px(getActivity(), 5));
79 | tv_bq.setText(nameEnum.getValue());
80 | mBinding.fblType.getChildAt(0).setSelected(true);
81 | tv_bq.setOnClickListener(new View.OnClickListener() {
82 | @Override
83 | public void onClick(View v) {
84 | for (int i = 0; i < mBinding.fblType.getChildCount(); i++) {
85 | mBinding.fblType.getChildAt(i).setSelected(false);
86 | }
87 | tv_bq.setSelected(true);
88 | listener.onSelected(tv_bq.getText().toString());
89 | }
90 | });
91 | }
92 | mBinding.clear.setOnClickListener(new View.OnClickListener() {
93 | @Override
94 | public void onClick(View v) {
95 | GankBus.search().deleteAll();
96 | GankBus.search().querySearchHistory();
97 | }
98 | });
99 | }
100 |
101 | @Override
102 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
103 | super.onActivityCreated(savedInstanceState);
104 | GankBus.search().querySearchHistory();
105 | }
106 |
107 | @Override
108 | public void onDestroy() {
109 | super.onDestroy();
110 | GankBus.unregisterResponseObserver(this);
111 | }
112 |
113 | @Override
114 | public void onResult(Result testResult) {
115 | String resultCode = (String) testResult.getResultCode();
116 | switch (resultCode) {
117 | case GankResultCode.HISTORY_LIST_QUERYED:
118 | List results = (List) testResult.getResultObject();
119 | mBinding.fblHistory.setFlexWrap(FlexWrap.WRAP);
120 | mBinding.fblHistory.removeAllViews();
121 | for (String content : results) {
122 | final TextView tv_his = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tv_bq, mBinding.fblHistory, false);
123 | mBinding.fblHistory.addView(tv_his);
124 | tv_his.setText(content);
125 | FlexboxLayout.LayoutParams layoutParams = (FlexboxLayout.LayoutParams) tv_his.getLayoutParams();
126 | layoutParams.setMargins(DensityUtil.dip2px(getActivity(), 10),
127 | DensityUtil.dip2px(getActivity(), 5), 0, DensityUtil.dip2px(getActivity(), 5));
128 | tv_his.setOnClickListener(new View.OnClickListener() {
129 | @Override
130 | public void onClick(View v) {
131 | listener.onSearch(tv_his.getText().toString());
132 | }
133 | });
134 | }
135 | break;
136 | case GankResultCode.FAILURE:
137 |
138 | break;
139 | default:
140 | }
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/ui/collection/CollectionFragment.java:
--------------------------------------------------------------------------------
1 | package com.gank.ui.collection;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.os.Handler;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 |
12 | import com.gank.R;
13 | import com.gank.business.GankBus;
14 | import com.gank.business.constant.GankResultCode;
15 | import com.gank.data.database.entity.Image;
16 | import com.gank.data.network.response.Result;
17 | import com.gank.databinding.FragmentCollectionBinding;
18 | import com.gank.ui.adapter.CommonAdapter;
19 | import com.gank.ui.base.BaseFragment;
20 | import com.gank.ui.detail.DetailActivity;
21 | import com.gank.util.ItemDecoration;
22 | import com.gank.util.LogUtils;
23 | import com.gank.util.RecyclerViewUtil;
24 | import com.kunminx.architecture.business.bus.IResponse;
25 | import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
26 | import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
27 |
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | /**
32 | * Created by Administrator on 2017/4/20 0020.
33 | */
34 |
35 | public class CollectionFragment extends BaseFragment implements IResponse {
36 |
37 | private int page = 1;
38 | private List list = new ArrayList<>();
39 | private CommonAdapter mCommonAdapter = new CommonAdapter();
40 |
41 | private FragmentCollectionBinding mBinding;
42 |
43 | @Nullable
44 | @Override
45 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
46 | View view = inflater.inflate(R.layout.fragment_collection, container, false);
47 | mBinding = FragmentCollectionBinding.bind(view);
48 | GankBus.registerResponseObserver(this);
49 |
50 | return view;
51 | }
52 |
53 | @Override
54 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
55 | super.onViewCreated(view, savedInstanceState);
56 | mActivity.initToolbar(mBinding.toolbar);
57 | mBinding.toolbar.setTitle(R.string.main3);
58 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
59 | mBinding.recyclerView.addItemDecoration(new ItemDecoration(getActivity(), ItemDecoration.VERTICAL_LIST));
60 | linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
61 | mBinding.recyclerView.setLayoutManager(linearLayoutManager);
62 | mBinding.recyclerView.setAdapter(mCommonAdapter);
63 | RecyclerViewUtil.setHeader(getActivity(), mBinding.refreshLayout);
64 | mBinding.refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
65 | @Override
66 | public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
67 | new Handler().postDelayed(new Runnable() {
68 | @Override
69 | public void run() {
70 | page = 1;
71 | list.clear();
72 | GankBus.gank().queryCollectionData(page);
73 | }
74 | }, 0);
75 | }
76 |
77 | @Override
78 | public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
79 | LogUtils.v("onLoadmore");
80 | new Handler().postDelayed(new Runnable() {
81 | @Override
82 | public void run() {
83 | GankBus.gank().queryCollectionData(page);
84 | }
85 | }, 500);
86 | }
87 |
88 | @Override
89 | public void onFinishLoadMore() {
90 | page++;
91 | LogUtils.v("onFinishLoadMore");
92 | super.onFinishLoadMore();
93 | }
94 |
95 | });
96 |
97 | mCommonAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
98 | @Override
99 | public void OnItemClick(View v, int position) {
100 | Intent intent = new Intent(getActivity(), DetailActivity.class);
101 | Result resultbean = list.get(position);
102 | if (resultbean.getImg() != null && resultbean.getImg().size() > 0) {
103 | List imgList = new ArrayList();
104 | for (Image image : resultbean.getImg()) {
105 | imgList.add(image.getImageUrl());
106 | resultbean.setImages(imgList);
107 | }
108 | }
109 | intent.putExtra("bean", resultbean);
110 | startActivity(intent);
111 | }
112 | });
113 |
114 | }
115 |
116 | @Override
117 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
118 | super.onActivityCreated(savedInstanceState);
119 | mBinding.refreshLayout.startRefresh();
120 | }
121 |
122 | @Override
123 | protected void refreshUI() {
124 | refreshToolbar(mBinding.toolbar);
125 | }
126 |
127 | @Override
128 | public void onDestroy() {
129 | super.onDestroy();
130 | GankBus.unregisterResponseObserver(this);
131 |
132 | }
133 |
134 | @Override
135 | public void onResult(com.kunminx.architecture.business.bus.Result testResult) {
136 | String resultCode = (String) testResult.getResultCode();
137 | switch (resultCode) {
138 | case GankResultCode.INDEX_LIST_QUERYED:
139 | List collections = (List) testResult.getResultObject();
140 | if (collections != null) {
141 | list.addAll(collections);
142 | RecyclerViewUtil.loadMoreSetting(mBinding.refreshLayout, list);
143 | mCommonAdapter.setData(list, true);
144 | mCommonAdapter.notifyDataSetChanged();
145 | }
146 | break;
147 | case GankResultCode.FAILURE:
148 |
149 | break;
150 | default:
151 | }
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gank/ui/search/SearchResultFragment.java:
--------------------------------------------------------------------------------
1 | package com.gank.ui.search;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.os.Handler;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 |
12 | import com.gank.Constants;
13 | import com.gank.R;
14 | import com.gank.business.GankBus;
15 | import com.gank.business.constant.GankResultCode;
16 | import com.gank.data.network.response.Result;
17 | import com.gank.databinding.FragmentSearchTwoBinding;
18 | import com.gank.ui.adapter.CommonAdapter;
19 | import com.gank.ui.base.BaseFragment;
20 | import com.gank.ui.detail.DetailActivity;
21 | import com.gank.util.ItemDecoration;
22 | import com.gank.util.RecyclerViewUtil;
23 | import com.gank.util.WrapContentLinearLayoutManager;
24 | import com.kunminx.architecture.business.bus.IResponse;
25 | import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
26 | import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
27 |
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | /**
32 | * Created by Administrator on 2017/4/27 0027.
33 | */
34 |
35 | public class SearchResultFragment extends BaseFragment implements IResponse {
36 |
37 | private CommonAdapter commonAdapter = new CommonAdapter();
38 | private int page;
39 | private boolean isRefresh = true;
40 | private String content;
41 | private String type;
42 | private List list = new ArrayList<>();
43 |
44 | public static SearchResultFragment newInstance(Bundle bundle) {
45 | SearchResultFragment fragment = new SearchResultFragment();
46 | fragment.setArguments(bundle);
47 | return fragment;
48 | }
49 |
50 | private FragmentSearchTwoBinding mBinding;
51 |
52 | @Nullable
53 | @Override
54 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
55 | View view = inflater.inflate(R.layout.fragment_search_two, container, false);
56 | mBinding = FragmentSearchTwoBinding.bind(view);
57 | GankBus.registerResponseObserver(this);
58 |
59 | return view;
60 | }
61 |
62 | @Override
63 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
64 | super.onViewCreated(view, savedInstanceState);
65 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
66 | linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
67 | mBinding.recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
68 | mBinding.recyclerView.addItemDecoration(new ItemDecoration(getActivity(), ItemDecoration.VERTICAL_LIST));
69 | RecyclerViewUtil.setHeader(getActivity(), mBinding.refreshLayout);
70 | mBinding.refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
71 | @Override
72 | public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
73 | new Handler().postDelayed(new Runnable() {
74 | @Override
75 | public void run() {
76 | page = 1;
77 | isRefresh = true;
78 | GankBus.search().search(content, type, page + "");
79 | }
80 | }, 0);
81 | }
82 |
83 | @Override
84 | public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
85 | new Handler().postDelayed(new Runnable() {
86 | @Override
87 | public void run() {
88 | GankBus.search().search(content, type, page + "");
89 | }
90 | }, 500);
91 | }
92 |
93 | @Override
94 | public void onFinishLoadMore() {
95 | if (Constants.ERROR) {
96 | Constants.ERROR = false;
97 | } else {
98 | page++;
99 | }
100 | super.onFinishLoadMore();
101 | }
102 |
103 | });
104 |
105 | commonAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
106 | @Override
107 | public void OnItemClick(View v, int position) {
108 | Intent intent = new Intent(getActivity(), DetailActivity.class);
109 | Result resultbean = list.get(position);
110 | intent.putExtra("bean", resultbean);
111 | startActivity(intent);
112 | }
113 | });
114 | }
115 |
116 | @Override
117 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
118 | super.onActivityCreated(savedInstanceState);
119 | mBinding.recyclerView.setAdapter(commonAdapter);
120 | loadData(getArguments().getString("content"), getArguments().getString("type"));
121 | }
122 |
123 | public void loadData(String content, String type) {
124 | this.content = content;
125 | this.type = type;
126 | mBinding.refreshLayout.startRefresh();
127 | }
128 |
129 | @Override
130 | protected void refreshUI() {
131 |
132 | }
133 |
134 | @Override
135 | public void onDestroy() {
136 | super.onDestroy();
137 | GankBus.unregisterResponseObserver(this);
138 | }
139 |
140 | @Override
141 | public void onResult(com.kunminx.architecture.business.bus.Result testResult) {
142 | String resultCode = (String) testResult.getResultCode();
143 | switch (resultCode) {
144 | case GankResultCode.SEARCH_LIST_QUERYED:
145 | List results = (List) testResult.getResultObject();
146 | if (isRefresh) {
147 | list.clear();
148 | isRefresh = false;
149 | }
150 | int start = list.size();
151 | list.addAll(results);
152 | RecyclerViewUtil.loadMoreSetting(mBinding.refreshLayout, list);
153 | commonAdapter.setSearchData(list, true);
154 | commonAdapter.notifyData(start, results.size());
155 | break;
156 | case GankResultCode.FAILURE:
157 | Constants.ERROR = true;
158 | mBinding.refreshLayout.onFinishLoadMore();
159 | break;
160 | default:
161 | }
162 | }
163 | }
164 |
--------------------------------------------------------------------------------