androidx的下拉刷新

  mSwipeRefreshLayout=messageLayout.findViewById(R.id.swipe_fresh);
 mSwipeRefreshLayout.setRefreshing(true);
   private void initView(){
           mSwipeRefreshLayout.setOnRefreshListener(() -> {
               index();
           });
       }
       <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/swipe_fresh"
    xmlns:android="http://schemas.android.com/apk/res/android"/>
下拉刷新就这么简单 
上拉的话
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"/>

这事要刷新发起的请求

   public void refresh (Integer page,Integer type ){
            String url = "
            MyHttp.okHttpGet(mContext,url,new MyCallBackUtil(){
                public void onSuccess(Context context, JSONObject response) {
                    try {
                        Log.d(TAG, "获取收益信息" + response);
                        items = response.getJSONArray("items");
                        notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    });
    }


mNestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            if (scrollY > oldScrollY) {
                // 向下滑动
            }
            if (scrollY < oldScrollY) {
                // 向上滑动
            }
            if (scrollY == 0) {
                // 顶部
            }
            if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                // 底部
                Log.d(TAG, "触发: ");
                new Handler().postDelayed(new Runnable(){
                    public void run() {
                        sendMoreRequest();
                    }
                }, 1000);

            }
        });
        mRecyclerView.setAdapter(mAdaptaer);
        通过监听滚动判断是否到了底部
        getMeasuredHeight()是实际View的大小,与屏幕无关
        而getHeight的大小此时则是屏幕的大小
        当超出屏幕后,getMeasuredHeight()等于getHeight()加上屏幕之外没有显示的大小

也就是滚动的距离等于总长度减去一个屏幕的大小 就是到了底部;

这么减应该是负的 2减1是1 1减2是负1 所以理解起来挺容易
写在定时里面是为了延迟触发

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值