Android 下拉重新整理的實現。使用 SwipeRefreshLayout 代替 pull-to-refesh

來源:互聯網
上載者:User

標籤:

概述

  Google官方推出了SwipeRefreshLayout 來實現下拉重新整理的效果。對比以前我們常用的 pull-to-refesh ,這個方案顯得更加的簡單方便。

 

關聯項目引用(管理依賴)

  在你的 應用層級的 build.gradle  中添加如下:  

   compile ‘com.android.support:appcompat-v7:23.0.0‘    compile ‘com.android.support:support-v4:23.0.0‘

 

編寫布局(Layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipeRefreshLayout1"        android:layout_width="match_parent"        android:layout_height="match_parent">        <ListView            android:id="@+id/listview1"            android:layout_width="match_parent"            android:layout_height="match_parent" />    </android.support.v4.widget.SwipeRefreshLayout></LinearLayout>

 

在代碼中  添加監聽器處理事件
  public void setOnRefreshListener(OnRefreshListener listener)
  
完整代碼
package demo.vir56k.swiperefreshlayoutdemo;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.ArrayAdapter;import android.widget.ListView;import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class MainActivity extends AppCompatActivity {    private static final int REFRESH_COMPLETE = 0X110;    private SwipeRefreshLayout mSwipeLayout;    private ListView mListView;    private ArrayAdapter<String> mAdapter;    private List<String> mDatas;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mListView = (ListView) findViewById(R.id.listview1);        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout1);        mDatas = creatDataSource();        mSwipeLayout.setOnRefreshListener(mSwipeRefreshLayoutOnRefreshListener);        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,                android.R.color.holo_orange_light, android.R.color.holo_red_light);        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);        mListView.setAdapter(mAdapter);    }    private List<String> creatDataSource() {        mDatas = new ArrayList<String>();        for (int i = 0; i < 5; i++) {            mDatas.add("item" + i);        }        return mDatas;    }    SwipeRefreshLayout.OnRefreshListener mSwipeRefreshLayoutOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {        @Override        public void onRefresh() {            mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);        }    };    private Handler mHandler = new Handler() {        public void handleMessage(android.os.Message msg) {            switch (msg.what) {                case REFRESH_COMPLETE:                    List<String> lst = new ArrayList<String>();                    for (int i = mDatas.size() + 3; i > mDatas.size(); i--) {                        lst.add("item -" + i);                    }                    mDatas.addAll(0, lst);                    mAdapter.notifyDataSetChanged();                    mSwipeLayout.setRefreshing(false);                    break;            }        }        ;    };}

至此完成。

 

補充:

其他情形,遇到"SwipeRefreshLayout 的子控制項不 直接是個listview 等,是個普通 relativewlayout 怎麼辦?"

解決方案: 繼承 SwipeRefreshLayout 重寫 canChildScrollUp 方法

 

/* * Copyright 2016, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.example.android.architecture.blueprints.todoapp.tasks;import android.content.Context;import android.support.v4.view.ViewCompat;import android.support.v4.widget.SwipeRefreshLayout;import android.util.AttributeSet;import android.view.View;/** * Extends {@link SwipeRefreshLayout} to support non-direct descendant scrolling views. * <p> * {@link SwipeRefreshLayout} works as expected when a scroll view is a direct child: it triggers * the refresh only when the view is on top. This class adds a way (@link #setScrollUpChild} to * define which view controls this behavior. */public class ScrollChildSwipeRefreshLayout extends SwipeRefreshLayout {    private View mScrollUpChild;    public ScrollChildSwipeRefreshLayout(Context context) {        super(context);    }    public ScrollChildSwipeRefreshLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    public boolean canChildScrollUp() {        if (mScrollUpChild != null) {            return ViewCompat.canScrollVertically(mScrollUpChild, -1);        }        return super.canChildScrollUp();    }    public void setScrollUpChild(View view) {        mScrollUpChild = view;    }}

 

  

  

  

Android 下拉重新整理的實現。使用 SwipeRefreshLayout 代替 pull-to-refesh

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.