首先先创建5个fragment
此步骤省略
创建bean 类
此步骤省略
创建v层
public interface Iview {
void showView(List<User.DataBean.ActivityInfoBean.ActivityInfoListBean> list);
void show(List<Bean> list);
}
创建m层
接口回调
public interface Imodel {
void getData(Observer observer);
}
model层
public class ImodelBack implements Imodel{
@Override
public void getData(Observer observer) {
OkHttpClient OkClient = new OkHttpClient.Builder().build();
//使用Retrofit进行设置
Retrofit retrofit = new Retrofit.Builder()
//添加OkHttpClient
.client(OkClient)
//添加接口头部
.baseUrl(BaseUtils.BASE_URL)
//设置解析方式
.addConverterFactory(GsonConverterFactory.create())
//添加支持RxJAva
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
URLUtils urlUtils = retrofit.create(URLUtils.class);
//得到被观察者
urlUtils.getObservable()
//设置观察者在主线程
.observeOn(AndroidSchedulers.mainThread())
//设置被观察者在子线程
.subscribeOn(Schedulers.io())
//订阅观察者
.subscribe(observer);
}
p层
public class Mypresenter {
Iview iview;
ImodelBack imodelBack;
MyUserDao dao;
Context context;
public Mypresenter(Iview iview,Context context) {
this.iview = iview;
imodelBack=new ImodelBack();
this.context=context;
}
public void setData(boolean state){
dao=new MyUserDao(context);
if(!state){
// Toast.makeText(context, "1111111", Toast.LENGTH_SHORT).show();
//zoushujuku
List<Bean> been = dao.queryUserList();
//Toast.makeText(context, "22222"+been.size(), Toast.LENGTH_SHORT).show();
// Toast.makeText(context, "33333", Toast.LENGTH_SHORT).show();
iview.show(been);
}else{
//Toast.makeText(context, "44444", Toast.LENGTH_SHORT).show();
imodelBack.getData(new Observer<User>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.i("======", "onError: "+e);
}
@Override
public void onNext(User user) {
Log.i("=======", "onNext: "+user.toString());
List<User.DataBean.ActivityInfoBean.ActivityInfoListBean> listBeen=user.getData().getActivityInfo().getActivityInfoList();
iview.showView(listBeen);
dao.deleteAll();
List<User.DataBean.ActivityInfoBean.ActivityInfoListBean> list = user.getData().getActivityInfo().getActivityInfoList();
for (int i = 0; i < list.size(); i++) {
Bean bean = new Bean();
bean.setEndtime(list.get(i).getEndtime());
dao.insertUser(bean);
}
}
});
}
}
}
}
util
public class BaseUtils {
public static final String BASE_URL = "http://m.yunifang.com/";
}
public interface URLUtils {
@GET("yunifang/mobile/home")
Observable<User> getObservable();
}
主页面
public class MainActivity extends AppCompatActivity {
@BindView(R.id.vp)
ViewPager vp;
@BindView(R.id.rg)
RadioGroup rg;
List<Fragment> list;
private One one;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initData();
boolean connectedByState = NetWorkUtils.isConnectedByState(this);
Log.i("---222---", "onCreate: ");
if(connectedByState){
Log.i("----", "showView: ");
Toast.makeText(this,"网络已连接",Toast.LENGTH_LONG).show();
//EventBus.getDefault().postSticky(connectedByState);
}else{
Toast.makeText(this,"无网络连接",Toast.LENGTH_LONG).show();
Log.i("--1111---", "showView: ");
}
MypagerAdpter madpter=new MypagerAdpter(getSupportFragmentManager());
vp.setAdapter(madpter);
one.getState(connectedByState);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
switch (i)
{
case R.id.rb1:
vp.setCurrentItem(0);
break;
case R.id.rb2:
vp.setCurrentItem(1);
break;
case R.id.rb3:
vp.setCurrentItem(2);
break;
case R.id.rb4:
vp.setCurrentItem(3);
break;
case R.id.rb5:
vp.setCurrentItem(4);
break;
}
}
});
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position)
{
case 0:
rg.check(R.id.rb1);
break;
case 1:
rg.check(R.id.rb2);
break;
case 2:
rg.check(R.id.rb3);
break;
case 3:
rg.check(R.id.rb4);
break;
case 4:
rg.check(R.id.rb5);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initData() {
list=new ArrayList<Fragment>();
one = new One();
list.add(one);
list.add(new Two());
list.add(new Three());
list.add(new Four());
list.add(new Fieve());
}
class MypagerAdpter extends FragmentPagerAdapter{
public MypagerAdpter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
}
fragmrnt
public class One extends Fragment implements Iview{
RecyclerView rv;
Mypresenter pp;
private boolean state1;
boolean flag;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.one, null);
// EventBus.getDefault().register(getActivity());
rv=view.findViewById(R.id.rv);
initData();
return view;
}
@Override
public void onResume() {
super.onResume();
pp=new Mypresenter(this,getActivity());
pp.setData(flag);
}
private void initData() {
LinearLayoutManager lin=new LinearLayoutManager(getActivity());
rv.setLayoutManager(lin);
}
@Override
public void showView(List<User.DataBean.ActivityInfoBean.ActivityInfoListBean> listBeen) {
rv.setAdapter(new Myadpter(listBeen,getActivity()));
}
@Override
public void show(List<Bean> list) {
rv.setAdapter(new Myadpter_noneNet(list,getActivity()));
}
public void getState(boolean type){
this.flag=type;
}
}
网友评论