之前一直觉得ListView好用,但是好多情况不太适用。而RecycleView比较方便。然后今天就说这个吧。下面是要实现的效果。
首先,先加入需要的依赖,每个依赖的作用已经在代码中标明。
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) //noinspection GradleCompatible implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' //安卓Utils implementation 'com.blankj:utilcode:1.21.2' //上拉刷新,下拉加载 implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1' //万能适配器 implementation 'com.zhy:base-rvadapter:3.0.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
第二步开始画布局。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/two_listViews" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" /> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </LinearLayout>
第三步开始码代码。
首先为了方便在Application中先创建一个静态的SmartRefreshLayout代码,注:这个级别最低,还可以在代码中或者布局中设置。下面是application的代码。
/** * Created by Jiang on 2019/2/2 */ public class App extends Application { //static 代码段可以防止内存泄露 static { //设置全局的Header构建器 SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> { layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色 return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header,默认是 贝塞尔雷达Header }); //设置全局的Footer构建器 SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> { //指定为经典Footer,默认是 BallPulseFooter return new ClassicsFooter(context).setDrawableSize(20); }); } }
好了,准备工作做完就开始写点代码吧,由于代码比较简单,在代码中也有注释,不懂得可以留言。
public class MainActivity extends AppCompatActivity { private SmartRefreshLayout refreshLayout; private RecyclerView two_listViews; private CommonAdapter recycleAdapter; //初始化所需集合 private List<String> stringList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化view initView(); } /** * 初始化控件 */ private void initView() { refreshLayout = findViewById(R.id.refreshLayout); two_listViews = findViewById(R.id.two_listViews); //设置数据 setData(); //设置ReCycleView setReCycleView(); } /** * 设置数据 */ private void setData() { if (stringList.size() == 0) { for (int i = 1; i < 21; i++) { stringList.add("第" + i + "条数据"); } } } /** * 设置ReCycleView */ private void setReCycleView() { //设置ReCycleView LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); two_listViews.setLayoutManager(linearLayoutManager); //设置ReCycleView所需的adapter setAdapter(stringList); two_listViews.setAdapter(recycleAdapter); setRefresh(); } /** * 设置适配器 * * @param onlines 传入的数据 */ private void setAdapter(List<String> onlines) { recycleAdapter = new CommonAdapter<String>(this, R.layout.activity_list, onlines) { @SuppressLint("SetTextI18n") @Override protected void convert(ViewHolder holder, final String items, int position) { TextView one = holder.getView(R.id.textView); LinearLayout linearLayout = holder.getView(R.id.linear); //设置数据 one.setText(items); //设置条目点击事件 linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtils.showShort("点击了" + items); } }); } }; } /** * 下拉刷新,上拉加载 */ private void setRefresh() { refreshLayout.setOnRefreshListener(new OnRefreshListener() { @RequiresApi(api = Build.VERSION_CODES.N) @Override public void onRefresh(RefreshLayout refreshLayout) { //设置刷新时间 //方法运行前记录当前时间戳 Long start = System.currentTimeMillis(); stringList.add(0, "下拉刷新"); recycleAdapter.notifyDataSetChanged(); //方法结束后记录当前时间戳减去刚才记录的时间戳就是刷新所需时间 //下面的上拉加载意思同这个 Long stop = System.currentTimeMillis() - start; refreshLayout.finishRefresh(stop.intValue());//传入false表示加载失败 } }); refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { @RequiresApi(api = Build.VERSION_CODES.N) @Override public void onLoadMore(RefreshLayout refreshlayout) { stringList.add("上拉加载"); recycleAdapter.notifyDataSetChanged(); refreshlayout.finishLoadMore(1000);//传入false表示加载失败 } }); } }