今天在codepath 上看到一个开源项目 [点击查看]使用到了SwipeRefreshLayout 实现了下拉刷新,但示例并不完整,于是自己就动手写了下.之前看到郭霖的博客上也有介绍下拉刷新,不过他是纯手动实现的,代码量大,较为繁琐.[点击查看]而使用Android 提供的SwipeRefreshLayout 则大大减少了我们的工作量,当然,学会了使用SwipeRefreshLayout之后还是建议去看看怎样不借助SwipeRefreshLayout从零开始实现"下拉刷新".


SwipeRefreshLayoutis a ViewGroup that can hold only one scrollable view as a child. This can be either aScrollViewor anAdapterViewsuch as aListView.

Note:This layout only exists within more recent versions of support-v4 asexplained in this post. Edit yourapp/build.gradlefile to include a support library later than version 19:

要注意的是 SwipeRefreshLayout 在 Android 4.4.2(API 19) 的版本才得到支持,因此在建工程的时候最低版本要选 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);        }}



接下来是两个类 Item类 和Adapter类[这就属于ListView的的基本用法了] 里面做了优化


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



更多相关文章

  1. Android使用Realm数据库实现App中的收藏功能(代码详解)
  2. Android开发笔记(一百二十三)下拉刷新布局SwipeRefreshLayout
  3. Android实现固定屏幕显示的方法
  4. Android: JNI本地函数控制Java端代码
  5. Android(安卓)JNI 面面观 1
  6. Android生成带圆角的Bitmap图片
  7. Android(安卓)TextView如何显示html样式的文字
  8. Android仿QQ5.0 的侧滑菜单效果的实现
  9. Android(安卓)UI目录

随机推荐

  1. Android如何高效加载大图
  2. Android内存监控与分析(一):内存泄漏(Memory
  3. 红茶一杯话Binder(ServiceManager篇)
  4. 使用Application Crash Reports for Andr
  5. 【转】Android最佳性能实践(三)——高性
  6. Android(安卓)快捷方式
  7. [行业趋势] Android前景大讨论
  8. andorid 自动化测试初探
  9. android两端对齐
  10. Android手写优化-更为平滑的签名效果实现