美文网首页swipeRefreshLayout
【SwipeRefreshLayout】Google官方下拉刷新

【SwipeRefreshLayout】Google官方下拉刷新

作者: jackLee | 来源:发表于2016-03-24 16:27 被阅读4158次

是否还为ListView的下拉刷新而感到困扰?是否还在寻找第三方的下拉刷新的库?不用慌,Google出了自己的下拉刷新控件了。

关于SwipeRefreshLayout

  • 官方API:SwipeRefreshLayout
  • SwipeRefreshLayout继承自ViewGroup,完整包地址:android.support.v4.widget.SwipeRefreshLayout。
  • 使用起来很简单:只要在需要刷新的控件最外层加上SwipeRefreshLayout,然后他的child首先是可滚动的view,如ScrollView或者ListView,RecyclerView
  • 注意:必须把你的support library的版本升级到19.1

Demo:

<pre>
mainlayout.xml布局文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
</pre>

  • 代码中调用:
    <pre>
    package com.jabony.swiperefreshlayou;
    import java.util.ArrayList;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.widget.ListView;
    import com.jabony.swiperefreshlayout.R;
    public class SwipRefreshLayoutActivity extends Activity implements
    SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout swipeLayout;
    private ListView listView;
    private ListViewAdapter adapter;
    private ArrayList<SoftwareClassificationInfo> list; private boolean isRefresh = false;//是否刷新中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_refresh_layout);
    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);

    setColorSchemeResources(R.color.orange, R.color.green, R.color.blue);小圈圈的颜色。转一圈换一种颜色,每一圈耗时1s。

     list = new ArrayList<SoftwareClassificationInfo>();  
     list.add(new SoftwareClassificationInfo(1, "asdas"));  
     listView = (ListView) findViewById(R.id.list);  
     adapter = new ListViewAdapter(this, list);  
     listView.setAdapter(adapter);  
    

    }

    public void onRefresh() { if(!isRefresh){ isRefresh = true;
    new Handler().postDelayed(new Runnable() {
    public void run() {
    swipeLayout.setRefreshing(false);
    list.add(new SoftwareClassificationInfo(2, "ass"));
    adapter.notifyDataSetChanged(); isRefresh= false;
    }
    }, 3000); }
    }
    }
    </pre>
    需要注意的地方:

  • SwipeRefreshLayout有一个Listener需要被实现然后添加入SwipeRefreshLayout对象中。在onRefresh() 方法中实现自己的业务逻辑,比如刷新ListView的数据。

  • setRefreshing(boolean): 显示或隐藏刷新进度条

  • isRefreshing(): 检查是否处于刷新状态

  • setColorScheme(): 设置进度条的颜色主题,最多能设置四种//此方法已过时,推荐setColorSchemeResources()

  • setColorSchemeResources():设置小圈的颜色,小圈每转一圈需要1s。


参考文档:

关注我学习更多技术哦

学习更多技术

相关文章

网友评论

    本文标题:【SwipeRefreshLayout】Google官方下拉刷新

    本文链接:https://www.haomeiwen.com/subject/qxerlttx.html