安卓開發之自訂下拉重新整理頭部

來源:互聯網
上載者:User

一直用的下拉重新整理庫就是android-Ultra-Pull-to-Refresh,本身這個庫就帶有幾種樣式的下拉重新整理頭部,大家可以去git看一下,地址https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh。最為方便的是我們可以自己定製各式各樣的頭部。最近項目有個自訂的下拉頭部,自己研究了一下。實現效果是:

我們要做的就是自己寫一個樣式xml檔案,然後實現PtrUIHandler這個介面,代碼如下:


public class LoadMoreFooterView extends FrameLayout implements PtrUIHandler {
    private LayoutInflater inflater;
 
    // 下拉重新整理視圖(頭部視圖)
    private ViewGroup headView;
 
    // 下拉重新整理文字
    private TextView tvHeadTitle;
 
    // 下拉表徵圖
    private ImageView ivWindmill;
 
  //  private WindmillDrawable drawable;
 
    public LoadMoreFooterView(Context context) {
        this(context, null);
    }
 
    public LoadMoreFooterView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public LoadMoreFooterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
 
    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
 
        inflater = LayoutInflater.from(context);
        /**
         * 頭部
         */
        headView = (ViewGroup) inflater.inflate(R.layout.widget_header, this, true);
        ivWindmill = (ImageView) headView.findViewById(R.id.iv_windmill);
        tvHeadTitle = (TextView) headView.findViewById(R.id.tv_head_title);
        ivWindmill.setVisibility(VISIBLE);
        ivWindmill.setImageResource(R.mipmap.icon_logo);
        tvHeadTitle.setText("下拉重新整理");
 
    }
 
    @Override
    public void onUIReset(PtrFrameLayout ptrFrameLayout) {
        tvHeadTitle.setText("下拉重新整理");
 
    }
 
    @Override
    public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {
        tvHeadTitle.setText("下拉重新整理");
    }
 
    @Override
    public void onUIRefreshBegin(PtrFrameLayout ptrFrameLayout) {
        tvHeadTitle.setText("正在重新整理");
 
    }
 
    @Override
    public void onUIRefreshComplete(PtrFrameLayout ptrFrameLayout) {
        ivWindmill.clearAnimation();
        tvHeadTitle.setText("重新整理完成");
    }
 
    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        final int mOffsetToRefresh = frame.getOffsetToRefresh();
        final int currentPos = ptrIndicator.getCurrentPosY();
        final int lastPos = ptrIndicator.getLastPosY();
 
        if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                tvHeadTitle.setText("下拉重新整理");
            }
        } else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                tvHeadTitle.setText("鬆開重新整理");
            }
        }
 
    }
 
}


使用:

 

/* 建立自訂重新整理頭部view */
LoadMoreFooterView header = new LoadMoreFooterView(this);
/* 設定重新整理頭部view */
ptr_view.setHeaderView(header);
/* 設定回調 */
ptr_view.addPtrUIHandler(header);
ptr_view.setPtrHandler(new PtrHandler() {
    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }
 
    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        ptr_view.postDelayed(new Runnable() {
            @Override
            public void run() {
              
                getdata();
            }
        }, 2000);
    }
});
    /* 延時100秒,自動重新整理 */
ptr_view.postDelayed(new Runnable() {
    @Override
    public void run() {
        ptr_view.autoRefresh();
    }
}, 100);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.