MainAcitvty 用来视图展示
public class MainActivity extends AppCompatActivity implements ShowContract.ShowView {
private RecyclerView RecyView;
private ShowPresenter showPresenter;
private ShowAdapter showAdapter;
private DataBeanDao dataBeanDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataBeanDao = DaoMaster.newDevSession(getBaseContext(), DataBeanDao.TABLENAME).getDataBeanDao();
RecyView=findViewById(R.id.RecyView);
if (IfNetWork.isNetworkConnected(this)) {
HashMap<String,String> params=new HashMap<>();
params.put("page","1");
new ShowPresenter(this).getShowPresneter(params);
}else{
List<DataBean> dataBeans = dataBeanDao.loadAll();
Toast.makeText(getBaseContext(),"没网状态",Toast.LENGTH_LONG).show();
showAdapter = new ShowAdapter(this, dataBeans);
RecyView.setLayoutManager(new LinearLayoutManager(this));
RecyView.setAdapter(showAdapter);
}
}
@Override
public void Success(List<DataBean> resultBeans) {
showAdapter = new ShowAdapter(this, resultBeans);
RecyView.setLayoutManager(new LinearLayoutManager(this));
RecyView.setAdapter(showAdapter);
for(int i=0;i<resultBeans.size();i++){
DataBean dataBean = resultBeans.get(i);
dataBeanDao.insertOrReplace(new DataBean(i,null,null,null,null,dataBean.getAuthor_name(),null
,null,dataBean.getThumbnail_pic_s03()));
}
}
@Override
public void FailUre(String msg) {
}
}
api Api 接口
public interface Api {
@GET
Call<UserBean> show(@Url String url, @QueryMap HashMap<String,String> params);
}
ApiUrl
public class ApiUrl {
public static String SHOW="api/news/news.php";
}
Bean类里面
@Entity
public class DataBean {
@Id
private long id;
contract
public interface ShowContract {
public abstract class ShowPresenter{
public abstract void getShowPresneter (HashMap<String,String>params);
}
public interface ShowModel{
void getShowModel(HashMap<String,String> params,ShowModelCallBack showModelCallBack);
}
public interface ShowView{
void Success(List<DataBean> resultBeans);
void FailUre(String msg);
}
public interface ShowModelCallBack{
void Success(List<DataBean> dataBeanList);
void FailUre(String msg);
}
}
model
public class ShowModel implements ShowContract.ShowModel {
@Override
public void getShowModel(HashMap<String, String> params, final ShowContract.ShowModelCallBack showModelCallBack) {
Api api=RetrofitUtils.getInstance().api();
Call<UserBean> userBeanCall = api.show(ApiUrl.SHOW, params);
userBeanCall.enqueue(new Callback<UserBean>() {
@Override
public void onResponse(Call<UserBean> call, Response<UserBean> response) {
if (showModelCallBack!=null){
if (showModelCallBack!=null){
showModelCallBack.Success(response.body().getData());
}
}
}
@Override
public void onFailure(Call<UserBean> call, Throwable t) {
if (showModelCallBack!=null){
showModelCallBack.FailUre(t.getMessage());
}
}
});
}
}
presenter
public class ShowPresenter extends ShowContract.ShowPresenter {
private ShowContract.ShowView showView;
private ShowModel showModel;
public ShowPresenter(ShowContract.ShowView showView){
this.showView=showView;
showModel = new ShowModel();
}
@Override
public void getShowPresneter(HashMap<String, String> params) {
showModel.getShowModel(params, new ShowContract.ShowModelCallBack() {
@Override
public void Success(List<DataBean> dataBeanList) {
showView.Success(dataBeanList);
}
@Override
public void FailUre(String msg) {
showView.FailUre(msg);
}
});
}
}
net
public class RetrofitUtils {
private final Retrofit retrofit;
private static RetrofitUtils mInstance;
private RetrofitUtils(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.addNetworkInterceptor(httpLoggingInterceptor)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("http://www.xieast.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public static RetrofitUtils getInstance(){
if (mInstance==null){
synchronized (RetrofitUtils.class){
mInstance = new RetrofitUtils();
}
}
return mInstance;
}
public Api api(){
return retrofit.create(Api.class);
}
}
自定义缓存路径
public class aaaaa extends Application {
@Override
public void onCreate() {
super.onCreate();
DiskCacheConfig config = DiskCacheConfig.newBuilder(this)
.setBaseDirectoryName("images")
.setBaseDirectoryPath(Environment.getExternalStorageDirectory())
.build();
ImagePipelineConfig build = ImagePipelineConfig.newBuilder(this)
.setMainDiskCacheConfig(config)
.build();
Fresco.initialize(this,build);
}
}
网络判断
public class IfNetWork {
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
}
xml 布局
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
item
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/simpimage"
android:layout_width="100dp"
android:layout_height="100dp"
fresco:roundTopLeft="true"
fresco:roundBottomLeft="true"
fresco:roundTopRight="true"
fresco:roundBottomRight="true"
fresco:roundedCornerRadius="20dp"
/>
<TextView
android:id="@+id/text_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="成功就在眼前"
android:textSize="15sp"
/>
</LinearLayout>