Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

是不是分分钟实现下拉刷新。当然了,你可能会有疑问,下拉刷新的指示器上的文字可以自定义吗?那个图片可以换成箭头吗?说好的上拉加载更多呢?后面会一一添加~

2、添加上拉加载更多

如过希望实现上拉加载更多,那么首先需要在布局文件的声明属性中添加一个属性,用于指定目前的下拉模式:

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<com.handmark.pulltorefresh.library.PullToRefreshListView

xmlns:ptr=“http://schemas.android.com/apk/res-auto”

android:id=“@+id/pull_refresh_list”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:cacheColorHint=“#00000000”

android:divider=“#19000000”

android:dividerHeight=“4dp”

android:fadingEdge=“none”

android:fastScrollEnabled=“false”

android:footerDividersEnabled=“false”

android:headerDividersEnabled=“false”

android:smoothScrollbar=“true”

ptr:ptrMode=“both” >

</com.handmark.pulltorefresh.library.PullToRefreshListView>

我们添加了一个属性:ptr:ptrMode=“both” ,意思:上拉和下拉都支持。

可选值为:disabled(禁用下拉刷新),pullFromStart(仅支持下拉刷新),pullFromEnd(仅支持上拉刷新),both(二者都支持),manualOnly(只允许手动触发)

当然了,如果你不喜欢在布局文件中指定,完全可以使用代码设置,在onCreate里面写:mPullRefreshListView.setMode(Mode.BOTH);//设置你需要的模式

设置了模式为双向都支持,当然必须为上拉和下拉分别设置回调,请看下面的代码:

package com.example.zhy_pulltorefreash_chenyoca;

import java.util.LinkedList;

import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.text.format.DateUtils;

import android.util.Log;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;

import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;

import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;

import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class PullToRefreshListActivity extends Activity

{

private LinkedList mListItems;

/**

  • 上拉刷新的控件

*/

private PullToRefreshListView mPullRefreshListView;

private ArrayAdapter mAdapter;

private int mItemCount = 9;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 得到控件

mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);

mPullRefreshListView.setMode(Mode.BOTH);

// 初始化数据

initDatas();

// 设置适配器

mAdapter = new ArrayAdapter(this,

android.R.layout.simple_list_item_1, mListItems);

mPullRefreshListView.setAdapter(mAdapter);

mPullRefreshListView

.setOnRefreshListener(new OnRefreshListener2()

{

@Override

public void onPullDownToRefresh(

  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值