史上最最最炫酷動感的下拉刷新,只需要十秒鐘就可以讓你完成。

在我們處理很多很繁冗數據的時候,我們用到了listview去裝載數據,但可顯示的區域有限,這時候我們就需要用到我們的下拉刷新去加載更多的數據了。這個效果是仿製android5.0的效果在額外多了一個粘性效果動畫,更加的酷了。


   下面 我們還是先來看 一下 項目的結構。

上面的是我們加入的工具類 那裏的代碼都是人家封裝好的了 我們關鍵看 怎麼用就好了,站在巨人的肩膀總是輕鬆的。我們看一下 怎麼使用這個 刷新的 主類把

MainActivity
package jp.co.recruit_lifestyle.sample;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout;

public class MainActivity extends AppCompatActivity implements WaveSwipeRefreshLayout.OnRefreshListener {

  private ListView mListview;

  private WaveSwipeRefreshLayout mWaveSwipeRefreshLayout;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    /**設置沒有標題欄*/
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //透明狀態欄
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    //透明導航欄
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    setSampleData();
  }

  private void initView() {
    mWaveSwipeRefreshLayout = (WaveSwipeRefreshLayout) findViewById(R.id.main_swipe);
    //設置 下拉在轉的圈圈的顏色            第一個顏色 是 第一圈帶箭頭的 第二個顏色是不帶箭頭的
    mWaveSwipeRefreshLayout.setColorSchemeColors(Color.CYAN, Color.RED);
    mWaveSwipeRefreshLayout.setOnRefreshListener(this);
    /**紅色0x000000ff
     綠色0x0000ff00
     藍色0x00ff0000
     黃色0x0000ffff
     青色0x00ffff00
     品紅0x00ff00ff
     黑色0x00000000
     白色0x00ffffff
     灰色0x00808080
     草綠0x006bdec7
     紫色0x00c000c0*/
    //mWaveSwipeRefreshLayout.setWaveColor(0x0000ff00);
    //mWaveSwipeRefreshLayout.setBackgroundColor(0x00000000);

    //設置水滴落下的最大高度
    mWaveSwipeRefreshLayout.setMaxDropHeight(500);

    mListview = (ListView) findViewById(R.id.main_list);
  }

  private void setSampleData() {
    ArrayList<String> sampleArrayStr = new ArrayList<>();
    for (int i = 0; i < 60; i++) {
      sampleArrayStr.add("" );
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, sampleArrayStr);
    mListview.setAdapter(adapter);
  }

  private void refresh() {
    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        // 更新が終了したらインジケータ非表示
        mWaveSwipeRefreshLayout.setRefreshing(false);
      }
    }, 3000);
  }

  @Override
  protected void onResume() {
    mWaveSwipeRefreshLayout.setRefreshing(false);
    refresh();
    super.onResume();
  }

  @Override
  public void onRefresh() {
    refresh();
  }




}
在這個類裏面 我們調用了刷新的類 設置了他一些屬性,當然 我們通過
mWaveSwipeRefreshLayout.set****();你可以去設置更多你想要自己設置的東西了。
我們的佈局也非常簡單 就是 view+view +自定義的刷新類+listview的形式來實現的
代碼:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >

  <View
      android:layout_width="match_parent"
      android:layout_height="25dip"
      android:background="@color/primary_dark"
      />

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      >

    <android.support.v7.widget.Toolbar
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/primary"
        android:id="@+id/toolbar"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/toolbar_dropshadow"
        android:layout_below="@+id/toolbar"
        />


    <jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_swipe"
        android:layout_below="@+id/toolbar"
        >

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

    </jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout>

  </RelativeLayout>

</LinearLayout>
還有圖片資源 跟 顏色的配置之類的 大家 自行看我上傳的demo 到 res 文件夾下查看。

通過人家封裝好的工具類我們花一點時間就可以實現 跟別人與衆不同的下拉控件了。

大家 有問題可以 進羣:166120952 進行 討論

下載地址:http://download.csdn.net/detail/ningzhouxu/9619348

現在上傳的資源大部分都是 android studio 來進行編譯的了。麼麼噠


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章