Api包下
package com. example. zz2019330. Api;
public class Api {
public static final String Api= "http://172.17.8.100/small/" ;
public static final String APIbANNER= "commodity/v1/bannerShow" ;
public static final String ApisHOW= "commodity/v1/commodityList" ;
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
package com. example. zz2019330. Api;
import java. util. Map;
import okhttp3. ResponseBody;
import retrofit2. http. DELETE;
import retrofit2. http. GET;
import retrofit2. http. Header;
import retrofit2. http. POST;
import retrofit2. http. PUT;
import retrofit2. http. QueryMap;
import retrofit2. http. Url;
import rx. Observable;
public interface ApiService {
@POST
Observable< ResponseBody> doPost ( @Url String url ,
@QueryMap Map< String , String> map,
@Header ( "userId" ) String userId,
@Header ( "sessionId" ) String sessionId) ;
@GET
Observable< ResponseBody> allGet ( @Url String url ,
@QueryMap Map< String , String> map,
@Header ( "userId" ) String userId,
@Header ( "sessionId" ) String sessionId) ;
@GET
Observable< ResponseBody> dompGet ( @Url String url, @QueryMap Map< String, String> params) ;
@GET
Observable< ResponseBody> doGet ( @Url String url) ;
@DELETE
Observable< ResponseBody> dodelete ( @Url String url ,
@QueryMap Map< String , String> map,
@Header ( "userId" ) String userId,
@Header ( "sessionId" ) String sessionId) ;
@PUT
Observable< ResponseBody> doput ( @Url String url ,
@QueryMap Map< String , String> map,
@Header ( "userId" ) String userId,
@Header ( "sessionId" ) String sessionId) ;
}
工具类
package com. example. zz2019330. Utils;
import com. example. zz2019330. Api. Api;
import com. example. zz2019330. Api. ApiService;
import java. io. IOException;
import java. util. HashMap;
import java. util. concurrent. TimeUnit;
import okhttp3. OkHttpClient;
import okhttp3. ResponseBody;
import okhttp3. logging. HttpLoggingInterceptor;
import retrofit2. Retrofit;
import retrofit2. adapter. rxjava. RxJavaCallAdapterFactory;
import retrofit2. converter. gson. GsonConverterFactory;
import rx. Observer;
import rx. Scheduler;
import rx. android. schedulers. AndroidSchedulers;
import rx. schedulers. Schedulers;
public class RetrofitUtils {
private final ApiService myService;
private RetrofitUtils ( ) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor ( ) ;
interceptor. setLevel ( HttpLoggingInterceptor. Level. BODY) ;
OkHttpClient okHttpClient = new OkHttpClient. Builder ( )
. addInterceptor ( interceptor)
. writeTimeout ( 10 , TimeUnit. SECONDS)
. readTimeout ( 10 , TimeUnit. SECONDS)
. connectTimeout ( 10 , TimeUnit. SECONDS)
. retryOnConnectionFailure ( true )
. build ( ) ;
Retrofit retrofit = new Retrofit. Builder ( )
. addCallAdapterFactory ( RxJavaCallAdapterFactory. create ( ) )
. addConverterFactory ( GsonConverterFactory. create ( ) )
. baseUrl ( Api. Api)
. client ( okHttpClient)
. build ( ) ;
myService = retrofit. create ( ApiService. class ) ;
}
public static RetrofitUtils getInstance ( ) {
return RetrofitHolder. UTIL;
}
private static class RetrofitHolder {
private static final RetrofitUtils UTIL= new RetrofitUtils ( ) ;
}
public RetrofitUtils doGet ( String url, HashMap< String, String> map, String userId, String sessionId, HttpListener listener) {
myService. doGet ( url) . observeOn ( AndroidSchedulers. mainThread ( ) )
. subscribeOn ( Schedulers. io ( ) )
. subscribe ( getObserver ( listener) ) ;
return RetrofitUtils. getInstance ( ) ;
}
public Observer getObserver ( final HttpListener httpListener) {
Observer observer= new Observer < ResponseBody> ( ) {
@Override
public void onCompleted ( ) {
}
@Override
public void onError ( Throwable e) {
if ( httpListener!= null) {
httpListener. failure ( e. getMessage ( ) ) ;
}
}
@Override
public void onNext ( ResponseBody responseBody) {
if ( httpListener!= null) {
try {
httpListener. success ( responseBody. string ( ) ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
} ;
return observer;
}
public interface HttpListener {
void success ( String result) ;
void failure ( String error) ;
}
private HttpListener httpListener;
public void setHttpListener ( HttpListener httpListener) {
this . httpListener= httpListener;
}
}
契约类
package com. example. zz2019330. Utils;
public interface ContractCallBack < T> {
void usuccess ( T uresult) ;
void udefeated ( T uerror) ;
}
Model
package com. example. zz2019330. Model;
import com. example. zz2019330. Utils. ContractCallBack;
import java. util. HashMap;
public interface ModelCallBack {
void getShowModelData ( String url, HashMap< String, String> map, String userId, String sessionId, int type, ContractCallBack utilsCallBack) ;
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- model类
package com. example. zz2019330. Model;
import com. example. zz2019330. Bean. ShowBean;
import com. example. zz2019330. Bean. ZahnshiBean;
import com. example. zz2019330. Utils. ContractCallBack;
import com. example. zz2019330. Utils. RetrofitUtils;
import com. google. gson. Gson;
import java. util. HashMap;
public class Model implements ModelCallBack {
@Override
public void getShowModelData ( String url, HashMap< String, String> map, String userId, String sessionId, int type, ContractCallBack utilsCallBack) {
switch ( type) {
case 1 :
getData ( url, map, utilsCallBack) ;
break ;
case 2 :
getShowqData ( url, map, utilsCallBack) ;
break ;
}
}
private void getShowqData ( String url, HashMap< String, String> map, final ContractCallBack utilsCallBack) {
RetrofitUtils. getInstance ( ) . doGet ( url, null, null, null, new RetrofitUtils. HttpListener ( ) {
@Override
public void success ( String result) {
ZahnshiBean zahnshiBean = new Gson ( ) . fromJson ( result, ZahnshiBean. class ) ;
utilsCallBack. usuccess ( zahnshiBean) ;
}
@Override
public void failure ( String error) {
utilsCallBack. udefeated ( error) ;
}
} ) ;
}
private void getData ( String url, HashMap< String, String> map, final ContractCallBack contractCallBack) {
RetrofitUtils. getInstance ( ) . doGet ( url, null, null, null, new RetrofitUtils. HttpListener ( ) {
@Override
public void success ( String result) {
ShowBean showBean = new Gson ( ) . fromJson ( result, ShowBean. class ) ;
contractCallBack. usuccess ( showBean) ;
}
@Override
public void failure ( String error) {
contractCallBack. udefeated ( error) ;
}
} ) ;
}
}
Presenter
package com. example. zz2019330. Presenter;
import java. util. HashMap;
public interface PresenterCallBack {
void getShowPresenterData ( String url, HashMap< String, String> map, String userId, String sessionId, int type) ;
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - Presenter类
package com. example. zz2019330. Presenter;
import com. example. zz2019330. Model. Model;
import com. example. zz2019330. Utils. ContractCallBack;
import com. example. zz2019330. View. View;
import java. util. HashMap;
public class Presenter implements PresenterCallBack {
private Model model;
private View view;
public Presenter ( View view) {
model= new Model ( ) ;
this . view = view;
}
@Override
public void getShowPresenterData ( String url, HashMap< String, String> map, String userId, String sessionId, int type) {
model. getShowModelData ( url, map, userId, sessionId, type, new ContractCallBack ( ) {
@Override
public void usuccess ( Object uresult) {
view. onsuess ( uresult) ;
}
@Override
public void udefeated ( Object uerror) {
view. onError ( uerror) ;
}
} ) ;
}
}
View
package com. example. zz2019330. View;
public interface View < T> {
void onsuess ( T result) ;
void onError ( T error) ;
}
调用
package com. example. zz2019330. Fragment;
import android. os. Bundle;
import android. support. annotation. NonNull;
import android. support. annotation. Nullable;
import android. support. v4. app. Fragment;
import android. support. v7. widget. LinearLayoutManager;
import android. support. v7. widget. RecyclerView;
import android. util. Log;
import android. view. LayoutInflater;
import android. view. View;
import android. view. ViewGroup;
import android. widget. Toast;
import com. example. zz2019330. Api. Api;
import com. example. zz2019330. BaseFragment;
import com. example. zz2019330. Bean. ShowBean;
import com. example. zz2019330. Bean. ZahnshiBean;
import com. example. zz2019330. MyAdapter;
import com. example. zz2019330. Presenter. Presenter;
import com. example. zz2019330. R;
import java. util. HashMap;
import java. util. List;
public class Fragment01 < T> extends BaseFragment implements com. example. zz2019330. View. View < T> {
private RecyclerView recyclerView;
private Presenter presenter;
@Override
protected int oncreateview ( ) {
return R. layout. fragment01;
}
@Override
protected void initView ( View view) {
recyclerView = view. findViewById ( R. id. recyclerView) ;
}
@Override
protected void initDate ( ) {
}
@Override
protected void loadData ( ) {
presenter = new Presenter ( this ) ;
presenter. getShowPresenterData ( Api. ApisHOW, null, null, null, 2 ) ;
presenter. getShowPresenterData ( Api. APIbANNER, null, null, null, 1 ) ;
}
@Override
public void onsuess ( T result) {
if ( result instanceof ShowBean ) {
ShowBean showBean= ( ShowBean) result;
List< ShowBean. ResultBean> data = showBean. getResult ( ) ;
Log. i ( "ppp" , "onsuess: " + data. size ( ) ) ;
} else if ( result instanceof ZahnshiBean ) {
ZahnshiBean zahnshiBean= ( ZahnshiBean) result;
ZahnshiBean. ResultBean result1 = zahnshiBean. getResult ( ) ;
List< ZahnshiBean. ResultBean. MlssBean. CommodityListBeanXX> commodityList = result1. getMlss ( ) . getCommodityList ( ) ;
Log. i ( "iii" , "onsuess://" + result1. getMlss ( ) + "=====" ) ;
}
}
@Override
public void onError ( T error) {
}
}
懒加载 Base Fragment 基类
package com. example. zz2019330;
import android. os. Bundle;
import android. support. annotation. NonNull;
import android. support. annotation. Nullable;
import android. support. v4. app. Fragment;
import android. view. LayoutInflater;
import android. view. View;
import android. view. ViewGroup;
public abstract class BaseFragment extends Fragment {
private boolean isViewCreated;
private boolean isUIVisible;
@Nullable
@Override
public View onCreateView ( @NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= View. inflate ( getActivity ( ) , oncreateview ( ) , null) ;
initView ( view) ;
initDate ( ) ;
return view;
}
protected abstract int oncreateview ( ) ;
protected abstract void initView ( View view) ;
protected abstract void initDate ( ) ;
@Override
public void onViewCreated ( @NonNull View view, @Nullable Bundle savedInstanceState) {
super . onViewCreated ( view, savedInstanceState) ;
isViewCreated = true ;
lazyLoad ( ) ;
}
@Override
public void setUserVisibleHint ( boolean isVisibleToUser) {
super . setUserVisibleHint ( isVisibleToUser) ;
if ( isVisibleToUser) {
isUIVisible = true ;
lazyLoad ( ) ;
} else {
isUIVisible = false ;
}
}
private void lazyLoad ( ) {
if ( isViewCreated && isUIVisible) {
loadData ( ) ;
isViewCreated = false ;
isUIVisible = false ;
}
}
@Override
public void onDestroy ( ) {
super . onDestroy ( ) ;
isViewCreated = false ;
isUIVisible = false ;
}
protected abstract void loadData ( ) ;
}