MVP加Retrofit的用法的一个轮播加Recyclerview列表展示的一个案例

首先是依赖

    //recyclerview
    implementation 'jp.wasabeef:recyclerview-animators:2.3.0'
    //fresco
    implementation 'com.facebook.fresco:fresco:1.9.0'
    //retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.youth.banner:banner:1.4.10'
    //glide
    implementation 'com.github.bumptech.glide:glide:4.4.0'

接口

//View接口
public interface IMainView {

    void showSkin(List<SkinBean.DataBean.SubjectsBean> subjects);

    void showError(String error);
}

//Model接口
public interface IModel {
    //获取数据
    void getSkin(String uri);
}
隶属于model下的注解请求
public interface MyService {

    @GET("umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611")
    Call<SkinBean> getSkin(@Query("uri") String uri);
}


//Presenter接口
public interface IPresenter {

    //p的请求数据
    void showSkinToView(IModel iModel, IMainView iMainView);

    //接受从m传上来的数据
    void getData(List<SkinBean.DataBean.SubjectsBean> subjects);

    //接受异常
    void getError(String error);
}

Model层的请求数据

public class ModelFusion implements IModel {

    private final IPresenter iPresenter;

    public ModelFusion(IPresenter iPresenter) {
        this.iPresenter = iPresenter;
    }

    @Override
    public void getSkin(String uri) {

        //网络请求
        RetrofitUtil retrofitUtil = RetrofitUtil.getInstance();
        MyService myService = retrofitUtil.createRequest(MyService.class);
        Call<SkinBean> skin = myService.getSkin(uri);
        //执行
        skin.enqueue(new Callback<SkinBean>() {
            @Override
            public void onResponse(Call<SkinBean> call, Response<SkinBean> response) {
                if (response.isSuccessful()){
                    SkinBean body = response.body();
                    List<SkinBean.DataBean.SubjectsBean> subjects = body.getData().getSubjects();
                    iPresenter.getData(subjects);
                }
            }

            @Override
            public void onFailure(Call<SkinBean> call, Throwable t) {

            }
        });

    }
}

P层的逻辑

public class PresenterFusion implements IPresenter {

    private IMainView iMainView;
    private String uri = "homepage";
    @Override
    public void showSkinToView(IModel iModel, IMainView iMainView) {
        this.iMainView = iMainView;
        //model请求数据
        iModel.getSkin(uri);
    }

    @Override
    public void getData(List<SkinBean.DataBean.SubjectsBean> subjects) {

        iMainView.showSkin(subjects);
    }

    @Override
    public void getError(String error) {
        iMainView.showError(error);
    }
}

View层的视图展示

public class MainActivity extends AppCompatActivity implements IMainView {

    private Banner main_banner;
    private RecyclerView main_miao;
    private PresenterFusion presenterFusion;
    private MyAdapter myAdapter;
    //倒计时用到的变量
    private TextView tvHour;
    private TextView tvMinute;
    private TextView tvSecond;
    static long mHour = 02;
    static long mMin = 15;
    boolean isRun = true;
    static long mSecond = 36;
    private Handler timeHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == 1) {
                //调用 倒计时计算的方法
                computeTime();
                if (mHour < 10) {
                    tvHour.setText("0" + mHour + "");
                } else {
                    tvHour.setText(mHour + "");
                }
                if (mMin < 10) {
                    tvMinute.setText("0" + mMin + "");
                } else {
                    tvMinute.setText(mMin + "");
                }
                if (mSecond < 10) {
                    tvSecond.setText("0" + mSecond + "");
                } else {
                    tvSecond.setText(mSecond + "");
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        startRun();
        initDatas();
        initBanner();
    }

    private void initViews() {
        main_banner = findViewById(R.id.main_banner);
        main_miao = findViewById(R.id.main_miao);
        //倒计时
        tvHour = findViewById(R.id.tv_hour);
        tvMinute = findViewById(R.id.tv_minute);
        tvSecond = findViewById(R.id.tv_second);
    }

    public void initDatas(){
        presenterFusion = new PresenterFusion();
        presenterFusion.showSkinToView(new ModelFusion(presenterFusion),this);
    }

    private void initBanner() {

        //设置banner样式...CIRCLE_INDICATOR_TITLE包含标题
        main_banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        main_banner.setImageLoader(new GlideImageLoader());
        //设置自动轮播,默认为true
        main_banner.isAutoPlay(true);
        //设置轮播时间
        main_banner.setDelayTime(3000);
        //设置指示器位置(当banner模式中有指示器时)
        main_banner.setIndicatorGravity(BannerConfig.CENTER);
    }

    @Override
    public void showSkin(List<SkinBean.DataBean.SubjectsBean> subjects) {

        List<String> imgs = new ArrayList<>();
        for (int i = 0; i < subjects.size(); i++) {
            String image = subjects.get(i).getImage();
            imgs.add(image);
        }

        main_banner.setImages(imgs);
        main_banner.start();

        main_miao.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
        myAdapter = new MyAdapter(MainActivity.this,subjects);
        main_miao.setAdapter(myAdapter);
    }

    @Override
    public void showError(String error) {

    }

    /**
     * 开启倒计时
     */
    private void startRun() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRun) {
                    try {
                        //睡眠一秒发送消息handler
                        Thread.sleep(1000);
                        Message message = Message.obtain();
                        message.what = 1;
                        //发送消息
                        timeHandler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    //调用 倒计时计算的方法

    /**
     * 倒计时计算
     */
    private static void computeTime() {
        //首先把秒减1
        mSecond--;
        if (mSecond < 0) {//如果秒已经减到了0
            mMin--;//分钟就减1
            mSecond = 59;//秒变成 59
            if (mMin < 0) {//如果分钟小于0
                mMin = 59;//分钟变成59
                mHour--;//小时减1
            }
        }
    }
}
//倒计时的一个shape
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>
    <size android:width="10dp" android:height="10dp"/>
    <solid android:color="#000"/>
</shape>

列表的适配器

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<SkinBean.DataBean.SubjectsBean> data;
    //判断条目的类型
    private int ONE = 0;
    private int TWO = 1;

    public MyAdapter(Context context, List<SkinBean.DataBean.SubjectsBean> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == ONE) {
            View oview = LayoutInflater.from(context).inflate(R.layout.one_item, parent, false);
            MyHolderone myHolderone = new MyHolderone(oview);
            return myHolderone;
        } else if (viewType == TWO) {
            View tview = LayoutInflater.from(context).inflate(R.layout.two_item, parent, false);
            MyHoldertwo myHoldertwo = new MyHoldertwo(tview);
            return myHoldertwo;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof MyHolderone) {

            ((MyHolderone) holder).one_text.setText(data.get(position).getDetail());
            Uri uri = Uri.parse(data.get(position).getDescImage());
            ((MyHolderone) holder).one_img.setImageURI(uri);

        } else if (holder instanceof MyHoldertwo) {

            ((MyHoldertwo) holder).two_text.setText(data.get(position).getDetail());

            Uri uri = Uri.parse(data.get(position).getDescImage());
            ((MyHoldertwo) holder).two_img.setImageURI(uri);

        }
    }

    @Override
    public int getItemViewType(int position) {

        if (position % 2 == 0) {
            return ONE;
        } else {
            return TWO;
        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

}

布局

//一个有倒计时的主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".view.MainActivity">


    <com.youth.banner.Banner
        android:id="@+id/main_banner"
        android:layout_width="match_parent"
        android:layout_height="300dp">

    </com.youth.banner.Banner>

    <!--京东秒杀-->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--时间-->
        <LinearLayout
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="16dp">

            <TextView
                android:textColor="#FF3848"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="17sp"
                android:text="京东秒杀"/>

            <TextView
                android:textColor="#222222"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="14sp"
                android:text="  12点场 "/>
            <TextView
                android:textStyle="bold"
                android:id="@+id/tv_hour"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:paddingTop="3dp"
                android:paddingBottom="3dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:background="@drawable/daojishi_shape"
                android:textColor="@android:color/white"
                android:textSize="11sp"
                android:text="02"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:textStyle="bold"
                android:textColor="@android:color/black"
                android:text=":"/>
            <TextView
                android:textStyle="bold"
                android:id="@+id/tv_minute"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:paddingTop="3dp"
                android:paddingBottom="3dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:background="@drawable/daojishi_shape"
                android:textColor="@android:color/white"
                android:textSize="11sp"
                android:text="15"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:textStyle="bold"
                android:textColor="@android:color/black"
                android:text=":"/>
            <TextView
                android:textStyle="bold"
                android:id="@+id/tv_second"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:paddingTop="3dp"
                android:paddingBottom="3dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:background="@drawable/daojishi_shape"
                android:textColor="@android:color/white"
                android:textSize="11sp"
                android:text="36"/>

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/main_miao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </android.support.v7.widget.RecyclerView>
    </LinearLayout>



</LinearLayout>

猜你喜欢

转载自blog.csdn.net/wrpbk/article/details/80302838