Kotlin_Recyclerview_Rxjava的封装
来源:互联网 发布:ui设计软件下载 编辑:程序博客网 时间:2024/06/09 22:42
View层
interface ViewIn { fun Succers(bn: bean) fun Filde(e: Exception)}Modle层接口
interface Modle { fun Succers(bn: bean) fun Filde(e: Exception)}Modle层实体类
class Modles { fun getdata(modle: Modle) { var ok = OkHttpClient.Builder() RetrofitUnitl.getInstance("http://japi.juhe.cn", ok.build()) .setCreate<ViewObj>(ViewObj::class.java) .getdata() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe { next -> modle.Succers(next!!) } }}Presenter层
class presenter { lateinit var wr :WeakReference<ViewIn> internal lateinit var md :Modles constructor(wr: ViewIn) { datach(wr as MainActivity) this.md = Modles() } fun xinyuayn(){ md.getdata(object :Modle{ override fun Filde(e: Exception) { wr.get()!!.Filde(e) } override fun Succers(bn: bean) { wr.get()!!.Succers(bn) } }) } // 绑定 内存泄漏 fun datach(view: MainActivity) { wr = WeakReference(view) } //解绑 fun data() { wr.clear() }}App
class App : Application() { override fun onCreate() { super.onCreate() Fresco.initialize(this) }}网络请求接口
interface ViewObj { @GET("/comic/book?key=f54c4c57143b8fad9bf3193cab52a81c") fun getdata() : Observable<bean>}自己封装的Rxjava+retrofit
public class RetrofitUnitl { private Retrofit mRetrofit; private String baseUrl; OkHttpClient client; private static RetrofitUnitl mRetrofitManager; private RetrofitUnitl(String baseUrl, OkHttpClient client){ this.baseUrl=baseUrl; this.client=client; initRetrofit(); } public static synchronized RetrofitUnitl getInstance(String baseUrl, OkHttpClient client){ if (mRetrofitManager == null){ mRetrofitManager = new RetrofitUnitl(baseUrl,client); } return mRetrofitManager; } private void initRetrofit() { mRetrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build(); } public <T> T setCreate(Class<T> reqServer) { return mRetrofit.create(reqServer); }}适配器
class KolintApdata (context1: Context) : RecyclerView.Adapter<KolintApdata.IViewHodler>() { var context :Context=context1 var list : ArrayList<Book> = ArrayList() fun addData(bn : bean) { list.addAll(bn.result.bookList) notifyDataSetChanged() } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): IViewHodler {// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. var view = LayoutInflater.from(context).inflate(R.layout.rvlayout,parent,false) return IViewHodler(view) } override fun onBindViewHolder(holder: IViewHodler?, position: Int) {// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. holder!!.item_textview.setText(list.get(position).name) Glide.with(context).load(list.get(position).coverImg).into(holder!!.item_imageview) } override fun getItemCount(): Int {// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. return list.size } class IViewHodler(view : View) : RecyclerView.ViewHolder(view) { var item_imageview : ImageView var item_textview : TextView init { item_imageview = view.findViewById(R.id.iv) item_textview = view.findViewById(R.id.tv) } }}适配器布局
<?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/iv" android:layout_width="150dp" android:layout_height="150dp"> </ImageView> <TextView android:id="@+id/tv" android:layout_width="50dp" android:layout_height="50dp" /></LinearLayout>主Activity
class MainActivity : AppCompatActivity(),ViewIn { lateinit var kn : KolintApdata private var rv_main : RecyclerView? = null; lateinit var pt : presenter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) rv_main = findViewById(R.id.rv_main); pt = presenter(this) pt.xinyuayn() } override fun Succers(bn: bean) { val manager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) kn = KolintApdata(this) rv_main!!.setLayoutManager(manager ); rv_main!!.setAdapter(kn) kn.addData(bn) } override fun Filde(e: Exception) { } //内存泄漏 override fun onDestroy() { super.onDestroy() //解绑 pt.data() }}主Activity布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context="com.example.kotlinrecyclerview.MainActivity"><android.support.v7.widget.RecyclerView android:id="@+id/rv_main" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView></LinearLayout>
用到的依赖
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 'io.reactivex.rxjava2:rxandroid:2.0.1'compile 'io.reactivex.rxjava2:rxjava:2.1.7'compile 'io.reactivex:rxjava:1.1.1'compile 'io.reactivex:rxandroid:1.1.0'compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'compile 'com.facebook.fresco:fresco:1.5.0'compile 'com.squareup.retrofit2:converter-gson:2.3.0'implementation 'com.github.bumptech.glide:glide:4.4.0'implementation 'com.android.support:support-v4:26.1.0'
阅读全文
0 0
- Kotlin_Recyclerview_Rxjava的封装
- 封装机制封装的概念
- 封装:树形节点的封装
- 简单的jdbc封装+servlet的封装
- 工具类的封装-sharePreference的封装
- 封装性是指对外部的封装
- 强大的封装标签封装类
- 什么是封装,封装类的使用!
- java的封装(如何封装类)
- alertController的封装(在TYAlertController基础上封装)
- 工具类的封装--Log封装
- 工具类的封装--SharedPreferences封装
- 封装尺寸与封装的对应关系
- 什么是javascript封装,封装的方法有几种
- 关于封装的设计
- 函数的封装解释
- Java的封装类
- JavaScript封装的HashMap
- Python-数组
- TabLayout中的Tab间隔设置方法
- 滑动窗:Subsequence
- 日志系列之Log4j2 Garbage-free模式
- [USACO3.1]最短网络 Agri-Net kruskal
- Kotlin_Recyclerview_Rxjava的封装
- android编译中的BUILD_JAVA_LIBRARY
- CentOS6.5系统"libc.so.6: version 'GLIBC_2.15' not found"解决方法
- 智能合约语言 Solidity 教程系列3
- numpy.log
- POJ 3281 Dining(转换+最大流)
- Sublime text 3 打造Java编译环境
- 通过dockerfile 创建镜像以及更新镜像
- 查看python下OpenCV版本