android自带下拉刷新SwipeRefreshLayout

Posted ocean123123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android自带下拉刷新SwipeRefreshLayout相关的知识,希望对你有一定的参考价值。

也是一个布局容器,只有一个子组件,类似scrollView

<?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:orientation="vertical"
    android:id="@+id/swipeLayout" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

</android.support.v4.widget.SwipeRefreshLayout>
public class MainActivity extends AppCompatActivity 
    SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeLayout);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,
                R.color.colorPrimary,
                R.color.colorPrimary,
                R.color.colorPrimaryDark);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);;
        swipeRefreshLayout.setProgressViewOffset(true, 0, 200);
        swipeRefreshLayout.setProgressViewEndTarget(true, 100);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() 
            @Override
            public void onRefresh() 
                new Thread(new Runnable() 
                    @Override
                    public void run() 
                        try 
                           sleep(3000);
                         catch (InterruptedException e) 
                            e.printStackTrace();
                        
                        mHandler.sendEmptyMessage(1);
                    
                ).start();
            
        );
        //handler
        @SuppressLint("HandlerLeak")
        Handler mHandler = new Handler()
            @Override
            public void handleMessage(Message msg) 
                super.handleMessage(msg);
                switch (msg.what) 
                    case 1:
                        Toast.makeText(MainActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
                        swipeRefreshLayout.setRefreshing(false);
                        break;
                
            
        ;
    

属性一运行便知道,在监听器里执行刷新任务,用handler通知主线程

以上是关于android自带下拉刷新SwipeRefreshLayout的主要内容,如果未能解决你的问题,请参考以下文章

Android-利用Jetpack-Compose-+Paging3+swiperefresh实现分页加载,下拉上拉效果

android自带下拉刷新SwipeRefreshLayout

android webview怎么实现下拉刷新

SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新

Jetpack Compose : 一学就会的自定义下拉刷新&加载更多

Jetpack Compose : 一学就会的自定义下拉刷新&加载更多