Android-PullToRefresh(一)


先講下這篇寫啥東西,也就是這家伙(chrisbanes)寫的一個上拉下拉刷新的Demo,連接https://github.com/fengcunhan/Android-PullToRefresh

東西弄下來之后,會看到library和sample 2個文件夾,至於library怎么用,先看看官網的資料http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject

注意:如果勾選了library,那么這個項目是不能運行的,會提示:android library projects cannot be launched。所以注意了。

其實library怎么用,很簡單,就是你新建個Android項目,在其他項目中復制.classpath和.project文件到library下面,使之成為一個Android項目(只是這個項目是被引用的項目),如果你不怕麻煩也可以新建一個Android項目,把有用的文件對應拷貝到新建的Android項目中。

sample文件夾也一樣。在該例子中有4個,PullToRefreshExpandableListActivity,PullToRefreshGridActivity,PullToRefreshListActivity,PullToRefreshWebViewActivity

在應用開發中最常用的當屬 PullToRefreshListActivity

接下來貼點代碼如何把這個例子搬到自己的項目中:代碼來源library中的,

com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor

com.handmark.pulltorefresh.library.internal.LoadingLayout

com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase<T extends AbsListView>

com.handmark.pulltorefresh.library.PullToRefreshBase<T extends View>

com.handmark.pulltorefresh.library.PullToRefreshListView

這里面的5個類就可以組建你想要的上拉下拉組件,當然還需要一些res文件了

當然也需要改動:

     com.handmark.pulltorefresh.library.PullToRefreshBase類中

private void init(Context context, AttributeSet attrs) {

        setOrientation(LinearLayout.VERTICAL);

        touchSlop = ViewConfiguration.getTouchSlop();

        // Styleables from XML first
        //final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PullToRefresh);
        //mode = a.getInteger(R.styleable.PullToRefresh_mode, MODE_PULL_DOWN_TO_REFRESH);
        //Styleables from XML modify last
        mode = 0x3;
        
        
        // Refreshable View
        // By passing the attrs, we can add ListView/GridView params via XML
        refreshableView = this.createRefreshableView(context, attrs);
        this.addRefreshableView(context, refreshableView);

        // Loading View Strings
        String pullLabel = context.getString(R.string.pull_to_refresh_pull_label);
        String refreshingLabel = context.getString(R.string.pull_to_refresh_refreshing_label);
        String releaseLabel = context.getString(R.string.pull_to_refresh_release_label);

        // Add Loading Views
        if (mode == MODE_PULL_DOWN_TO_REFRESH || mode == MODE_BOTH) {
            headerLayout = new LoadingLayout(context, MODE_PULL_DOWN_TO_REFRESH, releaseLabel, pullLabel,
                    refreshingLabel);
            addView(headerLayout, 0, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            measureView(headerLayout);
            headerHeight = headerLayout.getMeasuredHeight();
        }
        if (mode == MODE_PULL_UP_TO_REFRESH || mode == MODE_BOTH) {
            footerLayout = new LoadingLayout(context, MODE_PULL_UP_TO_REFRESH, releaseLabel, pullLabel, refreshingLabel);
            addView(footerLayout, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            measureView(footerLayout);
            headerHeight = footerLayout.getMeasuredHeight();
        }

        // Styleables from XML modify first
        /*if (a.hasValue(R.styleable.PullToRefresh_headerTextColor)) {
            final int color = a.getColor(R.styleable.PullToRefresh_headerTextColor, Color.BLACK);
            if (null != headerLayout) {
                headerLayout.setTextColor(color);
            }
            if (null != footerLayout) {
                footerLayout.setTextColor(color);
            }
        }
        if (a.hasValue(R.styleable.PullToRefresh_headerBackground)) {
            this.setBackgroundResource(a.getResourceId(R.styleable.PullToRefresh_headerBackground, Color.WHITE));
        }
        if (a.hasValue(R.styleable.PullToRefresh_adapterViewBackground)) {
            refreshableView.setBackgroundResource(a.getResourceId(R.styleable.PullToRefresh_adapterViewBackground,
                    Color.WHITE));
        }
        a.recycle();*/
        //Styleables from XML modify last
        if (null != headerLayout) {
            headerLayout.setTextColor(Color.BLACK);
        }
        if (null != footerLayout) {
            footerLayout.setTextColor(Color.BLACK);
        }
        
        // Hide Loading Views
        switch (mode) {
            case MODE_BOTH:
                setPadding(0, -headerHeight, 0, -headerHeight);
                break;
            case MODE_PULL_UP_TO_REFRESH:
                setPadding(0, 0, 0, -headerHeight);
                break;
            case MODE_PULL_DOWN_TO_REFRESH:
            default:
                setPadding(0, -headerHeight, 0, 0);
                break;
        }

        // If we're not using MODE_BOTH, then just set currentMode to current
        // mode
        if (mode != MODE_BOTH) {
            currentMode = mode;
        }
    }


修改前和修改后的代碼都有注釋

至於這個組件該怎么用還是自己琢磨把///

歡迎拍磚

--------------------------------------------------------------------------

前段時間由於工作太忙,故沒有上傳demo,特此補上...

 Threew_PullToRefresh-master.rar

------------------------------------------------------------------------

最近在網上查了點資料,關於Android Library的參考資料

官網的資料

Android Library

Android 公共庫的建立方法

github入門到精通


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM