展示(mvp)

1.首先导入okhttp网络封装依赖

implementation 'com.squareup.okhttp3:okhttp:3.6.0'
    implementation 'com.squareup.okio:okio:1.11.0'

2.然后搭建框架(mvp)

下面开始写view层

package com.example.week16.di;

public interface Iview {

    interface View{
        void showData(String msg);
    }
    interface Ipresenter<IView>{
        void attdata(IView iview);
        void delectData(IView iview);
        void infoData();
    }
    interface Imodel{
        interface oncallback{
            void stringmag(String msg);
        }
        void requesData(oncallback oncallback);
    }
}

3.presenter层

package com.example.week16.di;

import java.lang.ref.WeakReference;

public class Ipresenter implements Iview.Ipresenter<Iview.View>{

    Iview.View view;
    private Iview.Imodel imodel;
    private WeakReference viewweakReference;
    private WeakReference modelweakReference;


    @Override
    public void attdata(Iview.View iview) {
        this.view=iview;
        imodel=new Imodel();
        viewweakReference = new WeakReference(iview);
        modelweakReference = new WeakReference(imodel);
    }

    @Override
    public void delectData(Iview.View iview) {
        viewweakReference.clear();
        modelweakReference.clear();
    }
    @Override
    public void infoData() {
        imodel.requesData(new Iview.Imodel.oncallback() {
            @Override
            public void stringmag(String msg) {
                view.showData(msg);
            }
        });

    }
}

4.model层

package com.example.week16.di;

import com.example.week16.OkhttpUtlis.OkHttpUtlis;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Imodel implements Iview.Imodel{
    private static final String URLGET="http://172.17.8.100/movieApi/cinema/v1/findRecommendCinemas?longitude=116.30551391385724&latitude=40.04571807462411&page=1&count=10&userld=18&sessionld=15320748258726";
    @Override
    public void requesData(final oncallback oncallback) {
        OkHttpUtlis.getinstan().get(URLGET, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String string = e.getMessage().toString();
                oncallback.stringmag(string);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();
                oncallback.stringmag(s);
            }
        });
    }
}

5.MainActivity层

package com.example.week16.ui.activity.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.week16.R;
import com.example.week16.data.User;
import com.example.week16.di.Ipresenter;
import com.example.week16.di.Iview;
import com.example.week16.ui.activity.adapter.MyAdapter;
import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity implements Iview.View {

    private RecyclerView recycler_view;
    private Ipresenter ipresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recycler_view = findViewById(R.id.recycler_view);
        ipresenter = new Ipresenter();
        ipresenter.attdata(this);
        ipresenter.infoData();
    }

    @Override
    public void showData(final String msg) {
        runOnUiThread(new Runnable() {

            private List<User.ResultBean.NearbyCinemaListBean> list;

            @Override
            public void run() {
                Gson gson=new Gson();
                User user = gson.fromJson(msg, User.class);
                list = user.getResult().getNearbyCinemaList();
                LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);
                recycler_view.setLayoutManager(layoutManager);
                MyAdapter adapter=new MyAdapter(list,MainActivity.this);
                recycler_view.setAdapter(adapter);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ipresenter.delectData(this);
    }
}

6.Myadapter层

package com.example.week16.ui.activity.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.week16.R;
import com.example.week16.data.User;
import com.squareup.picasso.Picasso;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHodel> {
    private List<User.ResultBean.NearbyCinemaListBean> list;
    private Context context;

    public MyAdapter(List<User.ResultBean.NearbyCinemaListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.buju1, parent, false);
        ViewHodel hodel = new ViewHodel(view);
        return hodel;
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHodel holder, int position) {
        holder.itme_text.setText(list.get(position).getAddress());
        Picasso.with(context).load(list.get(position).getLogo()).into(holder.itme_img);
    }

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

    class ViewHodel extends RecyclerView.ViewHolder{

        private  ImageView itme_img;
        private  TextView itme_text;

        public ViewHodel(View itemView) {
            super(itemView);
            itme_img = itemView.findViewById(R.id.itme_img);
            itme_text = itemView.findViewById(R.id.itme_text);
        }
    }

}

7.下面是布局。一个主布局。还有一个是myadapter布局

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

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


</LinearLayout>


-- 上面是主布局  --
-- 下面是myadapter --

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/itme_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_background"
        />
    <TextView
        android:id="@+id/itme_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xxxx"
        android:textSize="25sp"
        />
</LinearLayout>

最后运行就可以了。对还有okhttp没有写。到时候自己写一下就可以了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值