xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:orientation="vertical"
    tools:context="example.com.picassodemo.MainActivity">
 
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <ListView
            android:id="@+id/lv_activity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>

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

JAVA

public class MainActivity extends AppCompatActivity  {
 
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        final SwipeRefreshLayout swipeRefresh= (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.colorAccent,null));
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
 
                Toast.makeText(getApplicationContext(),"onRefresh",Toast.LENGTH_SHORT).show();
 
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            Thread.sleep(2000);
 
                        }catch (Exception e){
                            e.printStackTrace();
                        }
 
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
 
                                //更新数据
                                swipeRefresh.setRefreshing(false);
                            }
                        });
 
                    }
                }).start();
 
 
            }
        });
    }

————————————————
版权声明:本文为博主「aidnexl」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。