用MVP框架实现引导页

首先定义一个Constant类写接口

public class Constant {
    public static final String YDAO_URL = "http://www.zhaoapi.cn/ad/getAd";
 
}

契约类

public interface YinContract {

    
    public interface YinView{
        public void showData(User user);
    }

    public interface YinPresenter<YinView>{
        public void attachData(YinView yinView);

        public void detachData(YinView yinView);

        public void responseData();
    }
    public interface YinModel{
        public void containResponseData(CallBack callBack);

        public interface CallBack{
            public void resquestData(User user);
        }
    }
}

 

M层

public class YinModel implements YinContract.YinModel {
    @Override
    public void containResponseData(final CallBack callBack) {
        OkGo.<String>post(Constant.YDAO_URL).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String urltring = response.body().toString();
                Gson gson = new Gson();

                User user = gson.fromJson(urltring,User.class);

                callBack.resquestData(user);
            }
        });
    }
}

P层

public class YinPresenter implements YinContract.YinPresenter<YinContract.YinView> {

    YinContract.YinView yinView;
    private SoftReference<YinContract.YinView> softReference;
    private YinModel yinModel;

    @Override
    public void attachData(YinContract.YinView yinView) {
        this.yinView = yinView;
        softReference = new SoftReference<>(yinView);
        yinModel = new YinModel();
    }

    @Override
    public void detachData(YinContract.YinView yinView) {
        softReference.clear();
    }

    @Override
    public void responseData() {
        yinModel.containResponseData(new YinContract.YinModel.CallBack() {
            @Override
            public void resquestData(User user) {
                yinView.showData(user);
            }
        });
    }
}

主页面布局

<?xml version="1.0" encoding="utf-8"?>
<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"
   >


    <android.support.v4.view.ViewPager
        android:id="@+id/home_viewPage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher" />

    <Button
        android:id="@+id/home_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:text="开启购物" />


</RelativeLayout>

主页面代码

public class MainActivity extends AppCompatActivity implements YinContract.YinView {

    @BindView(R.id.home_viewPage)
    ViewPager homeViewPage;
    @BindView(R.id.home_button2)
    Button homeButton2;
    Context context;
    private YinPresenter yinPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        context = MainActivity.this;
        homeButton2.setVisibility(View.GONE);
        yinPresenter = new YinPresenter();
        yinPresenter.attachData(this);
        yinPresenter.responseData();
    }

    @Override
    public void showData(User user) {
        final ArrayList<User.DataBean> list = (ArrayList<User.DataBean>) user.getData();

        MyAdapter myAdapter = new MyAdapter();
        myAdapter.act(context,list);
        homeViewPage.setAdapter(myAdapter);

        homeViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i==list.size()-1){
                    homeButton2.setVisibility(View.VISIBLE);
                }else {
                    homeButton2.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @OnClick(R.id.home_button2)
    public void onViewClicked() {
        startActivity(new Intent(MainActivity.this,TwoActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        yinPresenter.detachData(this);
    }
}

Adapter适配器

public class MyAdapter extends PagerAdapter {

    ArrayList<User.DataBean> list;
    Context context;

    public void act(Context context,ArrayList<User.DataBean> list){
        this.list = list;
        this.context = context;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return  view==o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        Glide.with(context).load(list.get(position).getIcon()).into(imageView);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View)object);
    }
}

 

完了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值