导入Fresco,EventBus,GreenDao,Butterknife,Retrofit等的依赖
bean类
public class Bean<T> {
private boolean error;
private T results;
public Bean(boolean error, T results) {
this.error = error;
this.results = results;
}
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public T getResults() {
return results;
}
public void setResults(T results) {
this.results = results;
}
@Override
public String toString() {
return "Bean{" +
"error=" + error +
", results=" + results +
'}';
}
//数据请求bean类
public class ResultsBean {
private String _id;
private String createdAt;
private String desc;
private String publishedAt;
private String source;
private String type;
private String url;
private boolean used;
private String who;
private List<String> images;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isUsed() {
return used;
}
public void setUsed(boolean used) {
this.used = used;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
@Override
public String toString() {
return "ResultsBean{" +
"_id='" + _id + '\'' +
", createdAt='" + createdAt + '\'' +
", desc='" + desc + '\'' +
", publishedAt='" + publishedAt + '\'' +
", source='" + source + '\'' +
", type='" + type + '\'' +
", url='" + url + '\'' +
", used=" + used +
", who='" + who + '\'' +
", images=" + images +
'}';
}
}
//数据库bean类
@Entity
public class MyBean {
@Id
private Long id;
private String desc;
private String publishedAt;
private String images;
public String getImages() {
return this.images;
}
public void setImages(String images) {
this.images = images;
}
public String getPublishedAt() {
return this.publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getDesc() {
return this.desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Generated(hash = 883105429)
public MyBean(Long id, String desc, String publishedAt, String images) {
this.id = id;
this.desc = desc;
this.publishedAt = publishedAt;
this.images = images;
}
@Generated(hash = 1281580447)
public MyBean() {
}
@Override
public String toString() {
return "MyBean{" +
"id=" + id +
", desc='" + desc + '\'' +
", publishedAt='" + publishedAt + '\'' +
", images='" + images + '\'' +
'}';
}
}
请求数据接口
public interface ApiService {
@GET("api/data/Android/10/1")
Call<Bean<List<ResultsBean>>> login();
}
//网络判断
public class WifeUtils {
private static volatile WifeUtils instance;
private Context context;
private WifeUtils(Context context) {
this.context = context;
}
public static WifeUtils getInstance(Context context) {
if (instance == null) {
synchronized (WifeUtils.class) {
if (instance == null) {
instance = new WifeUtils(context);
}
}
}
return instance;
}
public int getNetype() {
int netType = -1;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
//无网络
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
//手机网络
if (nType == ConnectivityManager.TYPE_MOBILE) {
netType = 2;
} else if (nType == ConnectivityManager.TYPE_WIFI) {//wifi网络
netType = 1;
}
//返回
return netType;
}
}
//适配器
//fragmentpublic class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> { private FragmentActivity activity; private List<MyBean> myBeen; public CategoryAdapter(FragmentActivity activity, List<MyBean> myBeen) { this.activity = activity; this.myBeen = myBeen; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(activity, R.layout.item_category, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Glide.with(activity).load(myBeen.get(position).getImages()).into(holder.imgLogo); holder.txtName.setText(myBeen.get(position).getDesc()); holder.txtTime.setText(myBeen.get(position).getPublishedAt()); } @Override public int getItemCount() { return myBeen.size(); } class ViewHolder extends RecyclerView.ViewHolder { private ImageView imgLogo; private TextView txtName; private TextView txtTime; public ViewHolder(View itemView) { super(itemView); imgLogo = (ImageView) itemView.findViewById(R.id.iv); txtName = (TextView) itemView.findViewById(R.id.textView); txtTime = (TextView) itemView.findViewById(R.id.textView2); } } }
//MainActivitypublic class OneFragment extends Fragment { @BindView(R.id.rv_catagory) RecyclerView rvCatagory; Unbinder unbinder; private View view; private MyBeanDao dao; private MyBean bean; private List<MyBean> list = new ArrayList<>(); private CategoryAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(getActivity(), R.layout.fragment1, null); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); int netype = WifeUtils.getInstance(getActivity()).getNetype(); rvCatagory.setLayoutManager(new LinearLayoutManager(getActivity())); adapter = new CategoryAdapter(getActivity(), list); rvCatagory.setAdapter(adapter); dao = DbHelper.getInstance(getActivity()).getMyBeanDao(); if(netype!=-1){ Toast.makeText(getActivity(),"有网",Toast.LENGTH_SHORT).show(); list.clear(); insert(); }else{ Toast.makeText(getActivity(),"没网",Toast.LENGTH_SHORT).show(); list.clear(); query(); } insert(); } private void query() { list.clear(); List<MyBean> beanList = dao.loadAll(); list.addAll(beanList); adapter.notifyDataSetChanged(); } private void insert() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://gank.io/") .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService service = retrofit.create(ApiService.class); Call<Bean<List<ResultsBean>>> call = service.login(); call.enqueue(new Callback<Bean<List<ResultsBean>>>() { @Override public void onResponse(Call<Bean<List<ResultsBean>>> call, Response<Bean<List<ResultsBean>>> response) { Toast.makeText(getActivity(), "网络请求成功", Toast.LENGTH_SHORT).show(); Bean<List<ResultsBean>> body = response.body(); List<ResultsBean> been = body.getResults(); list.clear(); dao.deleteAll(); for (int i = 0; i < been.size(); i++) { MyBean myBean = new MyBean(); myBean.setId(null); myBean.setPublishedAt( been.get(i).getPublishedAt()); myBean.setDesc(been.get(i).getDesc()); List<String> images = been.get(i).getImages(); if (images != null) { myBean.setImages(images.get(0)); } list.add(myBean); dao.insert(myBean); Log.e("CDM" + "", "onResponse:list " + list); } adapter.notifyDataSetChanged(); } @Override public void onFailure(Call<Bean<List<ResultsBean>>> call, Throwable t) { Toast.makeText(getActivity(), "网络请求失败,原因是:" + t.getMessage(), Toast.LENGTH_SHORT).show(); } }); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } }
//主布局文件public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomTabBar mb = (BottomTabBar) findViewById(R.id.bottom_tab_bar); mb.init(getSupportFragmentManager()) .setImgSize(50, 50) .setFontSize(8) .setTabPadding(4, 6, 10) .setChangeColor(Color.RED, Color.DKGRAY) .addTabItem("首页", R.drawable.dfk, OneFragment.class) .addTabItem("想法", R.drawable.dsh, TwoFragment.class) .addTabItem("市场", R.drawable.kind_liner, ThreeFragment.class) .addTabItem("通知", R.drawable.th_sh, FourFragment.class) .addTabItem("更多", R.drawable.normalheart, FiveFragment.class) .isShowDivider(false) .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() { @Override public void onTabChange(int position, String name) { } }); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.hjm.bottomtabbar.BottomTabBar android:id="@+id/bottom_tab_bar" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>//Fragment布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_catagory" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>//子bean类<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/iv" android:background="@drawable/loading_01"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> </LinearLayout>