今天在codepath 上看到一個開源項目 [點擊查看]使用到了 SwipeRefreshLayout 實現了下拉刷新,但示例並不完整,於是自己就動手寫了下.之前看到郭霖的博客上也有介紹下拉刷新,不過他是純手動實現的,代碼量大,較爲繁瑣.[點擊查看]而使用Android 提供的SwipeRefreshLayout 則大大減少了我們的工作量,當然,學會了使用SwipeRefreshLayout之後還是建議去看看怎樣不借助SwipeRefreshLayout從零開始實現"下拉刷新".
SwipeRefreshLayout is a ViewGroup that can hold only one scrollable
view as a child. This can be either a ScrollView
or anAdapterView
such
as a ListView
.
Note: This layout only exists within more recent versions of support-v4 as explained in this
post. Edit your app/build.gradle
file to include a support library later than version 19:
先看效果:
接下來我就直接上代碼了. [其中用到的圖片可以下載源碼,圖片均包含在裏面]
佈局文件(XML)[activity_main.xml]:
裏面就一個ListView,也不要有其他多餘的東西
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_marginTop="30dp"
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
MainActivity 代碼:
package com.demo.mummyding.learnswiperefreshlayout;
import android.app.Activity;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
/**
* 這裏要實現 OnRefreshListener 接口
*/
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener{
private SwipeRefreshLayout swipeContainer;
ListView listView;
List<viewItem> list;
itemAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
/**
* 下來刷新就會觸發執行此方法
*/
@Override
public void onRefresh() {
/**
* 用Handler().postDelayed 延遲執行
* 當然,不用延遲也可以,我這裏是爲了看效果,因爲這裏刷新嘩的一下就沒了~
*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
list.clear();
addItems();
adapter.notifyDataSetChanged();
swipeContainer.setRefreshing(false);
}
}, 1000);
/*
不用延遲可以直接像下面這樣寫
*/
/*
* list.clear();
addItems();
adapter.notifyDataSetChanged();
swipeContainer.setRefreshing(false);
*/
}
/**
* 初始化變量&添加事件監聽
*/
void init(){
listView = (ListView) findViewById(R.id.list_view);
swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
swipeContainer.setOnRefreshListener(this);
list = new ArrayList<viewItem>();
adapter = new itemAdapter(this,R.layout.view_layout,list);
listView.setAdapter(adapter);
}
/**
* 向ListView添加Item 下面的Item 可以多複製幾遍~_~
*/
void addItems(){
viewItem addItem = new viewItem("Aaron");
list.add(addItem);
addItem = new viewItem("Barton");
list.add(addItem);
addItem = new viewItem("Beacher");
list.add(addItem);
addItem = new viewItem("Colbert");
list.add(addItem);
addItem = new viewItem("Dick");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
}
}
package com.demo.mummyding.learnswiperefreshlayout;
/**
* Created by mummyding on 15-7-20.
*/
public class viewItem {
private String itemName;
public String getItemName() {
return itemName;
}
public viewItem(String itemName) {
this.itemName = itemName;
}
}
package com.demo.mummyding.learnswiperefreshlayout;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by mummyding on 15-7-20.
*/
public class itemAdapter extends ArrayAdapter<viewItem> {
public itemAdapter(Context context, int resource, List<viewItem> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder;
viewItem item = getItem(position);
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(R.layout.view_layout, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.tv_Name);
viewHolder.name.setText(item.getItemName());
view.setTag(viewHolder);
}else{
view = convertView;
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.name.setText(item.getItemName());
}
return view;
}
/**
* 爲了減少getView方法中 findViewById 調用次數 而添加的一個輔助類.
*/
class ViewHolder{
TextView name;
}
}
最後還有一個Item View的局部文件[view_layout.xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_Uers"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="@drawable/user"
/>
<TextView
android:id="@+id/tv_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_Uers"
android:gravity="center"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</LinearLayout>
完整代碼:https://github.com/MummyDing/SwipeRefreshLayout
【轉載請註明出處】
Author: MummyDing
出處:http://blog.csdn.net/mummyding/article/category/5651761