Android RecyclerView的使用及下拉刷新和上拉加载更多

本文介绍了Android中RecyclerView的使用,包括如何配置、创建简单布局、实现下拉刷新和上拉加载更多功能。通过自定义分割线和适配器,详细阐述了RecyclerView在实际应用中的实现步骤,并提供了效果图。最后提到了避免使用过时方法的建议,鼓励开发者保持代码的最新性。
摘要由CSDN通过智能技术生成

一. 简介

       RecyclerView 是android5.0提出的代替ListView的新控件,还可以实现GridView的效果,自带分割线,也可以自定义分割线,增加List显示的美观性,而新增的LayoutManager可用来确定item的排列方式,可以通过LayoutManager来设置list要展示的是垂直还是水平,还添加了默认的增加和删除item动画。

二. 配置

在model下添加:

    compile 'com.android.support:recyclerview-v7:21.0.0'

三. RecyclerView的使用和下拉刷新,上拉加载更多

(1)简单布局

activity_layout.xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

注:在布局中SwipeRefreshLayout是下拉加载的控件,需要包裹RecyclerView.

recycler_layout.xml list的布局:

<?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="45dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_recycler"
        android:gravity="center"
        android:background="#0f0"
        android:textColor="#f00"
        android:textSize="20dp"
        android:layout_margin="3dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

footview_layout.xml上拉加载更多的foot布局:

<?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="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/foot"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:textSize="15sp"
        android:background="#fff"
        android:layout_gravity="center"
        android:layout_marginBottom="1dp"/>

</LinearLayout>

注:列表布局我只显示了一个TextView,简单明了。需要注意的是父控件的高度是wrap_content,这样当TextView隐藏的时候,LinnearLayout也就跟着隐藏了,从而达到foot的整体隐藏。

(2)RecyclerView适配器

都知道写ListView展示数据都需要适配器,RecyclerView也不例外

RecyclerViewAdapter.java : 

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {

    List<String> list;    //数据集合
    Context context;

    public RecyclerViewAdapter(List<String> list, Context context){
        this.list = list;
        this.context = context;
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值