时间:2021-05-19
首先为大家介绍Andorid5.0原生下拉刷新简单实现。
先上效果图;
相对于上一个19.1.0版本中的横条效果好看了很多。使用起来也很简单。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="MergeRootFrame" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </android.support.v4.widget.SwipeRefreshLayout></FrameLayout>package hi.xiaoyu.swiperefreshlayout;import hi.xiaoyu.swiperefreshlayout.adapter.TestAdapter;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;import android.widget.ListView;public class MainActivity extends Activity implements OnRefreshListener { private SwipeRefreshLayout swipeLayout; private ListView listView; private List<String> listDatas; private TestAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container); listView = (ListView) findViewById(R.id.list); swipeLayout.setOnRefreshListener(this); swipeLayout.setColorSchemeResources(android.R.color.holo_orange_dark, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); listDatas = new ArrayList<String>(); for (int i = 0; i < 10; i++) { listDatas.add("item" + i); } adapter = new TestAdapter(this, listDatas, R.layout.test_item); listView.setAdapter(adapter); } public void onRefresh() { new Handler().postDelayed(new Runnable() { public void run() { swipeLayout.setRefreshing(false); listDatas.addAll(listDatas); adapter.notifyDataSetChanged(); } }, 3000); }}几行代码就可以实现下拉刷新,效果也还不错,不用引入第三方jar,唯一的缺憾就是没有上拉加载,不知道谷歌工程师基于什么方面的考虑,希望能在下个版本看到。不过自己修改下源码加一个上拉也比较简单,结合上个一个版本的刷新效果做成上拉效果还不错。
二、Android实现上拉加载更多功能以及下拉刷新功能
采用了目前比较火的PullToRefresh,他是目前实现比较好的下拉刷新的类库。
目前他支持的控件有:ListView, ExpandableListView,GridView,WebView等。
首先第一步当然是导入libriay到咱们的项目了,具体导入方式,这里不再赘述。
下面是个例子采用的是ListView,当然其余的和这个类似
1、布局文件activity_main.xml
2、要实现下拉刷新的功能很简单,只需要实现OnRefreshListener的OnRefresh方法即可。
这里说一下如何实现上拉和下拉分别执行不同的操作。
原理是:根据下拉和上拉显示的布局的可见状态类区分上拉还是下拉,然后执行相应操作。
在PullToRefresh的类库的com.handmark.pulltorefresh.library包下,打开PullToRefreshBase.java,在这个类的最后面添加如下代码:
3、在Activity的代码如下:
如上代码所示,在OnRefresh的实现代码中,用以区分上拉还是下拉,关键代码如下:
至此,运行项目,可以得到演示结果了。
本文已经被整理到《Android下拉刷新上拉加载效果》,欢迎大家学习研究。
文章内容很丰富,希望对大家学习Android实现上拉加载更多以及下拉刷新功能有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Android实现下拉刷新和上拉加载更多的具体代码,供大家参考,具体内容如下先分享下源码:Android实现下拉刷新和上拉加载更多实现思路:
简介上拉加载,是目前手机网站加载数据的一种常用方式,本文主要讲解AnglarJs集成,上拉加载功能。通常与下拉刷新配置使用,下拉刷新请查阅。实现页面{{bran
之前使用的组件是ListView,当时要添加一个下拉刷新,上拉加载的功能,所以对ListView做了一些封装,但是后来看官方文档,不建议再使用ListView,
1.概述在实际开发中,我们经常遇到需要在ListView或RecycleView头部添加ViewPager实现Banner轮播效果,并需要添加下拉刷新,上拉加载
微信小程序scroll-view实现上拉加载与下拉刷新的实例实现效果图:如图,使用小程序的scroll-view实现的上拉加载数据,下拉刷新数据,试下代码如下: