pulltopullrefreshhlistview中有没有方法自动刷新

查看: 2636|回复: 10
android PullToRefreshListView 4.1上不能点击
主题帖子e币
各位兄弟,由于需要使用了PullToRefreshListView,在4.0及其以下的版本上使用都是好的,但是在4.1的系统上使用,发现不能滑动不能点击。有遇到的朋友请指点。谢谢。
主题帖子e币
哦& &你看看点的哪个方法执行了吗
谢谢你的回复,这个论坛现在问题都没有人回了,不像以前了&
主题帖子e币
haoliuyou 发表于
哦& &你看看点的哪个方法执行了吗
谢谢你的回复,这个论坛现在问题都没有人回了,不像以前了
先说说你的问题吧,估计是因为你的问题很多人都没有遇到过吧,所以回复才比较少&
主题帖子e币
xiangyong 发表于
谢谢你的回复,这个论坛现在问题都没有人回了,不像以前了
先说说你的问题吧,估计是因为你的问题很多人都没有遇到过吧,所以回复才比较少&&
主题帖子e币
Android4.1中,PullToRefreshListView点击项目没反应,也不会报错,解决方法就是在 onAttachedToWindow()方法中加入 super.onAttachedToWindow();
[mw_shl_code=java,true]&&@Override
&&protected void onAttachedToWindow() {& &
& & super.onAttachedToWindow();&&
& & setSelection(1);&&
} [/mw_shl_code]
主题帖子e币
正好也在找這個,可以解決的
主题帖子e币
addcn 发表于
Android4.1中,PullToRefreshListView点击项目没反应,也不会报错,解决方法就是在 onAttachedToWindow()方 ...
主题帖子e币
可以直接在这里找嘛
/johannilsson/android-pulltorefresh/commit/aeb5fde61a4d932a066
主题帖子e币
也遇到这个问题,addcn的方法很好用
主题帖子e币
主题帖子e币
还是不太明白, onAttachedToWindow()这个方法应该加在哪里呢?还有里面那个方法是什么?
社区贡献者
eoeAndriod社区贡献网友
QQ已认证,此人靠谱
推荐阅读热门话题
621971795173919071670146014091362129911151092928911664715
26&分钟前26&分钟前28&分钟前半小时前半小时前半小时前半小时前半小时前半小时前半小时前1&小时前
特别关注 /4
从基础入门到熟练精通,9个阶段知识体系图指引你逐步提升~还有技术问答+讲师互动,每天给你新惊喜!
专为零基础定制,从基础到精通,只需3个月轻松搞定!将iOS开发学到极致,月薪过万不是梦!马上开始学习吧!
本期eoe特邀陈晔老师带你学习Android与iOS应用自动化测试内容。大神带你装逼带你飞,有问题赶快回复喽!活动时间:-
eoe双蛋活动开启啦!火热开闹中,按要求参与活动即可获得大奖哦!土豪版机械键盘,珍藏版Android书籍,限量极客学院免费VIP,30元充值卡……更多大奖等你拿!
Powered by
扫一扫 关注eoe官方微信& & & & & & & & & & & & & & & &正在刷新 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & 刷新后
一、导入Library
下载源码后(),里面有个Library工程,添加工程到Eclipse中;
另外extras文件夹还有两个工程:PullToRefreshListFragment和PullToRefreshViewPager,由于我们的这个用不到他们的库文件,所以不必导入了;
1、新建工程,添加Libray库到工程中
新建工程(try_PullToRefresh)后,右键-》Properties-》Android-》Add& 选择上面的Library,然后就是这个样子的
2、重写activity_main.xml
XML内容为:
其中中间那一大段&com.handmark.pull………………/&就是相当于ListView控件,用这段来代替原是ListView控件的代码
3、JAVA代码讲解
全部代码:
代码讲解:
1、变量定义
2、在OnCreate()中主要分为两步
(1)初始化mPullRefreshListView并设置监听器,以执行当需要刷新时,应该怎么办,至于真正执行刷新的类GetDataTask()我们后面再细讲,对应代码为:
(2)设置适配器列表内容,并与ListView绑定以显示出来,对应代码为:
3、执行刷新的类GetDataTask()
先贴出这段代码来:
(1)派生自AsyncTask
由于派生自AsyncTask,所以下面的那个函数doInBackground和onPostExecute就不难理解了,这两个函数是AsyncTask必须是重写的两个函数
(2)doInBackground函数
doInBackground执行要于后台执行的语句,返回的值可以是任意类型,但要提前在extends&AsyncTask&Void, Void, String& 中定义,这个返回值会做为onPostExecute的参数result传到onPostExecute函数中;如果对于网络访问程序,doInBackground就执行访问网络的代码,然后讲返回值存在result中传给onPostExecute函数,以刷新列表;
&(3)onPostExecute函数
&onPostExecute()是对返回的值进行操作,并添加到ListView的列表中,有两种添加方式添加到头部----mListItems.addFirst(result);和添加在尾部----mListItems.addLast(result);
至于&AsyncTask,下面是几个网页,讲的还可以,大家可以参考下:
&《android AsyncTask 详解》:
&《android AsyncTask 详解》(同名):
《android之AsyncTask》:
《Android源码分析--带你认识不一样的AsyncTask》&(绝对精品)
&1、设置向上拉刷新还是向下拉刷新的代码:
&mPullRefreshListView.setMode(Mode.PULL_FROM_END);//向下拉刷新
&mPullRefreshListView.setMode(Mode.PULL_FROM_START);//向上拉刷新
mPullRefreshListView.setMode(Mode.BOTH);//两端刷新
注意:这只是一个精简版,在源码中还有一些可借签的代码,可以在看懂这个以后,可以回过头来再看看源码,我相信肯定会有收获的。
源码来啦:(不要分,仅供分享)
请大家尊重原创者版权,转载请标明出处:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:5867次
排名:千里之外
原创:11篇
转载:104篇
(10)(38)(8)(3)(2)(21)(29)(4)[Android开发][Android widget]开源下拉刷新PullToRefreshExpandableListView在fragment中使用问题
输入搜索关键字,多个条件用空格分开
[Android开发][Android widget]开源下拉刷新PullToRefreshExpandableListView在fragment中使用问题
更新: 12:55:45
一个下拉刷新的开源项目Android-PullToRefresh-master,用过的应该知道。现在遇到一个问题就是,当在fragment中使用PullToRefreshExpandableListView的时候,mListView.setAdapter(mAdapter)报错。不知道该如何解决,求高手指点!
不知道你错的什么信息,直接贴出错误信息。
是报的空指针的错呢,还是什么错,如果是空指针的话检查一下listview是否初始化,adapter是否初始化
贴LogCat出来,我们才知道是什么错了。不过看你那么久没有回复也许是你找到错误了。顶个贴,刷刷积分。是报的空指针的错呢,还是什么错,如果是空指针的话检查一下listview是否初始化,adapter是否初始化
查看原网页[0]&
举报本内容
&&网站联系: qq: email:&
Android开发|Android教程无法找到该页
无法找到该页
您正在搜索的页面可能已经删除、更名或暂时不可用。
请尝试以下操作:
确保浏览器的地址栏中显示的网站地址的拼写和格式正确无误。
如果通过单击链接而到达了该网页,请与网站管理员联系,通知他们该链接的格式不正确。
单击按钮尝试另一个链接。
HTTP 错误 404 - 文件或目录未找到。Internet 信息服务 (IIS)
技术信息(为技术支持人员提供)
转到 并搜索包括&HTTP&和&404&的标题。
打开&IIS 帮助&(可在 IIS 管理器 (inetmgr) 中访问),然后搜索标题为&网站设置&、&常规管理任务&和&关于自定义错误消息&的主题。开源项地址:/chrisbanes/Android-PullToRefresh
下拉刷新这个功能我们都比较常见了,今天介绍的就是这个功能的实现。我将按照这个开源库的范例来一点一点介绍,今天是介绍比较常见的PullToRefreshListView,是让listView有下拉刷新功能。
1.下载项目包,将library包导入即可,其他的包暂时不用
2.分析源码,看我们可以设置的有哪些
&?xml version="1.0" encoding="utf-8"?&
&resources&
&declare-styleable name="PullToRefresh"&
&!-- A drawable to use as the background of the Refreshable View --&
&!-- 设置刷新view的背景 --&
&attr name="ptrRefreshableViewBackground" format="reference|color" /&
&!-- A drawable to use as the background of the Header and Footer Loading Views --&
&!-- 设置头部view的背景 --&
&attr name="ptrHeaderBackground" format="reference|color" /&
&!-- Text Color of the Header and Footer Loading Views --&
&!-- 设置头部/底部文字的颜色 --&
&attr name="ptrHeaderTextColor" format="reference|color" /&
&!-- Text Color of the Header and Footer Loading Views Sub Header --&
&!-- 设置头部/底部副标题的文字颜色 --&
&attr name="ptrHeaderSubTextColor" format="reference|color" /&
&!-- Mode of Pull-to-Refresh that should be used --&
&!-- 设置下拉刷新的模式,有多重方式可选。无刷新功能,从顶部刷新,从底部刷新,二者都有,只允许手动刷新 --&
&attr name="ptrMode"&
&flag name="disabled" value="0x0" /&
&flag name="pullFromStart" value="0x1" /&
&flag name="pullFromEnd" value="0x2" /&
&flag name="both" value="0x3" /&
&flag name="manualOnly" value="0x4" /&
&!-- These last two are depreacted --&
&!-- 这两个属性不推荐了,用上面的代替即可 --&
&flag name="pullDownFromTop" value="0x1" /&
&flag name="pullUpFromBottom" value="0x2" /&
&!-- Whether the Indicator overlay(s) should be used --&
&!-- 是否显示指示箭头 --&
&attr name="ptrShowIndicator" format="reference|boolean" /&
&!-- Drawable to use as Loading Indicator. Changes both Header and Footer. --&
&!-- 指示箭头的图片 --&
&attr name="ptrDrawable" format="reference" /&
&!-- Drawable to use as Loading Indicator in the Header View. Overrides value set in ptrDrawable. --&
&!-- 顶部指示箭头的图片,设置后会覆盖ptrDrawable中顶部的设置 --&
&attr name="ptrDrawableStart" format="reference" /&
&!-- Drawable to use as Loading Indicator in the Fooer View. Overrides value set in ptrDrawable. --&
&!-- 底部指示箭头的图片,设置后会覆盖ptrDrawable中底部的设置 --&
&attr name="ptrDrawableEnd" format="reference" /&
&!-- Whether Android's built-in Over Scroll should be utilised for Pull-to-Refresh. --&
&attr name="ptrOverScroll" format="reference|boolean" /&
&!-- Base text color, typeface, size, and style for Header and Footer Loading Views --&
&!-- 设置文字的基本字体 --&
&attr name="ptrHeaderTextAppearance" format="reference" /&
&!-- Base text color, typeface, size, and style for Header and Footer Loading Views Sub Header --&
&!-- 设置副标题的基本字体 --&
&attr name="ptrSubHeaderTextAppearance" format="reference" /&
&!-- Style of Animation should be used displayed when pulling. --&
&!-- 设置下拉时标识图的动画,默认为rotate --&
&attr name="ptrAnimationStyle"&
&flag name="rotate" value="0x0" /&
&flag name="flip" value="0x1" /&
&!-- Whether the user can scroll while the View is Refreshing --&
&!-- 设置刷新时是否允许滚动,一般为true --&
&attr name="ptrScrollingWhileRefreshingEnabled" format="reference|boolean" /&
Whether PullToRefreshListView has it's extras enabled. This allows the user to be
able to scroll while refreshing, and behaves better. It acheives this by adding
Header and/or Footer Views to the ListView.
&!-- 允许在listview中添加头/尾视图 --&
&attr name="ptrListViewExtrasEnabled" format="reference|boolean" /&
Whether the Drawable should be continually rotated as you pull. This only
takes effect when using the 'Rotate' Animation Style.
&!-- 当设置rotate时,可以用这个来设置刷新时旋转的图片 --&
&attr name="ptrRotateDrawableWhilePulling" format="reference|boolean" /&
&!-- BELOW HERE ARE DEPRECEATED. DO NOT USE. --&
&attr name="ptrAdapterViewBackground" format="reference|color" /&
&attr name="ptrDrawableTop" format="reference" /&
&attr name="ptrDrawableBottom" format="reference" /&
&/declare-styleable&
&/resources&
看到有这么多可以设置的属性,别以为真的就可以定制了。真正要定制还得到layout中改变刷新布局
3.开始用它建立自己的工程
设置布局文件
就是插入PullToRefreshListView
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:background="#000000"&
The PullToRefreshListView replaces a standard ListView widget. --&
&com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="/apk/res-auto"
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000000"
android:divider="#"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrAnimationStyle="rotate"
ptr:ptrHeaderTextColor="#ffffff"
ptr:ptrHeaderSubTextColor="#00ffff"
ptr:ptrHeaderBackground="@null"
ptr:ptrDrawable="@drawable/ic_launcher"/&
&/RelativeLayout&
开始编写代码
1.找到这个控件,并且设置监听器
这里面用到了一个日期的工具类,其实就是设置上次下拉的时间的。此外在下拉后会触发一个异步任务
* 设置下拉刷新的listview的动作
private void initPTRListView() {
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
//设置拉动监听器
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener&ListView&() {
public void onRefresh(PullToRefreshBase&ListView& refreshView) {
//设置下拉时显示的日期和时间
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// 更新显示的label
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// 开始执行异步任务,传入适配器来进行数据改变
new GetDataTask(mPullRefreshListView, mAdapter,mListItems).execute();
// 添加滑动到底部的监听器
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
public void onLastItemVisible() {
Toast.makeText(getApplication(), "已经到底了", Toast.LENGTH_SHORT).show();
//mPullRefreshListView.isScrollingWhileRefreshingEnabled();//看刷新时是否允许滑动
//在刷新时允许继续滑动
mPullRefreshListView.setScrollingWhileRefreshingEnabled(true);
//mPullRefreshListView.getMode();//得到模式
//上下都可以刷新的模式。这里有两个选择:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END
mPullRefreshListView.setMode(Mode.BOTH);
* 设置反馈音效
SoundPullEventListener&ListView& soundListener = new SoundPullEventListener&ListView&(this);
soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
mPullRefreshListView.setOnPullEventListener(soundListener);
2.从上面的那个控件中,得到它包含的listView,并且设置适配器
//普通的listview对象
private ListView actualListV
//添加一个链表数组,来存放string数组,这样就可以动态增加string数组中的内容了
private LinkedList&String& mListI
//给listview添加一个普通的适配器
private ArrayAdapter&String& mA
这里用到了一个LinkedList的对象,这个是一个类似于ArrayList的链表数组,比较方便在开头和末尾添加String
* 设置listview的适配器
private void initListView() {
//通过getRefreshableView()来得到一个listview对象
actualListView = mPullRefreshListView.getRefreshableView();
String []data = new String[] {"android","ios","wp","java","c++","c#"};
mListItems = new LinkedList&String&();
//把string数组中的string添加到链表中
mListItems.addAll(Arrays.asList(data));
mAdapter = new ArrayAdapter&&(getApplicationContext(),
android.R.layout.simple_list_item_1, mListItems);
actualListView.setAdapter(mAdapter);
3.写一个异步任务,来模仿从网络加载数据
这里要注意的是,加载完后要出发刷新完成和通知适配器改变的方法
package com.kale.
import java.util.LinkedL
import android.os.AsyncT
import android.widget.ArrayA
import com.handmark.pulltorefresh.library.PullToRefreshListV
import com.handmark.pulltorefresh.library.PullToRefreshBase.M
* @author:Jack Tony
:通过异步任务来加载网络中的数据,进行更新
public class GetDataTask extends AsyncTask&Void, Void, Void&{
private PullToRefreshListView mPullRefreshListV
private ArrayAdapter&String& mA
private LinkedList&String& mListI
public GetDataTask(PullToRefreshListView listView,
ArrayAdapter&String& adapter,LinkedList&String& listItems) {
// TODO 自动生成的构造函数存根
mPullRefreshListView = listV
mAdapter =
mListItems = listI
protected Void doInBackground(Void... params) {
//模拟请求
Thread.sleep(2000);
} catch (InterruptedException e) {
return null;
protected void onPostExecute(Void result) {
// TODO 自动生成的方法存根
super.onPostExecute(result);
//得到当前的模式
Mode mode = mPullRefreshListView.getCurrentMode();
if(mode == Mode.PULL_FROM_START) {
mListItems.addFirst("这是刷新出来的数据");
mListItems.addLast("这是刷新出来的数据");
// 通知数据改变了
mAdapter.notifyDataSetChanged();
// 加载完成后停止刷新
mPullRefreshListView.onRefreshComplete();
贴上acitivty中的全部代码
MainActivity.java
package com.kale.
import java.util.A
import java.util.LinkedL
import android.app.A
import android.os.B
import android.text.format.DateU
import android.widget.ArrayA
import android.widget.ListV
import android.widget.T
import com.handmark.pulltorefresh.library.PullToRefreshB
import com.handmark.pulltorefresh.library.PullToRefreshBase.M
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleL
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshL
import com.handmark.pulltorefresh.library.PullToRefreshBase.S
import com.handmark.pulltorefresh.library.PullToRefreshListV
import com.handmark.pulltorefresh.library.extras.SoundPullEventL
public class MainActivity extends Activity {
//一个可以下拉刷新的listView对象
private PullToRefreshListView mPullRefreshListV
//普通的listview对象
private ListView actualListV
//添加一个链表数组,来存放string数组,这样就可以动态增加string数组中的内容了
private LinkedList&String& mListI
//给listview添加一个普通的适配器
private ArrayAdapter&String& mA
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//一打开应用就自动刷新,下面语句可以写到刷新按钮里面
mPullRefreshListView.setRefreshing(true);
//new GetDataTask(mPullRefreshListView, mAdapter, mListItems).execute();
//mPullRefreshListView.setRefreshing(false);
private void initView() {
initPTRListView();
initListView();
* 设置下拉刷新的listview的动作
private void initPTRListView() {
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
//设置拉动监听器
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener&ListView&() {
public void onRefresh(PullToRefreshBase&ListView& refreshView) {
//设置下拉时显示的日期和时间
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// 更新显示的label
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// 开始执行异步任务,传入适配器来进行数据改变
new GetDataTask(mPullRefreshListView, mAdapter,mListItems).execute();
// 添加滑动到底部的监听器
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
public void onLastItemVisible() {
Toast.makeText(getApplication(), "已经到底了", Toast.LENGTH_SHORT).show();
//mPullRefreshListView.isScrollingWhileRefreshingEnabled();//看刷新时是否允许滑动
//在刷新时允许继续滑动
mPullRefreshListView.setScrollingWhileRefreshingEnabled(true);
//mPullRefreshListView.getMode();//得到模式
//上下都可以刷新的模式。这里有两个选择:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END
mPullRefreshListView.setMode(Mode.BOTH);
* 设置反馈音效
SoundPullEventListener&ListView& soundListener = new SoundPullEventListener&ListView&(this);
soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
mPullRefreshListView.setOnPullEventListener(soundListener);
* 设置listview的适配器
private void initListView() {
//通过getRefreshableView()来得到一个listview对象
actualListView = mPullRefreshListView.getRefreshableView();
String []data = new String[] {"android","ios","wp","java","c++","c#"};
mListItems = new LinkedList&String&();
//把string数组中的string添加到链表中
mListItems.addAll(Arrays.asList(data));
mAdapter = new ArrayAdapter&&(getApplicationContext(),
android.R.layout.simple_list_item_1, mListItems);
actualListView.setAdapter(mAdapter);
源码下载:
阅读(...) 评论()

我要回帖

更多关于 pulltorefreshbase 的文章

 

随机推荐