主activity_xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
rv_view item布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="30dp" /> <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity
package com.bawie.www.demoweek.fragment; import android.os.Bundle; 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.bawie.www.demoweek.R; import com.bawie.www.demoweek.ResultsBean; import com.bawie.www.demoweek.adapter.MyAdapter; import com.bawie.www.demoweek.callback.IView; import com.bawie.www.demoweek.gen.ResultsBeanDao; import com.bawie.www.demoweek.presenter.NewPresenter; import com.bawie.www.demoweek.util.DBUtils; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; /** * Created by 杨文倩 on 2017/12/2. */ public class FragmentOne extends Fragment implements IView { @BindView(R.id.rv) RecyclerView rv; Unbinder unbinder; List<ResultsBean> lists; private MyAdapter adapter; private NewPresenter presenter; private ResultsBeanDao dao; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one, container, false); unbinder = ButterKnife.bind(this, view); presenter = new NewPresenter(); dao = DBUtils.getInstance(getActivity()).getDao(); //先从数据库取数据展示 lists = dao.loadAll(); Log.d("TT", "onCreateView: "+lists.size()+"1111111111111"); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); rv.setLayoutManager(linearLayoutManager); adapter = new MyAdapter(getActivity(), this.lists); rv.setAdapter(adapter); //如果数据库为空就从网路请求 if(lists==null||lists.size()==0){ Log.d("TT", "onCreateView: "+"ewrekgmtrghtkt"); presenter.getData(); } presenter.attchView(this); return view; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } @Override public void success(List<ResultsBean> list) { if(list!=null){ Log.d("TT", "success: "+list.toString()); for (ResultsBean l:list){ Log.d("TT", "success: "+l); dao.insert(l); } adapter.notifyDataSetChanged(); } } @Override public void failed(String msg) { Toast.makeText(getActivity(),"请求失败",Toast.LENGTH_SHORT).show(); } @Override public void onDestroy() { super.onDestroy(); if(presenter!=null){ presenter.detachView(); } } }
MainActivity判断联网
package com.bawie.www.demoweek; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import com.bawie.www.demoweek.bean.IsBean; import com.bawie.www.demoweek.fragment.FragmentFive; import com.bawie.www.demoweek.fragment.FragmentFour; import com.bawie.www.demoweek.fragment.FragmentOne; import com.bawie.www.demoweek.fragment.FragmentThree; import com.bawie.www.demoweek.fragment.FragmentTwo; import com.bawie.www.demoweek.util.NetWorkUtils; import com.hjm.bottomtabbar.BottomTabBar; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { @BindView(R.id.bt) BottomTabBar bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); EventBus.getDefault().register(this); bt.init(getSupportFragmentManager()) .setImgSize(50,50) .setFontSize(8) .setTabPadding(4,6,10) .setChangeColor(Color.RED,Color.DKGRAY) .addTabItem("首页",R.drawable.home_, FragmentOne.class) .addTabItem("分类",R.drawable.fenlei, FragmentTwo.class) .addTabItem("发现",R.drawable.faxian, FragmentThree.class) .addTabItem("购物车",R.drawable.car, FragmentFour.class) .addTabItem("我的",R.drawable.my, FragmentFive.class) .isShowDivider(false) .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() { @Override public void onTabChange(int position, String name) { } }); boolean b = NetWorkUtils.isNetworkConnected(this); IsBean isBean = new IsBean(); isBean.setFlag(b); EventBus.getDefault().post(isBean); } @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行 public void onDataSynEvent(IsBean event) { if(event.isFlag()){ Toast.makeText(this,"玩家已上线",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"网络飘走了",Toast.LENGTH_SHORT).show(); } } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
Newpresenter
package com.bawie.www.demoweek.presenter; import com.bawie.www.demoweek.ResultsBean; import com.bawie.www.demoweek.bean.MessageBean; import com.bawie.www.demoweek.callback.ApiService; import com.bawie.www.demoweek.callback.IView; import com.bawie.www.demoweek.util.RetrofitUtils; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; /** * Created by 杨文倩 on 2017/12/2. */ public class NewPresenter { private IView iView; public void attchView(IView iView){ this.iView=iView; } public void getData(){ RetrofitUtils instance = RetrofitUtils.getInstance(); Retrofit retrofit = instance.getRetrofit(); ApiService apiService = retrofit.create(ApiService.class); Call<MessageBean> call = apiService.getData("10", "1"); call.enqueue(new Callback<MessageBean>() { @Override public void onResponse(Response<MessageBean> response) { MessageBean body = response.body(); List<ResultsBean> results = body.getResults(); if(results!=null){ iView.success(results); } } @Override public void onFailure(Throwable t) { if(t!=null){ iView.failed(t.getMessage()); } } }); } public void detachView(){ if(iView!=null){ iView=null; } } }
IsBean
package com.bawie.www.demoweek.bean; /** * Created by 杨文倩 on 2017/12/2. */ public class IsBean { private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } }
MessageBean
package com.bawie.www.demoweek.bean; import com.bawie.www.demoweek.ResultsBean; import java.util.List; /** * Created by 杨文倩 on 2017/12/2. */ public class MessageBean { private boolean error; private List<ResultsBean> results; public boolean isError() { return error; } public void setError(boolean error) { this.error = error; } public List<ResultsBean> getResults() { return results; } public void setResults(List<ResultsBean> results) { this.results = results; } }
MyAdapter
package com.bawie.www.demoweek.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.bawie.www.demoweek.R; import com.bawie.www.demoweek.ResultsBean; import java.util.List; /** * Created by 杨文倩 on 2017/12/2. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ private Context context; private List<ResultsBean> list; public MyAdapter(Context context, List<ResultsBean> list) { this.context = context; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.rv_view, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.title.setText(list.get(position).getWho()); holder.text.setText(list.get(position).getDesc()); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder { private final TextView title; private final TextView text; public ViewHolder(View itemView) { super(itemView); title = (TextView)itemView.findViewById(R.id.tv_title); text = (TextView) itemView.findViewById(R.id.tv_text); } } }
接口类
package com.bawie.www.demoweek.callback; import com.bawie.www.demoweek.bean.MessageBean; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; /** * Created by 杨文倩 on 2017/12/2. */ public interface ApiService { @GET("data/Android/{num}/{page}") Call<MessageBean> getData(@Path("num") String num, @Path("page") String page); }
接口类2
package com.bawie.www.demoweek.callback; import com.bawie.www.demoweek.ResultsBean; import java.util.List; /** * Created by 杨文倩 on 2017/12/2. */ public interface IView { void success(List<ResultsBean> list); void failed(String msg); }
导入的依赖
compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'org.greenrobot:greendao:3.2.0' compile 'com.hjm:BottomTabBar:1.1.1' compile 'com.jakewharton:butterknife:8.8.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.0' compile 'com.android.support:recyclerview-v7:25.0.0' compile 'com.facebook.fresco:fresco:0.11.0' compile 'org.greenrobot:eventbus:3.0.0' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
导入的greenDao
greendao { schemaVersion 1 daoPackage 'com.bawie.www.demoweek.gen' targetGenDir 'src/main/java' }
App的最上层
apply plugin: 'org.greenrobot.greendao'
工程的依赖里
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' mavenCentral();