patterns = new HashMap<>();
21 |
22 | public DefaultPageSearcher(Context context, IFilterName filterName) {
23 | super(context);
24 | this.filterName = filterName;
25 | }
26 |
27 | @Override
28 | public boolean filter(String keyword, I entity) {
29 | String name = filterName.getFilterName(entity);
30 | return filterByName(keyword, entity, name);
31 | }
32 |
33 | protected boolean filterByName(String keyword, I entity, String name) {
34 | if (TextUtils.isEmpty(name)) {
35 | return false;
36 | }
37 | boolean isFind = false;
38 | Pattern pattern = patterns.get(keyword);
39 | if (pattern == null) {
40 | pattern = SearchHelper.buildPattern(keyword);
41 | patterns.put(keyword, pattern);
42 | }
43 | Matcher matcher = pattern.matcher(name);
44 | while (matcher.find()) {
45 | isFind = true;
46 | addHighlightKeyword(name.substring(matcher.start(), matcher.end()));
47 | }
48 | if (!isFind) {
49 | String firstName = name.substring(0, 1);
50 | String pinyin = PinyinUtils.getPinYin(name);
51 | if (Character.toString(firstName.charAt(0)).matches("[\\u4E00-\\u9FA5]+")) {
52 | keyword = keyword.toLowerCase(Locale.getDefault());
53 | pinyin = pinyin.toLowerCase(Locale.getDefault());
54 | if (pinyin.startsWith(keyword)) {
55 | isFind = true;
56 | addHighlightKeyword(firstName);
57 | }
58 | } else {
59 | if (name.startsWith(keyword)) {
60 | isFind = true;
61 | addHighlightKeyword(keyword);
62 | }
63 | }
64 | }
65 | return isFind;
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IFilter.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | /**
4 | * Created by Yanghuiqiang on 2017/2/9.
5 | */
6 |
7 | public interface IFilter {
8 |
9 | boolean filter(String keyword, I entity);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IFilterName.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | /**
4 | * Created by Yanghuiqiang on 2017/2/10.
5 | */
6 |
7 | public interface IFilterName {
8 | String getFilterName(I entity);
9 | }
10 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IPageAdapter.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * Created by Yanghuiqiang on 2016/10/11.
9 | *
10 | * 主要负责将解析后的数据适配到Adapter上并显示出来,
11 | *
12 | * 如ListAdapter、ExpandableListAdapter以及RecyclerAdapter,
13 | *
14 | * 只要实现了此接口,都是可以进行数据适配的。
15 | */
16 | public interface IPageAdapter {
17 |
18 | int getPageDataCount();
19 |
20 | void clear();
21 |
22 | void appendBefore(List data);
23 |
24 | void appendAfter(List data);
25 |
26 | List getPageListData();
27 |
28 | void notifyDataSetChanged();
29 |
30 | // 数据附加模式
31 | DataAppendMode getDataAppendMode();
32 |
33 | void setHighlightKeywords(List keywords);
34 |
35 | void setCheckedListData(List data);
36 |
37 | void setDisabledListData(List data);
38 |
39 | void onStateSaved(Bundle state);
40 |
41 | void onStateRestored(Bundle state);
42 | }
43 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IPageChecker.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * 可以checked的接口
7 | *
8 | * @author Yanghuiqiang
9 | *
10 | * 2015-10-30
11 | */
12 | public interface IPageChecker extends PageCheckerInterface, IStateSaved {
13 |
14 | void setPageData(List pageData);
15 |
16 | void appendPageData(List pageData);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IPageDataIntercept.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * 主要负责对解析后以及适配到列表之前的数据的拦截,有的时候我们可能要对这部分数据进行特殊处理,那我们就可以使用分页列表数据拦截器。
7 | *
8 | * Created by Yanghuiqiang on 2016/10/11.
9 | */
10 |
11 | public interface IPageDataIntercept {
12 | List intercept(Chain chain) throws Exception;
13 |
14 | interface Chain {
15 | List data();
16 |
17 | List handle(List data) throws Exception;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IPageDataParser.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Created by Yanghuiqiang on 2016/10/11.
7 | *
8 | * 主要负责解析从服务器请求到的数据,获取要适配到AbsListView或者RecyclerView这些分页列表上面的数据集以及列表数据的总个数。
9 | */
10 |
11 | public interface IPageDataParser {
12 |
13 | /**
14 | * 从请求数据中获取数据的列表
15 | *
16 | * @param data
17 | * @param isFromCache
18 | * @return
19 | */
20 | List getPageList(T data, boolean isFromCache);
21 |
22 | /**
23 | * 从请求中获取总数据的总数
24 | *
25 | * @param data
26 | * @param isFromCache
27 | * @return
28 | */
29 | long getPageTotal(T data, boolean isFromCache);
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IPageRequester.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | /**
4 | * 主要负责客户端向服务端的数据请求以及数据回调,框架自带了Retrofit+okhttp的请求器
5 | *
6 | * Created by Yanghuiqiang on 2016/10/11.
7 | */
8 | public interface IPageRequester {
9 |
10 | void onRequest(PageAction pageAction, Page page, IPageResponse pageResponse);
11 |
12 | void onCancel();
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IPageResponse.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import android.content.Context;
4 |
5 | /**
6 | * Created by Yanghuiqiang on 2016/10/11.
7 | */
8 |
9 | public interface IPageResponse {
10 | void onResponse(PageAction pageAction, T response, boolean isFromCache);
11 |
12 | void onException(Context context, PageAction pageAction, Throwable throwable);
13 | }
14 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IPageSearcher.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Created by Yanghuiqiang on 2017/2/9.
7 | */
8 |
9 | public interface IPageSearcher extends IStateSaved {
10 | void onSearch(PageAction pageAction, String keyword, PageManager.IPageDataCallback callback);
11 |
12 | void onCancel();
13 |
14 | void setPageData(List mPageData);
15 |
16 | List getHighlightKeywords();
17 | }
18 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/IStateSaved.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import android.os.Bundle;
4 |
5 | /**
6 | * Created by Yanghuiqiang on 2017/2/17.
7 | */
8 |
9 | interface IStateSaved {
10 | boolean saveState(Bundle state, OnPageDataStateSaved listener);
11 |
12 | boolean restoreState(Bundle state, OnPageDataStateSaved listener);
13 | }
14 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/OnPageCheckedChangeListener.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Created by Administrator on 2017/2/12.
7 | */
8 |
9 | public interface OnPageCheckedChangeListener {
10 | void onPageCheckedChanged(List checkedList, int count);
11 | }
12 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/OnPageCheckedEquals.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | /**
4 | * Created by Administrator on 2017/2/12.
5 | */
6 |
7 | public interface OnPageCheckedEquals {
8 | /**
9 | * 两个实体之间是否相等
10 | *
11 | * @param t1
12 | * @param t2
13 | * @return
14 | */
15 | boolean equals(T t1, T t2);
16 | }
17 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/OnPageCheckedInitListener.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | /**
4 | * Created by Yanghuiqiang on 2017/2/13.
5 | */
6 |
7 | public interface OnPageCheckedInitListener {
8 | boolean isEnable(int position, I entity);
9 |
10 | boolean isChecked(int position, I entity);
11 | }
12 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/OnPageDataStateSaved.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * Created by Yanghuiqiang on 2017/2/20.
9 | */
10 |
11 | public interface OnPageDataStateSaved {
12 | void onStateSaved(Bundle state, String key, List entity);
13 |
14 | List onStateRestored(Bundle state, String key);
15 | }
16 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/OnPageListener.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | /**
4 | * 涵盖了整个请求过程的生命周期。最常用的可能就是onPageLoadComplete方法了,这个方法在数据适配到adapter后回调,也就是说,你可以在这个方法里面拿到最终适配并显示出来的数据。
5 | *
6 | * Created by 杨慧强 on 2016/2/17.
7 | */
8 | public interface OnPageListener {
9 |
10 | // 取消请求
11 | void onPageCancelRequests();
12 |
13 | // 开始请求
14 | void onPageRequestStart(PageAction pageAction);
15 |
16 | // 请求载入完成,如果没有缓存,则不会回调此方法
17 | void onPageLoadComplete(PageAction pageAction, boolean isFromCache, boolean isSuccess);
18 |
19 | // 载入缓存,如果有缓存,会回调onPageLoadComplete
20 | void onPageLoadCache(PageAction pageAction, boolean isHaveCache);
21 |
22 | // 下拉刷新
23 | void onPageRefresh();
24 |
25 | // 上拉加载
26 | void onPageLoadMore();
27 |
28 | void onPageSearch(String keyword);
29 |
30 | // 初始化
31 | void onPageInit();
32 |
33 | // exception,会回调onPageLoadComplete方法
34 | void onPageException(Throwable e);
35 | }
36 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/OnPageListenerDispatcher.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * Created by 杨慧强 on 2016/2/17.
8 | */
9 | public class OnPageListenerDispatcher implements OnPageListener {
10 | private List mOnPageListeners;
11 |
12 | public OnPageListenerDispatcher(List onPageListeners) {
13 | this.mOnPageListeners = onPageListeners;
14 | }
15 |
16 | public OnPageListenerDispatcher() {
17 | this(new ArrayList());
18 | }
19 |
20 | public void addOnPageListener(OnPageListener listener) {
21 | this.mOnPageListeners.add(listener);
22 | }
23 |
24 | @Override
25 | public void onPageCancelRequests() {
26 | for (OnPageListener l : mOnPageListeners) {
27 | l.onPageCancelRequests();
28 | }
29 | }
30 |
31 | @Override
32 | public void onPageRequestStart(PageAction pageAction) {
33 | for (OnPageListener l : mOnPageListeners) {
34 | l.onPageRequestStart(pageAction);
35 | }
36 | }
37 |
38 | @Override
39 | public void onPageLoadComplete(PageAction pageAction, boolean isFromCache, boolean isSuccess) {
40 | for (OnPageListener l : mOnPageListeners) {
41 | l.onPageLoadComplete(pageAction, isFromCache, isSuccess);
42 | }
43 | }
44 |
45 | @Override
46 | public void onPageLoadCache(PageAction pageAction, boolean isHaveCache) {
47 | for (OnPageListener l : mOnPageListeners) {
48 | l.onPageLoadCache(pageAction, isHaveCache);
49 | }
50 | }
51 |
52 | @Override
53 | public void onPageRefresh() {
54 | for (OnPageListener l : mOnPageListeners) {
55 | l.onPageRefresh();
56 | }
57 | }
58 |
59 | @Override
60 | public void onPageLoadMore() {
61 | for (OnPageListener l : mOnPageListeners) {
62 | l.onPageLoadMore();
63 | }
64 | }
65 |
66 | @Override
67 | public void onPageSearch(String keyword) {
68 | for (OnPageListener l : mOnPageListeners) {
69 | l.onPageSearch(keyword);
70 | }
71 | }
72 |
73 | @Override
74 | public void onPageInit() {
75 | for (OnPageListener l : mOnPageListeners) {
76 | l.onPageInit();
77 | }
78 | }
79 |
80 | @Override
81 | public void onPageException(Throwable e) {
82 | for (OnPageListener l : mOnPageListeners) {
83 | l.onPageException(e);
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/OnPullToRefreshProvider.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | /**
4 | * 主要负责提供上拉加载下拉刷新组件,我们这里提供了XListView、XExpandableListView以及google的SwipeRefreshLayout组件,如果你使用的是其他的上拉加载下拉刷新组件,你需要实现OnPullToRefreshProvider接口。
5 | *
6 | * @author Yanghuiqiang 2014-9-4
7 | */
8 | public interface OnPullToRefreshProvider {
9 |
10 | /**
11 | * 刷新接口
12 | *
13 | * @author Yanghuiqiang 2014-10-9
14 | */
15 | interface OnRefreshListener {
16 | void onPullToRefresh();
17 |
18 | void onPullToLoadMore();
19 | }
20 |
21 | /**
22 | * 设置是否有更多数�?
23 | *
24 | * @param isHaveMoreData
25 | */
26 | void setHaveMoreData(boolean isHaveMoreData);
27 |
28 | /**
29 | * 下拉刷新是否有效
30 | *
31 | * @param enable
32 | */
33 | void setPullRefreshEnable(boolean enable);
34 |
35 | boolean isPullRefreshEnable();
36 |
37 | /**
38 | * 上拉加载是否有效
39 | *
40 | * @param enable
41 | */
42 | void setPullLoadMoreEnable(boolean enable);
43 |
44 | boolean isPullLoadMoreEnable();
45 |
46 | /**
47 | * 刷新完成
48 | *
49 | * @param newDataSize
50 | * @param success
51 | */
52 | void onRefreshComplete(int newDataSize, boolean success);
53 |
54 | /**
55 | * 设置刷新监听
56 | *
57 | * @param onRefreshListener
58 | */
59 | void setOnRefreshListener(OnRefreshListener onRefreshListener);
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/Page.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 分页信息
7 | *
8 | * @param
9 | * @author Yanghuiqiang 2015-5-24
10 | */
11 | public final class Page implements Serializable {
12 | // 数据大小
13 | long dataSize;
14 | // 一页的大小
15 | int pageSize = 20;
16 | // 页数
17 | int pageCount;
18 | // 当前页号
19 | int currentPage = 1;
20 | // 附带数据
21 | T mData;
22 |
23 | void init() {
24 | int count = (int) (this.dataSize / this.pageSize);
25 | if (this.dataSize % this.pageSize == 0) {
26 | pageCount = count;
27 | } else {
28 | pageCount = count + 1;
29 | }
30 | mData = null;
31 | }
32 |
33 | void reset() {
34 | currentPage = 1;
35 | }
36 |
37 | void next() {
38 | currentPage++;
39 | if (currentPage >= pageCount) {
40 | currentPage = pageCount;
41 | }
42 | }
43 |
44 | void previous() {
45 | currentPage--;
46 | if (currentPage <= 0) {
47 | currentPage = 0;
48 | }
49 | }
50 |
51 | /**
52 | * 是否有下一页
53 | *
54 | * @return
55 | */
56 | public boolean haveNextPage() {
57 | if (currentPage >= pageCount) {
58 | return false;
59 | }
60 | return true;
61 | }
62 |
63 | public long getDataSize() {
64 | return dataSize;
65 | }
66 |
67 | public int getPageSize() {
68 | return pageSize;
69 | }
70 |
71 | public int getPageCount() {
72 | return pageCount;
73 | }
74 |
75 | public int getCurrentPage() {
76 | return currentPage;
77 | }
78 |
79 | public T getData() {
80 | return mData;
81 | }
82 |
83 | public Page clone() {
84 | Page page = new Page();
85 | page.currentPage = currentPage;
86 | page.dataSize = dataSize;
87 | page.mData = mData;
88 | page.pageCount = pageCount;
89 | page.pageSize = pageSize;
90 | return page;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/PageAction.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | /**
4 | * Created by Yanghuiqiang on 2016/10/11.
5 | */
6 |
7 | public enum PageAction {
8 | INIT, REFRESH, LOADMORE, SEARCH, RESTORE
9 | }
10 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/PageCheckerInterface.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Created by Yanghuiqiang on 2017/2/17.
7 | */
8 |
9 | public interface PageCheckerInterface {
10 | /**
11 | * 清除所有的选中状态
12 | */
13 | void clearAllChecked();
14 |
15 | /**
16 | * 切换某个位置的选中和不选中的状态
17 | *
18 | * @param position
19 | */
20 | void toggleChecked(int position);
21 |
22 | /**
23 | * 这个位置是否选中
24 | *
25 | * @param position
26 | * @return
27 | */
28 | boolean isChecked(int position);
29 |
30 | /**
31 | * 这个位置是否不可选
32 | *
33 | * @param position
34 | * @return
35 | */
36 | boolean isDisable(int position);
37 |
38 | /**
39 | * 设置某一个位置选择或者不选中
40 | *
41 | * @param position
42 | * @param isChecked
43 | */
44 | void setChecked(int position, boolean isChecked);
45 |
46 | /**
47 | * 是否全选
48 | *
49 | * @return
50 | */
51 | boolean isAllChecked();
52 |
53 | /**
54 | * 全选按钮是否可以选择
55 | *
56 | * @return
57 | */
58 | boolean isAllEnable();
59 |
60 | /**
61 | * 全选或者全不选
62 | *
63 | * @param isChecked
64 | */
65 | void setAllChecked(boolean isChecked);
66 |
67 | /**
68 | * 获取checked的list
69 | *
70 | * @return
71 | */
72 | List getCheckedEntityList(boolean appendDisableEntity);
73 |
74 | List getDisabledEntityList();
75 |
76 | /**
77 | * 和初始化checked的list相比,新添加了哪些
78 | *
79 | * @return
80 | */
81 | List getAddedEntityList();
82 |
83 | /**
84 | * 和初始化checked的list相比,移除了哪些
85 | *
86 | * @return
87 | */
88 | List getRemovedEntityList();
89 |
90 | int getCheckEntityCount(boolean appendDisableEntity);
91 | }
92 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/PageRequester.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import android.content.Context;
4 |
5 | /**
6 | * Created by Yanghuiqiang on 2016/10/11.
7 | */
8 |
9 | public abstract class PageRequester implements IPageRequester {
10 | private Context context;
11 | private IPageResponse pageResponse;
12 | private PageAction pageAction;
13 |
14 | public PageRequester(Context context) {
15 | this.context = context;
16 | }
17 |
18 | @Override
19 | public final void onRequest(PageAction pageAction, Page page, IPageResponse pageResponse) {
20 | this.pageResponse = pageResponse;
21 | this.pageAction = pageAction;
22 | executeRequest(context, pageAction, page);
23 | }
24 |
25 | public abstract void executeRequest(Context context, PageAction pageAction, Page page);
26 |
27 | protected final void callResponse(T response) {
28 | this.callCacheResponse(null);
29 | this.callNetworkResponse(response);
30 | }
31 |
32 | protected final void callNetworkResponse(T response) {
33 | this.pageResponse.onResponse(pageAction, response, false);
34 | }
35 |
36 | protected final void callCacheResponse(T response) {
37 | this.pageResponse.onResponse(pageAction, response, true);
38 | }
39 |
40 | protected final void callException(Throwable throwable) {
41 | this.pageResponse.onException(context, pageAction, throwable);
42 | }
43 |
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/PageSearcher.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.text.TextUtils;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | /**
11 | * Created by Yanghuiqiang on 2017/2/9.
12 | */
13 |
14 | public abstract class PageSearcher implements IPageSearcher, IFilter {
15 | protected Context mContext;
16 | private PageManager.IPageDataCallback mCallback;
17 | private PageAction mPageAction;
18 | private List mPageData;
19 | private List mHighlightKeywords = new ArrayList<>();
20 |
21 | public PageSearcher(Context context) {
22 | this.mContext = context;
23 | }
24 |
25 | protected final void addHighlightKeyword(String keyword) {
26 | if (!mHighlightKeywords.contains(keyword)) {
27 | mHighlightKeywords.add(keyword);
28 | }
29 | }
30 |
31 | @Override
32 | public final List getHighlightKeywords() {
33 | return mHighlightKeywords;
34 | }
35 |
36 | protected void executeSearch(List pageData, String keyword) {
37 | List list;
38 | if (TextUtils.isEmpty(keyword)) {
39 | list = handleNullKeyword(pageData);
40 | } else {
41 | list = handleNotNullKeyword(pageData, keyword);
42 | }
43 | this.callSearchResponse(list);
44 | }
45 |
46 | protected final void callSearchResponse(List response) {
47 | this.mCallback.onPageDataCallback(mPageAction, response, false);
48 | }
49 |
50 | protected final void callException(Throwable throwable) {
51 | this.mCallback.onException(mContext, mPageAction, throwable);
52 | }
53 |
54 | public final void setPageData(List pageData) {
55 | this.mPageData = new ArrayList<>(pageData);
56 | }
57 |
58 | @Override
59 | public final void onSearch(PageAction pageAction, String keyword, PageManager.IPageDataCallback callback) {
60 | this.mHighlightKeywords.clear();
61 | this.mCallback = callback;
62 | this.mPageAction = pageAction;
63 | if (TextUtils.isEmpty(keyword)) {
64 | this.mCallback.onPageDataCallback(PageAction.REFRESH, mPageData, false);
65 | } else {
66 | executeSearch(mPageData, keyword);
67 | }
68 | }
69 |
70 | @Override
71 | public void onCancel() {
72 |
73 | }
74 |
75 | protected List handleNotNullKeyword(List pageData, String keyword) {
76 | List list = new ArrayList<>();
77 | for (int i = 0; i < pageData.size(); i++) {
78 | if (this.filter(keyword, pageData.get(i))) {
79 | list.add(pageData.get(i));
80 | }
81 | }
82 | return list;
83 | }
84 |
85 | protected List handleNullKeyword(List pageData) {
86 | return new ArrayList<>(pageData);
87 | }
88 |
89 | @Override
90 | public boolean saveState(Bundle state, OnPageDataStateSaved listener) {
91 | listener.onStateSaved(state, "PageSearcher.mPageData", mPageData);
92 | return true;
93 | }
94 |
95 | @Override
96 | public boolean restoreState(Bundle state, OnPageDataStateSaved listener) {
97 | mPageData = listener.onStateRestored(state, "PageSearcher.mPageData");
98 | return true;
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/core/SearchHelper.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.core;
2 |
3 | import android.graphics.Color;
4 | import android.text.Spannable;
5 | import android.text.SpannableString;
6 | import android.text.TextUtils;
7 | import android.text.style.ForegroundColorSpan;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 | import java.util.regex.Matcher;
12 | import java.util.regex.Pattern;
13 |
14 | /**
15 | * Created by Administrator on 2017/2/9.
16 | */
17 |
18 | public class SearchHelper {
19 |
20 | public final static List EscapeKeywords = new ArrayList<>();
21 |
22 | static {
23 | EscapeKeywords.add('.');
24 | EscapeKeywords.add('$');
25 | EscapeKeywords.add('(');
26 | EscapeKeywords.add(')');
27 | EscapeKeywords.add('*');
28 | EscapeKeywords.add('+');
29 | EscapeKeywords.add('[');
30 | EscapeKeywords.add('?');
31 | EscapeKeywords.add('\\');
32 | EscapeKeywords.add('^');
33 | EscapeKeywords.add('{');
34 | EscapeKeywords.add('|');
35 | }
36 |
37 | public static Pattern buildPattern(String keyword) {
38 | List keywords = new ArrayList<>();
39 | keywords.add(keyword);
40 | return buildPattern(keywords);
41 | }
42 |
43 | public static Pattern buildPattern(List keywords) {
44 | String pattern = "";
45 | for (String keyword : keywords) {
46 | StringBuffer buffer = new StringBuffer();
47 | for (char c : keyword.toCharArray()) {
48 | if (EscapeKeywords.contains(c)) {
49 | buffer.append("\\" + c);
50 | } else {
51 | buffer.append(c);
52 | }
53 | }
54 | pattern += "((" + buffer.toString() + ")+)|";
55 | }
56 | return Pattern.compile(pattern.substring(0, pattern.length() - 1));
57 | }
58 |
59 | public static CharSequence match(CharSequence text, List keywords) {
60 | if (keywords == null || keywords.isEmpty()) {
61 | return text;
62 | }
63 | Matcher matcher = buildPattern(keywords).matcher(text);
64 | SpannableString spannable = new SpannableString(text);// 用于可变字符串
65 | while (matcher.find()) {
66 | highlight(spannable, matcher.start(), matcher.end());
67 | }
68 | return spannable;
69 | }
70 |
71 | public static CharSequence match(CharSequence text, String keyword) {
72 | if (TextUtils.isEmpty(keyword)) {
73 | return text;
74 | }
75 | List keywords = new ArrayList<>();
76 | keywords.add(keyword);
77 | return match(text, keywords);
78 | }
79 |
80 | private static SpannableString highlight(SpannableString text, int start, int end) {
81 | ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
82 | text.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
83 | return text;
84 | }
85 |
86 | }
--------------------------------------------------------------------------------
/library/src/main/java/cn/yhq/page/http/RetrofitPageActivity.java:
--------------------------------------------------------------------------------
1 | package cn.yhq.page.http;
2 |
3 | import cn.yhq.page.core.IPageRequester;
4 | import cn.yhq.page.ui.PageActivity;
5 |
6 | public abstract class RetrofitPageActivity