MVP+OKHTTP+RecyclerView

本文介绍了一个安卓应用的具体实现过程,包括依赖配置、Model层的数据获取、View层的UI展示及刷新逻辑、Presenter层的业务逻辑处理,以及Adapter层的数据绑定等关键技术环节。
依赖:

 implementation fileTree(include: ['*.jar'], dir: 'libs')

   implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'

    implementation 'androidx.recyclerview:recyclerview:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.github.bumptech.glide:glide:4.7.1'
    implementation 'com.github.userswlwork:pull-to-refresh:1.0.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.github.asne.facebook:facebook:3.17.2'

Model层:

ModleHome类:

public class ModleHome {
    public static final String TAG="ModelHome";


    public void getData( final ImodleHome ModelListener){


        // 拦截器addInterceptor.addInterceptor(new LogInterceptor())
        OkHttpClient builder = new OkHttpClient.Builder().build();


        final Request request = new Request.Builder().url("https://www.zhaoapi.cn/product/getProducts?pscid=1").build();
        builder.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                ModelListener.callBackFailure(1);
            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.i(TAG, "onResponse: "+result.toString());


                Gson gson=new Gson();
                HomeBean qbean = gson.fromJson(result, HomeBean.class);
                ModelListener.callBackSuccess(qbean);
            }
        });
    }

}

ImodleHome类:

public interface ImodleHome {
    public void callBackSuccess(HomeBean hbean);
    public void callBackFailure(int code);

}

view层

IhomeView类

public interface IhomeView {
    public void callBackSuccess(HomeBean bean);
    public void callBackFailure(int code);


}

Preasenter层

IhomePreasenter类:

public class HomePresenter implements IhomePreasenter {


    private  IhomeView ihomeView;
    private ModleHome modleHome;


    public HomePresenter(IhomeView ihomeView) {
        this.ihomeView=ihomeView;
        this.modleHome=new ModleHome();
    }
    public void getdatas(){
        modleHome.getData(new ImodleHome() {
            @Override
            public void callBackSuccess(HomeBean hbean) {
                ihomeView.callBackSuccess(hbean);
            }


            @Override
            public void callBackFailure(int code) {
                ihomeView.callBackFailure(code);
            }
        });
    }
    @Override
    public void ondestroys() {
        if (ihomeView!=null){
            ihomeView=null;
        }
    }
}

IhomePreasenter类

public interface IhomePreasenter {
    void ondestroys();

}

Adapter层

HomeAdapter类

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.TextHolder>{
    Context  mContext;
    List<HomeBean.DataBean> homeBean;


    //构造函数
    public HomeAdapter(Context mcontext,List<HomeBean.DataBean> homeBean) {
        this.homeBean=homeBean;
        this.mContext=mcontext;


    }
    //创建视图
    @NonNull
    @Override
    public TextHolder  onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取子布局
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.item, parent, false);


        return new TextHolder(mContext,view);
    }
    //绑定数据和视图
    @Override
    public void onBindViewHolder(TextHolder textHolder, int position) {


        String mtext = homeBean.get(position).getTitle();


        String mpath = homeBean.get(position).getImages().split("\\|")[0];


        textHolder.bindText(mtext,mpath);
    }
    //统计条目数量
    @Override
    public int getItemCount() {
        return homeBean.size();
    }




    //通过内部类
    public static class TextHolder extends RecyclerView.ViewHolder{


        private TextView mTextView;
        private ImageView mImageView;
        private Context context;




        public TextHolder(Context mcontext,View itemView) {
            super(itemView);
            this.context=mcontext;


            mTextView = (TextView) itemView.findViewById(R.id.rcv_title);
            mImageView=(ImageView) itemView.findViewById(R.id.rcv_img);


        }
        public void bindText(String mText, String path){
            mTextView.setText(mText);


            Glide.with(context).load(path).into(mImageView);
        }
    }


}

MainActivity

public class MainActivity extends AppCompatActivity implements IhomeView {




    private HomePresenter homePresenter;
    private PullToRefreshScrollView pull_to_refresh;
    private RecyclerView recyclerview;
    private List<HomeBean.DataBean> data;
    private HomeAdapter homeAdapter;


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


    private void init() {
        homePresenter = new HomePresenter(this);
        homePresenter.getdatas();
        pull_to_refresh = findViewById(R.id.pull_to_refresh);
        recyclerview = findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));


        pull_to_refresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            //刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {


                homePresenter.getdatas();
                pull_to_refresh.onRefreshComplete();


            }
            //加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {


                homePresenter.getdatas();
                pull_to_refresh.onRefreshComplete();


            }
        });


    }


    @Override
    public void callBackSuccess(final HomeBean qbean) {
       data = qbean.getData();
       if (data!=null){
           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                    homeAdapter = new HomeAdapter(MainActivity.this, data);
                    recyclerview.setAdapter(homeAdapter);
                   pull_to_refresh.onRefreshComplete();
               }
           });
       }


    }


    @Override
    public void callBackFailure(int code) {


    }

}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">




    <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        android:id="@+id/pull_to_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        </androidx.recyclerview.widget.RecyclerView>




    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>


</LinearLayout>

子布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">




    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10px">


        <ImageView
            android:id="@+id/rcv_img"
            android:layout_width="150px"
            android:layout_height="150px" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10px">
            <TextView
                android:id="@+id/rcv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="每个中秋都不能简单"
                android:padding="10px"/>
            <TextView
                android:id="@+id/rcv_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10px"
                android:text="¥99.99"/>


        </LinearLayout>
    </LinearLayout>


</LinearLayout>












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值