当前位置: 首页 > 工具软件 > Swipe Bar > 使用案例 >

谷歌给出的Swipe—To—Refresh

田玉韵
2023-12-01
最近使用一款pianke的APP,发现里面的刷新是使用系统的Swiperefreshlayout;正好在开源环信UI的效果里面同样是这个刷新效果。我就去看看API,毕竟有时候在不想使用第三方开源库的前提下,可以去尝试使用它。


看一下官方给出的API:

The swipe-to-refresh user interface pattern is implemented entirely within the SwipeRefreshLayout widget, which detects the vertical swipe, displays a distinctive progress bar, and triggers callback methods in your app. You enable this behavior by adding the widget to your layout file as the parent of a ListView or GridView, and implementing the refresh behavior that gets invoked when the user swipes.

This lesson shows you how to add the widget to an existing layout. It also shows you how to add a refresh action to the action bar overflow area, so that users who may be unable to use the swipe gesture can trigger a manual update with an external device.

大概意思就是使用SwipeRefreshLayout可以实现listView或者GridView中;

在使用的过程中,保证工程中的v4包是最新版本的,不然会找不到对应的类。在listView中简单的使用:

mRefreshLayout.setColorSchemeResources(R.color.google_blue,
                R.color.google_green,
                R.color.google_red,
                R.color.google_yellow);

设置刷新的颜色;

对于首次加载自动弹出刷新加载效果,需要自己设置一个位置

mRefreshLayout.setProgressViewOffset(false, 0, CommonUtil.dip2px(this, 24));
mRefreshLayout.setRefreshing(true);//为true时动画显示,http加载完成时设置为false,刷新结束;

<pre name="code" class="java">mRefreshLayout.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                //自己的刷新逻辑
            }
        });
简单的一个下拉刷新效果就出现了,不过这种情况下使用于简单的显示效果,如果在项目中出现加载更多呢,这个时候需要我们去加载一个底部XML,重新方法实现。

 

参考了网上一个最简单的加载:
<pre name="code" class="html">package com.xf.swiperefreshlayout;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;

public class RefreshLayout extends SwipeRefreshLayout {
    private int mTouchSlop;
    private ListView mListView;
    private OnLoadListener mOnLoadListener;
    private View mListViewFooter;
    private int mYDown;
    private int mLastY;
    private boolean isLoading = false;
    
    public RefreshLayout(Context context) {
        this(context, null);
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    //set the footer of the ListView with a ProgressBar in it
    public void setFooterView(Context context, ListView mListView, int layoutId) {
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mListViewFooter = LayoutInflater.from(context).inflate(layoutId, null,
                false);
        mListView.addFooterView(mListViewFooter);
        mListView.setFooterDividersEnabled(false);
        this.mListView = mListView;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mYDown = (int) event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                mLastY = (int) event.getRawY();
                if (isPullingUp())
                    //you can add view or hint here when pulling up the ListView
                break;

            case MotionEvent.ACTION_UP:
                if (canLoad()) {
                    loadData();
                }
                break;
            default:
                break;
        }

        return super.dispatchTouchEvent(event);
    }

    private boolean canLoad() {
        return isBottom() && !isLoading && isPullingUp();
    }

    private boolean isBottom() {
        if (mListView.getCount() > 0) {
            if (mListView.getLastVisiblePosition() == mListView.getAdapter().getCount() - 1 &&
                    mListView.getChildAt(mListView.getChildCount() - 1).getBottom() <= mListView.getHeight()) {
                return true;
            }
        }
        return false;
    }

    private boolean isPullingUp() {
        return (mYDown - mLastY) >= mTouchSlop;
    }

    private void loadData() {
        if (mOnLoadListener != null) {
            setLoading(true);
            mOnLoadListener.onLoad();
        }
    }

    public void setLoading(boolean loading) {
        isLoading = loading;
        if (isLoading) {
            if (isRefreshing()) setRefreshing(false);
            if (mListView.getFooterViewsCount() == 0) {
                mListView.addFooterView(mListViewFooter);
                mListView.setSelection(mListView.getAdapter().getCount() - 1);
            } else {
                mListViewFooter.setVisibility(VISIBLE);
                //mListView.addFooterView(mListViewFooter);
            }
        } else {
            if (mListView.getAdapter() instanceof HeaderViewListAdapter) {
                mListView.removeFooterView(mListViewFooter);
            } else {
                mListViewFooter.setVisibility(View.GONE);
            }
            mYDown = 0;
            mLastY = 0;
        }
    }

    public void setOnLoadListener(OnLoadListener loadListener) {
        mOnLoadListener = loadListener;
    }

    public static interface OnLoadListener {
        public void onLoad();
    }
}
XML文件一个简单的ProgressBar控件,main的XML根据官方文档:
 
<pre name="code" class="java"><android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>
和抽屉控件的方式是一样的;然后在MainActivity中调一下重写onLoad的接口就可以简单的实现了,当然这个加载更多显示效果难看,没有动画效果,后面自己项目中如果使用到,会对他进行重写加入动画效果。
 
总结一下吧,第三方刷新的框架可以使用GotHub上的库,推荐一个自己使用一个刷新Demo,这个可以满足自己定义的布局文件

http://download.csdn.net/detail/u014400200/8945473



 类似资料: