MVCHelper?????????????????????????????
MVCHelper +?IDataSource?ITask?+ IDataAdapter + ?????? + ?????ILoadViewFactory?ILoadView?ILoadMoreView?
TaskHelper????????????????MVC???????????????ICallback??UI
TaskHelper+?ITask?IDataSource?+ ICallBack
Download sample Apk
?????????
https://github.com/LuckyJayce/MVCHelper/releases
??????
Gradle??
1.?????
//MVCHelper???? compile 'com.shizhefei:MVCHelper-Library:1.3.2' //?????recyclerview???????recyclerview compile 'com.android.support:recyclerview-v7:24.0.0'
2.???
<1> ?? https://github.com/LuckyJayce/CoolRefreshView ??????? ????View??? ??????Header???NestedScrollingParent,NestedScrollingChild????????ViewPager???????
//??????MVCCoolHelper ???????? MVCHelper compile 'com.shizhefei:MVCHelper-CoolRefresh:1.3.2' compile 'com.shizhefei:CoolRefreshView:1.0.1' compile 'com.android.support:support-v4:24.0.0'
<2> ?? https://github.com/chrisbanes/Android-PullToRefresh ???????
//??????MVCPullrefshHelper ???????? MVCHelper compile 'com.shizhefei:MVCHelper-Pullrefresh:1.3.2' //??????gradle???????????jcenter? compile 'com.shizhefei:pulltorefresh:1.0.1'
<3> ?? https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh ???????
//??????MVCUltraHelper ???????? MVCHelper compile 'com.shizhefei:MVCHelper-UltraRefresh:1.3.2' //??6?29???????????????????????? compile 'in.srain.cube:ultra-ptr:1.0.11'
<4> ??android v4?SwipeRefreshLayout?????????
//??????MVCSwipeRefreshHelper ???????? MVCHelper compile 'com.shizhefei:MVCHelper-SwipeRefresh:1.3.2' //v4?????????v7?????v4? compile 'com.android.support:support-v4:24.0.0'
<5> ????????????MVCHelper?Task????????????????????????????????
//MVCHelper????????ABSTestCaseFragment??List<TestCaseData> getTestCaseDatas()?? compile 'com.shizhefei:MVCHelper-TestCase:1.3.2' //?????gson compile 'com.google.code.gson:gson:2.2.4'
<6> MVCHelper-OkHttp ?OKHttp?????
//MVCHelper? OKHttp????? compile 'com.shizhefei:MVCHelper-OkHttp:1.3.2' //?????okhttp3 compile 'com.squareup.okhttp3:okhttp:3.4.0' compile 'com.squareup.okio:okio:1.9.0'
<7> MVCHelper-Tasks ??Task???????RxJava????????Task ??????token????????????task
IAsyncTask<User> task = Tasks
.create(new InitTokenTask())
.concatWith(new GetUserTask()));
????
//MVCHelper? Task???? compile 'com.shizhefei:MVCHelper-Tasks:1.3.2'
??
###???
https://raw.githubusercontent.com/LuckyJayce/MVCHelper/master/raw/MVCHelper%E7%B1%BB%E5%9B%BE.png
?? MVCHelper
MVCHelper. ???????????????????????????????????????????,???MVC??.
1.Model (IDataSource)????????
??????IDataSource??????okhttp,volley,rxjava+retrofit???IAsyncDataSource
<1>???????????
//???
public interface IDataSource<DATA> {
// ??????? public DATA refresh() throws Exception;
// ????????? public DATA loadMore() throws Exception;
// ??????????? public boolean hasMore();
}
?????????????
public class BooksDataSource implements IDataSource<List<Book>> {
private int page = 1; private int maxPage = 5;
@Override public List<Book> refresh() throws Exception {
return loadBooks(1);
}
@Override public List<Book> loadMore() throws Exception {
return loadBooks(page + 1);
}
private List<Book> loadBooks(int page) {
List<Book> books = new ArrayList<Book>();
for (int i = 0; i < 20; i++) {
books.add(new Book("page" + page + " Java???? " + i, 108.00d));
}
this.page = page;
return books;
}
@Override public boolean hasMore() {
return page < maxPage;
}
}
<2>???????????????????
/** * ????????Volley?OkHttp???????? * @param <DATA> */ public interface IAsyncDataSource<DATA> {
/**
* ???????
*
* @param sender ????????????MVCHelper?MVCHelper???IDataAdapter??notifyDataChenge??
* @return ?????????????.????refresh?????????refresh????????RequestHandle???????.MVCHelper?destroy?????????
* @throws Exception
*/
RequestHandle refresh(ResponseSender<DATA> sender) throws Exception;
/**
* ?????????
*
* @param sender ????????????MVCHelper?MVCHelper???IDataAdapter??notifyDataChenge??
* @return ?????????????.????refresh?????????refresh????????RequestHandle???????.MVCHelper?destroy?????????
* @throws Exception
*/
RequestHandle loadMore(ResponseSender<DATA> sender) throws Exception;
/**
* ???????????
*
* @return
*/
boolean hasMore();
}
??:OkHttp??
public class BooksOkHttpNormal_DataSource implements IAsyncDataSource<List<Book>> {
private int mPage;
private int mMaxPage = 5;
public BooksOkHttpNormal_DataSource() {
super();
}
@Override
public RequestHandle refresh(ResponseSender<List<Book>> sender) throws Exception {
return loadBooks(sender, 1);
}
@Override
public RequestHandle loadMore(ResponseSender<List<Book>> sender) throws Exception {
return loadBooks(sender, mPage + 1);
}
@Override
public boolean hasMore() {
return mPage < mMaxPage;
}
private RequestHandle loadBooks(final ResponseSender<List<Book>> sender, final int page) throws Exception {
Request request = new Request.Builder().url("https://www.baidu.com").get().build();
Call call = OkHttpUtils.client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//send ???????(????)
sender.sendError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final List<Book> books = new ArrayList<Book>();
for (int i = 0; i < 15; i++) {
books.add(new Book("page" + page + " Java???? " + i, 108.00d));
}
mPage = page;
//send ???????(????)
sender.sendData(books);
}
}
);
return new OKHttpRequestHandle(call);
}
}
public class OKHttpRequestHandle implements RequestHandle {
private final Call call;
public OKHttpRequestHandle(Call call) {
super();
this.call = call;
}
@Override
public void cancle() {
call.cancel();
}
@Override
public boolean isRunning() {
return false;
}
}
??:?? MVCHelper-OkHttp
public class BooksOkHttp_AsyncDataSource implements IAsyncDataSource<List<Book>> {
private int mPage;
private int mMaxPage = 5;
@Override
public RequestHandle refresh(ResponseSender<List<Book>> sender) throws Exception {
return loadBooks(sender, 1);
}
@Override
public RequestHandle loadMore(ResponseSender<List<Book>> sender) throws Exception {
return loadBooks(sender, mPage + 1);
}
@Override
public boolean hasMore() {
return mPage < mMaxPage;
}
private RequestHandle loadBooks(final ResponseSender<List<Book>> sender, final int page) throws Exception {
GetMethod method = new GetMethod("https://www.baidu.com");
method.addHeader("a", "aaaaa");
method.addParam("api_key", "75ee6c644cad38dc8e53d3598c8e6b6c");
//method ???????sender.sendData ? sendError????????ResponseParser??Response??????
method.executeAsync(sender, new ResponseParser<List<Book>>() {
@Override
public List<Book> parse(Response response) throws Exception {
List<Book> books = new ArrayList<Book>();
for (int i = 0; i < 15; i++) {
books.add(new Book("page" + page + " Java???? " + i, 108.00d));
}
mPage = page;
return books;
}
}
);
return method;
}
}
**?????? https://github.com/LuckyJayce/MVCHelper/tree/master/app/src/main/java/com/shizhefei/test/models/datasource
???
??? volley
??? okhttp
??? rxjava+retrofit
2.View?IDataAdapter? ???????
?????Android?view???????????????
public interface IDataAdapter<DATA> {
public abstract void notifyDataChanged(DATA data, boolean isRefresh);
public abstract DATA getData();
public boolean isEmpty();
}
?????????????
public class BooksAdapter extends BaseAdapter implements IDataAdapter<List<Book>> {
private List<Book> books = new ArrayList<Book>();
private LayoutInflater inflater;
public BooksAdapter(Context context) {
super();
inflater = LayoutInflater.from(context);
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_book, parent, false);
}
TextView textView = (TextView) convertView;
textView.setText(books.get(position).getName());
return convertView;
}
@Override public void notifyDataChanged(List<Book> data, boolean isRefresh) {
if (isRefresh) {
books.clear();
}
books.addAll(data);
notifyDataSetChanged();
}
@Override public int getCount() {
return books.size();
}
@Override public List<Book> getData() {
return books;
}
@Override public Object getItem(int position) {
return null;
}
@Override public long getItemId(int position) {
return 0;
}
}
3.Controller (Activity,Fragment,MVCHelper)???
?????????????????????????
Activity?????????
public class MainActivity extends Activity {
private MVCHelper<List<Book>> mvcHelper;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ??LoadView?factory???????????????????????????
// MVCHelper.setLoadViewFractory(new LoadViewFractory());
PullToRefreshListView refreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);
mvcHelper = new MVCPullrefshHelper<List<Book>>(refreshListView);
// ?????
mvcHelper.setDataSource(new BooksDataSource());
// ?????
mvcHelper.setAdapter(new BooksAdapter(this));
// ????
mvcHelper.refresh();
}
@Override protected void onDestroy() {
super.onDestroy();
// ????
mvcHelper.destory();
}
}
????????????????????????????????????????????????????????????????????,????????????????????????
4.ILoadViewFractory ??? ????????????????
??ILoadViewFractory
MVCHelper.setLoadViewFractory(new LoadViewFractory());
???????????????
5.????????????
1.?CoolRefreshView ??????? MVCCoolHelper?
??? https://github.com/LuckyJayce/CoolRefreshView
CoolRefreshView coolRefreshView = (CoolRefreshView) findViewById(R.id.coolRefreshView);
MVCHelper<List<Book>> mvcHelper = new MVCCoolHelper<List<Book>>(coolRefreshView);
// ????? mvcHelper.setDataSource(new BooksDataSource());
// ????? mvcHelper.setAdapter(new BooksAdapter(this));
// ???? mvcHelper.refresh();
2.?android-support-v4.jar ?SwipeRefreshLayout??????? MVCSwipeRefreshHelper?
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
MVCHelper<List<Book>> mvcHelper = new MVCSwipeRefreshHelper<List<Book>>(swipeRefreshLayout);
// ????? mvcHelper.setDataSource(new BooksDataSource());
// ????? mvcHelper.setAdapter(new BooksAdapter(this));
// ???? mvcHelper.refresh();
3.?Android-PullToRefresh-Library??????**?MVCPullrefshHelper?**
??? https://github.com/chrisbanes/Android-PullToRefresh
PullToRefreshListView refreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);
MVCHelper<List<Book>> mvcHelper = new MVCPullrefshHelper<List<Book>>(refreshListView);
// ????? mvcHelper.setDataSource(new BooksDataSource());
// ????? mvcHelper.setAdapter(new BooksAdapter(this));
// ???? mvcHelper.refresh();
4.?android-Ultra-Pull-To-Refresh-library??????**?MVCUltraHelper?**
??? https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh
PtrClassicFrameLayout mPtrFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.rotate_header_list_view_frame);
MVCHelper<List<Book>> mvcHelper = new MVCUltraHelper<List<Book>>(mPtrFrameLayout);
// ????? mvcHelper.setDataSource(new BooksDataSource());
// ????? mvcHelper.setAdapter(new BooksAdapter(this));
// ???? mvcHelper.refresh();
5.???????? MVCNormalHelper?
View contentLayout = findViewById(R.id.content_layout);
MVCHelper<Book> mvcHelper= new MVCNormalHelper<Book>(contentLayout);
// ????? mvcHelper.setDataSource(new BookDetailDataSource());
// ????? mvcHelper.setAdapter(dataAdapter);
// ???? mvcHelper.refresh();
6.????????????????MVCHelper?????
6.??View?MVC
?????View???????????????MVC????
/*** * ???????????? *
* @author LuckyJayce * */ public class BooDetailActivity extends Activity {
private MVCHelper<Book> listViewHelper; private TextView authorTextView; private TextView contentTextView; private TextView descriptionTextView; private TextView nameTextView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookdetail);
nameTextView = (TextView) findViewById(R.id.name_textView);
authorTextView = (TextView) findViewById(R.id.author_textView);
descriptionTextView = (TextView) findViewById(R.id.description_textView);
contentTextView = (TextView) findViewById(R.id.content_textView);
PtrClassicFrameLayout contentLayout = (PtrClassicFrameLayout) findViewById(R.id.rotate_header_list_view_frame);
listViewHelper = new MVCUltraHelper<Book>(contentLayout);
// ?????
listViewHelper.setDataSource(new BookDetailDataSource());
// ?????
listViewHelper.setAdapter(dataAdapter);
// ????
listViewHelper.refresh();
}
@Override protected void onDestroy() {
super.onDestroy();
// ????
listViewHelper.destory();
}
private IDataAdapter<Book> dataAdapter = new IDataAdapter<Book>() {
private Book data;
@Override
public void notifyDataChanged(Book data, boolean isRefresh) {
this.data = data;
authorTextView.setText(data.getAuthor());
contentTextView.setText(data.getContent());
descriptionTextView.setText(data.getDescription());
nameTextView.setText(data.getName());
}
@Override
public boolean isEmpty() {
return data == null;
}
@Override
public Book getData() {
return data;
}
}
;
public void onClickBack(View view) {
finish();
}
}
7.???????????
??
public class MovieDetailDataSource implements IDataSource<Movie>{
}
??Data1???????????
??Data2???????????
??Data3??????????
public class MovieDetailDataSource implements IDataSource<Data3<Movie, List<Discuss>, List<Movie>>>{
}
?? TaskHelper
???????????????ICallback??UI
1.Model (ITask, IAsyncTask)
??????Task??????okhttp,volley???IAsyncTask
/** * @param <DATA> *
???? */ public interface Task<DATA> {
/**
* ??????
*
* @param progressSender
*
???????
* @return
* @throws Exception
*/ public DATA execute(ProgressSender progressSender) throws Exception;
/**
* cancel ? execute ???????????cancle???UI?????
*/ public void cancel();
}
public interface IAsyncTask<DATA> extends ISuperTask<DATA> {
/**
* @param sender ????????????TaskHelper,MVCHelper,?????CallBack?Post????
* @return ?????????????
* @throws Exception
*/
RequestHandle execute(ResponseSender<DATA> sender) throws Exception;
}
????
public class LoginTask implements Task<User> {
private String name; private String password;
public LoginTask(String name, String password) {
super();
this.name = name;
this.password = password;
}
@Override public User execute(ProgressSender progressSender) throws Exception {
if (name.equals("aaa") && password.equals("111")) {
return new User("1", "aaa", 23, "???");
}
else {
throw new BizException("??????????");
}
}
@Override public void cancle() {
}
}
2.View?ICallback?
/** * * @param <DATA> *
???? */ public interface ICallback<DATA> {
/**
* ??task?????
*/ public void onPreExecute(Object task);
/**
* ??????
*
* @param percent
* @param current
* @param total
* @param exraData
*/ public void onProgressUpdate(Object task,int percent, long current, long total, Object exraData);
/**
* ??task????????code?????????task??????????????
*
* @param code
*
???
* @param exception
*
?????throw exception ?????
* @param data
*
?????
*/ public void onPostExecute(Object task,Code code, Exception exception, DATA data);
}
????????UI
private ICallback<User> loginCallback = new ICallback<User>() {
@Override
public void onPreExecute(Object task) {
loginButton.setEnabled(false);
loginButton.setText("???...");
}
@Override
public void onProgress(Object task, int percent, long current, long total, Object extraData) {
}
@Override
public void onPostExecute(Object task, Code code, Exception exception, User user) {
loginButton.setEnabled(true);
loginButton.setText("??");
switch (code) {
case EXCEPTION:
if (exception instanceof BizException) {
BizException bizException = (BizException) exception;
String fail = bizException.getMessage();
Toast.makeText(getApplicationContext(), fail, Toast.LENGTH_SHORT).show();
textView.setText(fail);
}
else {
Toast.makeText(getApplicationContext(), "??????", Toast.LENGTH_SHORT).show();
textView.setText("??????");
}
break;
case SUCCESS:
Toast.makeText(getApplicationContext(), "?????" + new Gson().toJson(user), Toast.LENGTH_LONG).show();
textView.setText("?????" + new Gson().toJson(user));
break;
default:
break;
}
}
}
;
3.Controller (Activity,Fragment,TaskHelper)
Activity?????????
TaskHelper<Object> taskHelper = new TaskHelper<>();
//??TaskHelper?callback?????taskHelpe???task??????.
//taskHelper.registerCallBack(loginCallback);
TaskHandle taskHandle = taskHelper.execute(new LoginAsyncTask(name, password), loginCallback);
//TaskHelper ??????task?TaskHandle?????task?????cancel????task??
TaskHandle taskHandle2 = taskHelper.execute(new LoginAsyncTask(name, password), loginCallback);
//taskHandle.cancel();
//??????
//taskHelper.destory();
//??????
//taskHelper.cancelAll();
//????
???
1.???
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
2.??????????????????????? mvcHelper.setNeedCheckNetwork(false);
????
** ????????????????? **
CoolRefreshView
Android-PullToRefresh-Library
android-Ultra-Pull-To-Refresh-library
android-support-v4.jar
**??????????MVCHelper **
MVCCoolHelper
MVCPullrefshHelper
MVCUltraHelper
MVCSwipeRefreshHelper
????
MVCHelper_Library
????
MVCHelper_Demo
????
1. https://github.com/LuckyJayce/ViewPagerIndicator
Indicator ?? tabhost???????tab?????????tab?????????banner?????????tab???
2. https://github.com/LuckyJayce/MVCHelper
???????????????????????????????????????????????view??????????????
3. https://github.com/LuckyJayce/MultiTypeView
??RecyclerView???type?adapter?Fragment???????RecyclerView????????????????
4. https://github.com/LuckyJayce/EventBus
??????????????????,?????????????????????????
5. https://github.com/LuckyJayce/LargeImage
?????????
6. https://github.com/LuckyJayce/GuideHelper
??????????????view?????????????????
7. https://github.com/LuckyJayce/HVScrollView
???????ScrollView?????ScrollView????????????????
8. https://github.com/LuckyJayce/CoolRefreshView
????RefreshView?????View??? ??????Header???NestedScrollingParent,NestedScrollingChild????????ViewPager???????
?????????6???
?????????
- ??: http://weibo.com/u/3181073384
- QQ ?: ?????????????: 549284336??????
License
Copyright 2015 shizhefei?LuckyJayce? Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.