最近在进行毕业设计,想要做一个线性流的资讯页面,在网上找了一下,好像对这块的描写都不太清楚。
在这个demo中我需要实现下拉刷新,上拉加载等。
首先导入github大神的开源库:
GitHub地址:https://github.com/jdsjlzx/LRecyclerView/
Step 1. 在你的根build.gradle文件中增加JitPack仓库依赖。
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io" }
}
}
Step 2. 在你的module的build.gradle文件中增加LRecyclerView依赖。
implementation 'com.github.jdsjlzx:LRecyclerView:1.5.4.3'
在这里我先把布局给安排好:
这个xml页面用来放置一整个LRecyclerView,我打算整个页面都用于资讯页,因此高度用了wrap_content,需要自定义其他功能的可以自己加。
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical">
<com.github.jdsjlzx.recyclerview.LRecyclerView
android:id="@+id/lrv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</com.github.jdsjlzx.recyclerview.LRecyclerView>
</LinearLayout>
接着再新建一个xml文件,在这个页面上,我们要放置你的单个ListView样式
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/color_hint"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="5dp"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_icon"
android:layout_width="160dp"
android:layout_height="120dp"
android:gravity="left"
android:scaleType="fitXY">
</ImageView>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="官宣!首尔马拉松宣布赛事取消 全额退还报名费"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="24sp">
</TextView>
</LinearLayout>
</RelativeLayout>
在这个xml内,你可以自己调节样式的摆放,接下来每个生成都会这个样子。
接下来我们需要添加一个适配器adapter,我们的数据添加与修改都要通过这个适配器。
public class DataAdapter extends LRecyclerView.Adapter<DataAdapter.RecyclerViewHodler> {
private final Context context;
private ArrayList<String> datas;
private Bitmap[] bitmaps;
private ArrayList<String> urls;
public DataAdapter(Context context) {
this.context = context;
}
/**
* 这里找到你的listview
* @param parent
* @p