Android自带下拉刷新SwipeRefreshLayout用法及添加上拉加载功能

本文介绍如何使用SwipeRefreshLayout实现下拉刷新功能,并通过监听ListView的滚动事件实现上拉加载更多功能。文章提供了完整的代码示例,包括XML布局配置及Activity中的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SwipeRefreshLayoutGooglesupport v4 19.1版本的library更新的一个下拉刷新组件, 实现刷新效果更方便。使用Android官方自带的控件能够保证通用性以及风格。目前市面上很多app的下拉刷新组件就是用的它。但是这个组件只支持下拉刷新,不支持上拉加载更多的操作。
因此,我们来简单的看一下如何使用SwipeRefreshLayout 实现下拉刷新功能,然后我们再来添加上拉加载更多。上拉加载更多主要是通过监听ListView的滚动事件来实现的。

先看一下效果,还是很不错的。。

这里写图片描述

本文只是提供功能实现的示例,并没有对组件进行封装。
整个过程比较简单,直接上代码了。

首先,在xml文件中引用android.support.v4.widget.SwipeRefreshLayout控件。
main.xml

<?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" >
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

配置底部 加载更多布局
footer.xml

<?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" >
    <LinearLayout
        android:id="@+id/footer_load"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="10dip"
        android:paddingTop="10dip" >
        <ProgressBar
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正在加载..." />
    </LinearLayout>
</LinearLayout>

示例ListView的item布局
item.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
            <TextView
                android:layout_centerInParent="true"
                android:id="@+id/item_title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="25sp"
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/item_content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                />
    </LinearLayout>

在Activity中实现
SwipeRefreshAndLoadActivity.java

package com.example.app;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by jorry on 2016/8/8.
 */
public class SwipeRefreshAndLoadActivity extends Activity implements AbsListView.OnScrollListener{
    int totalItemCount;// 总数量;
    int lastVisibleItem;// 最后一个可见的item;
    boolean isLoading;// 正在加载
    View footer;// 底部布局;
    ArrayList<HashMap<String, Object>> listItem;
    SimpleAdapter mSimpleAdapter;
    SwipeRefreshLayout swipeRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LayoutInflater mLi = LayoutInflater.from(this);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
        //设置下拉刷新 小圈圈颜色 ,最多可设置4个颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.dodgerblue, R.color.lawngreen, R.color.colorAccent, R.color.orange);
        ListView lv = (ListView) findViewById(R.id.list);
        //配置底部 加载更多 的布局
        footer = mLi.inflate(R.layout.footer, null);
        footer.findViewById(R.id.footer_load).setVisibility(View.VISIBLE);
        lv.addFooterView(footer, null, false);
        //模拟数据
        listItem = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemTitle", "第" + i + "行");
            map.put("ItemContent", "这是第" + i + "行");
            listItem.add(map);
        }
        //构造适配器
        mSimpleAdapter = new SimpleAdapter(this, listItem,
                R.layout.item, new String[]{"ItemTitle", "ItemContent"},
                new int[]{R.id.item_title, R.id.item_content}
        );
        lv.setAdapter(mSimpleAdapter);
        //设置点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(SwipeRefreshAndLoadActivity.this, listItem.get(i).get("ItemTitle").toString(), Toast.LENGTH_LONG).show();
            }
        });
        //设置下拉刷新监听器
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        doRefreshDatas();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        //设置滚动事件监听, 实现 上拉加载更多
        lv.setOnScrollListener(this);
    }
    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
//        System.out.println("hehe"+isLoading);
        if (totalItemCount == lastVisibleItem&& scrollState == SCROLL_STATE_IDLE) {
            if (!isLoading) {
                isLoading = true;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 加载更多
                        footer.findViewById(R.id.footer_load).setVisibility(View.VISIBLE);
                        doLoadMoreDatas();
                        isLoading = false;
                    }
                }, 2000);

            }
        }
    }
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        this.lastVisibleItem = firstVisibleItem + visibleItemCount;
        this.totalItemCount = totalItemCount;
    }
    //刷新数据, 并通知adapter更新
    public void doRefreshDatas(){
        listItem.clear();
        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemTitle", "第" + i + "行new");
            map.put("ItemContent", "这是第" + i + "行new");
            listItem.add(map);
        }
        mSimpleAdapter.notifyDataSetChanged();
    }
    //加载更多数据,并通知adapter更新
    public void doLoadMoreDatas(){
        int j =listItem.size()+10;
        for (int i =listItem.size(); i<=j; i++){
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemTitle", "第" + i + "行");
            map.put("ItemContent", "这是第" + i + "行");
            listItem.add(map);
        }
        mSimpleAdapter.notifyDataSetChanged();
    }
}

请尊重原创, 转载注明出处 http://blog.youkuaiyun.com/jorrytsai/article/details/52154197

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值