本人第三篇个人博客,还是Android方向的技术,retrofit我们可以简单理解
为网络加载框架,底层呢还是基于okhttp,对于新手来说,可以减少代码
量,更方便使用,而且还支持和RXJava一起使用。然后就是使用 Recyclerview
列表去展示我们所获取的数据
先看一眼效果:
那我们闲话少说,直接上代码了。
首先我们要在相对应的build.gradle文件中导入相应的依赖
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
//picasso是图片加载框架,也是简单的使用,有兴趣的小伙伴也可以看看
然后在清单文件中加入网络框架
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
接下来是布局
主布局
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
然后Recyclerview中的每一个item项的子布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="