SwipeRefreshLayout usage in Android

When we only use pull-down refresh, we can often use the more concise SwipeRefreshLayout to achieve this function. Let's take a look at the usage of this control.

1. Common methods

  • setOnRefreshListener: Set the refresh listener. The onRefresh method of the listener OnRefreshListener needs to be rewritten, which is triggered when the pull-down is released.
  • setRefreshing: Set the state of refreshing. true means refreshing, false means refreshing finished.
  • isRefreshing: Determine whether it is refreshing.
  • setColorSchemeColors: Set the ring color of the progress circle.
  • setProgressBackgroundColorSchemeColor: Set the background color of the progress circle.
  • setProgressViewOffset: Set the offset of the progress circle. The first parameter indicates whether the progress circle is zoomed, the second parameter indicates the offset from the top when the progress circle starts to appear, and the third parameter indicates the offset from the top when the progress circle is pulled to the maximum.
  • setDistanceToTriggerSync: Set how far down the gesture will trigger the refresh operation.

2. Sample code

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <!-- 注意SwipeRefreshLayout节点必须使用完整路径 -->
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/srl_simple"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- SwipeRefreshLayout的下级必须是可滚动的视图 -->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tv_simple"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingTop="10dp"
                android:text="这是一个简单视图"
                android:textColor="#000000"
                android:textSize="17sp" />
        </ScrollView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private TextView tv_simple;
    private SwipeRefreshLayout srl_simple; // 声明一个下拉刷新布局对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_simple = findViewById(R.id.tv_simple);
        // 从布局文件中获取名叫srl_simple的下拉刷新布局
        srl_simple = findViewById(R.id.srl_simple);
        // 给srl_simple设置下拉刷新监听器
        srl_simple.setOnRefreshListener(this);
        // 设置下拉刷新布局的进度圆圈颜色
        srl_simple.setColorSchemeResources(
                R.color.red, R.color.orange, R.color.green, R.color.blue);
    }

    private Handler mHandler = new Handler(); // 声明一个处理器对象
    // 定义一个刷新任务
    private Runnable mRefresh = new Runnable() {
        @Override
        public void run() {
            tv_simple.setText("刷新完成");
            // 结束下拉刷新布局的刷新动作
            srl_simple.setRefreshing(false);
        }
    };

    /**
     * 一旦在下拉刷新布局内部往下拉动页面,就触发下拉监听器的onRefresh方法
     */
    @Override
    public void onRefresh() {
        tv_simple.setText("正在刷新");
        // 延迟若干秒后启动刷新任务
        mHandler.postDelayed(mRefresh, 2000);
    }
}

 

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/115027179