简单的实现下拉刷新 SwipeRefreshLayout


主要有以下方法

  1. setColorSchemeResources:设置刷新循环显示的颜色
  2. setRefreshing :设置刷新是否可用
  3. setOnRefreshListener :实现刷新监听器,里面为刷新时实现的代码(在这里我新添加了两项,并且调用adapter 的notifyDataSetChanged方法对listView进行更新,调用handler的postDelayed方法设置延迟三秒发送停止刷新)
  4. mSwipeLayout.setProgressBackgroundColor(): 设定下拉圆圈的背景
  5. mSwipeLayout.setSize(): 设置圆圈的大小
布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe_swipe">
    <ListView
        android:id="@+id/swipe_listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
    </android.support.v4.widget.SwipeRefreshLayout>
主页面的代码Activity
public class SwipeActivity extends AppCompatActivity {

    private SwipeRefreshLayout mSwipe;
    private ListView mListView;
    private List<String> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe);
        mSwipe= (SwipeRefreshLayout) findViewById(R.id.swipe_swipe);
        mListView= (ListView) findViewById(R.id.swipe_listView);
        initData();
        final ArrayAdapter<String> mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mDatas);
        mListView.setAdapter(mAdapter);
        mSwipe.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);

        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDatas.add("IIIIII");
                        mDatas.add("JJJJJJ");
                        mAdapter.notifyDataSetChanged();
                        mSwipe.setRefreshing(false);
                    }
                },3000);
            }
        });
    }
    public void initData(){
        mDatas=new ArrayList<>();
        mDatas.add("AAAAAA");
        mDatas.add("BBBBBB");
        mDatas.add("CCCCCC");
        mDatas.add("DDDDDD");
        mDatas.add("EEEEEE");
        mDatas.add("FFFFFF");
        mDatas.add("GGGGGG");
        mDatas.add("HHHHHH");

    }

}

============================================================================

下面就对应第二种 结合recycleview实现

对应的主布局

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</android.support.v4.widget.SwipeRefreshLayout>

下面是主的Activity 

public class MainActivity extends AppCompatActivity  implements SwipeRefreshLayout.OnRefreshListener {

    SwipeRefreshLayout swipeRefreshLayout;//定义该控件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe); //初始化 findByid找到
        swipeRefreshLayout.setOnRefreshListener(this);       //定义它刷新的事件 
        //进行判断 什么时候停止  该方法必须写在 请求数据成功以后 得到数据在下面写这个方法 
        if (swipeRefreshLayout.isRefreshing()){
            swipeRefreshLayout.setRefreshing(false);
        }

    }
    //   Activity实现 (implements)实现   SwipeRefreshLayout.OnRefreshListener   对应返回的方法在里面写上你的解析的数据
    @Override
    public void onRefresh() {
        userpresenter.getUser(Api.HOME_URL); //解析的数据
    }

} 


//详细进去:http://blog.csdn.net/qq_39085422/article/details/78319253

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值