EvenBus
来源:互联网 发布:淘宝怎样申请退货退款 编辑:程序博客网 时间:2024/06/16 05:55
用到的依赖
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请求的
阅读全文
0 0
- evenbus
- EvenBus
- EvenBus
- android EvenBus
- EvenBus使用方法
- EvenBus初学
- EvenBus的入门
- EvenBus 避免跳进大坑
- Evenbus 3.0 分析
- evenbus的简单使用方法
- 使用RxBus替换EvenBus
- EvenBus简单使用
- EvenBus的使用,(详解)
- Evenbus普通事件,粘性事件
- EvenBus:Android应用组件之间高效通信的开发库
- Evenbus个人的一点小见解及简单用法
- EvenBus No subscribers registered for event class 发送消息失败
- Android TextInputLayout更好看的输入框加EvenBus传值
- RecyclerView的使用
- Doing Homework (状态dp)
- Win10升级后VMWare内的系统连不了网如何解决
- Python安装第三方模块
- 11月27号 侧边
- EvenBus
- Did you smile today?
- 图像几何运算
- jQuery插件开发精品教程,让你的jQuery提升一个台阶
- MySQL5.7 InnoDB Page Compression
- javascript基础练习-数字时钟
- Could not resolve all dependencies for configuration ':classpath'. Could not find com.android.t
- [莫比乌斯反演+数位DP]计蒜客 阿里云秘钥池
- 如何理解jsp页面中的“<base href="<%=basePath%>">”?