EvenBus

用到的依赖

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okio:okio:1.13.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.facebook.fresco:fresco:1.5.0'
App图片框架的初始化

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}
View层

public interface EvMainView {
    void Suuer(EvBean bean);//成功
    void Defeat(Exception e);//失败
}
Modle层的接口

public interface EvMainModleInterface {
    void Suuer(EvBean bean);//成功
    void Defeat(Exception e);//失败
}
Modle层的实体类

public class EvMainModle {
    //获取数据传入 modle接口
    public  void getData(final EvMainModleInterface evMainModleInterface){
        OkhttpUtils.getInstance().asy(null, "http://v.juhe.cn/weixin/query?key=88f7bbc507e3ecacfaeab2b47dd8936f", new AbstractUiCallBack<EvBean>() {

            @Override
            public void success(EvBean bean) {
                evMainModleInterface.Suuer(bean);

            }

            @Override
            public void failure(Exception e) {
                evMainModleInterface.Defeat(e);
            }
        });
    }

}
Prenter层

public class EvPresenter {
    EvMainModle em;//modle类
    EvMainView ev;//View接口

    public EvPresenter(EvMainView ev) {
        this.ev = ev;
        this.em = new EvMainModle();
    }
    //获取参数
    public  void  getData(){

        em.getData(new EvMainModleInterface() {
            @Override
            public void Suuer(EvBean bean) {
                if (ev!=null){
                    ev.Suuer(bean);
                }
            }

            @Override
            public void Defeat(Exception e) {
                if(ev ==null ){
                    ev.Defeat(e);
                }
            }
        });
    }
    //防止内存泄漏
    public  void datch(){
        ev =null;
    }
}
适配器

public class EvApdata extends RecyclerView.Adapter<EvApdata.ViewHolder> {
    //传入上下文
    Context context;
     List<EvBean.ResultBean.ListBean> listDa;
        //有参构造
    public EvApdata(Context context) {
        this.context = context;

    }
        //这里是添加数据  更新适配器
    public void addData(List<EvBean.ResultBean.ListBean> list) {

        if(listDa==null){
            listDa=new ArrayList<>();
        }
       listDa.addAll(list);
        notifyDataSetChanged();
    }
    @Override
    public EvApdata.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  view = View.inflate(context,R.layout.rcy_main,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(EvApdata.ViewHolder holder, int position) {
        holder.tv1.setText(listDa.get(position).getTitle());
        holder.tv2.setText(listDa.get(position).getTitle());
        Uri uri =  Uri.parse(listDa.get(position).getFirstImg());
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setAutoPlayAnimations(true)
                .build();
        holder.simpdv.setController(controller);
    }

    @Override
    public int getItemCount(){
        return listDa==null?0:listDa.size();
    }



    public class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView simpdv;
        TextView tv1,tv2;
        public ViewHolder(View itemView) {
            super(itemView);
            simpdv = itemView.findViewById(R.id.simpdv);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
        }
    }
    public UpdateUiListener listener;

    public void setListener(UpdateUiListener listener) {
        this.listener = listener;
    }

    //更新接口数据
    public interface UpdateUiListener {
        public void setTotal(String total, String num, boolean allCheck);
    }
}
主方法

public class MainActivity extends AppCompatActivity implements EvMainView {
    EvApdata evApdata;
    EvPresenter evPresenter;
    EvBean evBean;
    @BindView(R.id.rcv)
    RecyclerView rcv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        rcv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        evPresenter = new EvPresenter(this);
        evPresenter.getData();
        evApdata = new EvApdata(this);
    }

    @Override
    public void Suuer(EvBean bean) {
        //这里是调用适配器的添加方法 来获取请求到的数据
        evApdata.addData(bean.getResult().getList());
        rcv.setAdapter(evApdata);
    }

    @Override
    public void Defeat(Exception e) {

    }
    //防止内存泄漏
    @Override
    protected void onDestroy() {
        super.onDestroy();
        evPresenter.datch();
    }
}
布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.eventbus.MainActivity">

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

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

</LinearLayout>
适配器布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:fresco="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/simpdv"
        android:layout_width="150dp"
        android:layout_height="150dp"
        fresco:roundAsCircle="false"
        fresco:roundedCornerRadius="100dp"
        fresco:roundTopLeft="true"
        fresco:roundTopRight="true"
        fresco:roundBottomLeft="true"
        fresco:roundBottomRight="true"
        fresco:roundingBorderWidth="2dp"
        fresco:placeholderImage="@mipmap/ic_launher"
        />
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="标题标题标题"
            android:textSize="43dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            />
        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2017-10-11"
            android:layout_marginLeft="20dp"
            android:textSize="25dp"
            />

    </LinearLayout>
</LinearLayout>
这里用的是okHttp请求的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值