这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要介绍了 Android SwipereFreshLayout 下拉刷新的相关资料, 需要的朋友可以参考下
Android SwipereFreshLayout 下拉刷新
我们都知道现在 android5.0 以后就提倡使用 Material Design 设计了。在 Material Design 设计就有一个非常好的设计 SwipereFreshLayout,下面我们就来看看它的使用。既然它来源于 Material Design,我们第一步就应该是添加它的库。
1、我们就在 build.gradle 添加库:
- compile 'com.android.support:support-v4:22.1.1'
2、然后我们就直接在 res/layouts/activity_main.xml 布局里面使用:
- <android.support.v4.widget.SwipeRefreshLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/id_swipe_refresh"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <ListView
- android:id="@+id/id_listview"
- android:layout_width="match_parent"
- android:layout_height="match_parent"></ListView>
- </android.support.v4.widget.SwipeRefreshLayout>
我们可以看到 SwipeRefreshLayout 作为 ListView 的父布局,当滑动到 ListView 的边界时,SwipeRefreshLayout 就会显示正在刷新的动画,同时会提供一个 onRefresh 的事件供我们加载数据。
3、提供数据源
这里我们直接用 ArrayAdapter 就行了,所以我们直接来定义 string-array 就行了。
- <string-array name="singer_names">
- <item>周杰伦</item>
- <item>那英</item>
- <item>刘德华</item>
- <item>张学友</item>
- <item>许巍</item>
- <item>朴树</item>
- <item>陈奕迅</item>
- <item>A_Lin</item>
- <item>杨宗纬</item>
- </string-array>
4、设置 adapter
- setContentView(R.layout.activity_main);
- mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_refresh);
- mListView =(ListView)findViewById(R.id.id_listview);
- String[] singer = getResources().getStringArray(R.array.singer_names);
- mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, singer);
- mListView.setAdapter((ListAdapter) mAdapter);
- mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- refreshContent();
- }
- });
- private void refreshContent() {
- new Handler().postDelayed(new Runnable() {@Override public void run() {
- mAdapter = new ArrayAdapter < >(MainActivity.this, android.R.layout.simple_list_item_1, getSingerNames());
- mListView.setAdapter((ListAdapter) mAdapter);
- //设置刷新加载效果的icon是否继续显示
- mSwipeRefreshLayout.setRefreshing(false);
- }
- },
- 2000);
- }
- private List < String > getSingerNames() {
- List < String > newCatNames = new ArrayList < String > ();
- for (int i = 0; i < mSingerNames.length; i++) {
- int randomCatNameIndex = new Random().nextInt(mSingerNames.length - 1);
- newCatNames.add(mSingerNames[randomCatNameIndex]);
- }
- return newCatNames;
- }
主要是实现 SwipeRefreshLayout.OnRefreshListener 接口,然后实现 onRefresh 就可以刷新数据了,然后通过刷新数据源就可以更新数据了。其实用起来还是很简单的。
我们再来看看 SwipeRefreshLayout 的其他属性。
setColorSchemeResources(R.color.orange, R.color.green, R.color.blue); 改变加载图标的颜色。这样 SwipeRefreshLayout 旋转的时候将会在这三种颜色间切换
setEnabled(false)禁止使用刷新通知
这个属性在一个地方可能会用到,那就是 SwipereFreshLayout 包含多个 childView 的时候,有一个滑动事件冲突的问题,ListView 只能上滑,而不能下拉。一旦下拉,就会触发 SwipeRefreshLayout 的下拉刷新。这种情况肯定是在事件派发上出了问题。下拉的事件在通常情况下应该由 ListView 来进行处理;当 ListView 位于顶部时,由 SwipeRefreshLayout 来进行处理。而现在的情况是全都由 SwipeRefreshLayout 来处理的。这个问题有两种解决的办法:
1、我们知道这个是因为滑动派发的问题,那我们可以自定义一个 SwipeRefreshLayout 继承的 ImprovedSwipeLayout;
在 values 文件夹中新建一个 attrs.xml,内容如下:
- <?xml version="1.0" encoding="utf-8" ?>
- <resources>
- <declare-styleable name="ImprovedSwipeLayoutAttrs">
- <attr name="scrollableChildId" format="reference" />
- </declare-styleable>
- </resources>
在使用自定义 View 中指定 ListView 的 id:
- <com.goach.palm.demo.ImprovedSwipeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:fab="http://schemas.android.com/apk/res-auto"
- xmlns:isl="http://schemas.android.com/apk/res-auto"
- android:id="@+id/swipe_container"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/md_blue_grey_50"
- isl:scrollableChildId="@+id/list_statuses"
- >
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView
- android:id="@+id/list_statuses"
- android:minHeight="?android:attr/listPreferredItemHeight"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingTop="12dp"
- android:paddingBottom="12dp"
- android:paddingLeft="8dp"
- android:paddingRight="8dp"
- android:clipToPadding="false"
- android:divider="@android:color/transparent"
- android:dividerHeight="12dp"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:text="2234544543"
- />
- </FrameLayout>
- </com.goach.palm.demo.ImprovedSwipeLayout>
最后是我的 ImprovedSwipeLayout 全部代码:
- public class ImprovedSwipeLayout extends SwipeRefreshLayout {
- private static final String TAG = ImprovedSwipeLayout.class.getCanonicalName();
- private int mScrollableChildId;
- private View mScrollableChild;
- public ImprovedSwipeLayout(Context context) {
- this(context, null);
- }
- public ImprovedSwipeLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImprovedSwipeLayoutAttrs);
- mScrollableChildId = a.getResourceId(R.styleable.ImprovedSwipeLayoutAttrs_scrollableChildId, 0);
- mScrollableChild = findViewById(mScrollableChildId);
- a.recycle();
- }
- @Override public boolean canChildScrollUp() {
- ensureScrollableChild();
- if (android.os.Build.VERSION.SDK_INT < 14) {
- if (mScrollableChild instanceof AbsListView) {
- final AbsListView absListView = (AbsListView) mScrollableChild;
- return absListView.getChildCount() > 0 && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
- } else {
- return mScrollableChild.getScrollY() > 0;
- }
- } else {
- return ViewCompat.canScrollVertically(mScrollableChild, -1);
- }
- }
- private void ensureScrollableChild() {
- if (mScrollableChild == null) {
- mScrollableChild = findViewById(mScrollableChildId);
- }
- }
- }
还有一种方法就是我们使用上面的 setEnabled 来实现,通过 ListView 的 OnScrollListener 来实现,当滑动到第一个可见的 item 为 0 的时候,我们就 setEnabled(true),否则反之。
- lView.setOnScrollListener(new AbsListView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(AbsListView absListView, int i) {
- }
- @Override
- public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
- if (firstVisibleItem == 0)
- swipeView.setEnabled(true);
- else
- swipeView.setEnabled(false);
- }
- });
这样,就可以很好的解决这个问题了。
来源: http://www.phperz.com/article/17/0823/337580.html