文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

android中SwipeRefresh实现各种上拉,下拉刷新示例

2022-06-06 11:13

关注

SwipeRefresh

基于原生的SwipeRefreshLayout 做了封装处理

此项目中包括种:

1.原生SwipeRefreshLayout(上拉可通过滚动监听实现)

2.自定义支持上拉刷新的组件

3.自定义支持ViewPage的刷新组件VPSwipeRefreshLayout

4.RecyclerView+SwpieRefreshLayout实现下拉刷新效果同时实现上拉功能

主界面

1.原生SwipeRefreshLayout(上拉可通过滚动监听实现)

除了OnRefreshListener接口外,SwipRefreshLayout中还有一些其他重要的方法,具体如下:

1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器。

 2、setProgressBackgroundColor(int colorRes):设置进度圈的背景色(已经弃用)

setProgressBackgroundColorSchemeResource (可以)。

setProgressBackgroundColorSchemeColor(Color c) (可以)

3、setColorSchemeResources(int… colorResIds):设置进度动画的颜色。

4、setRefreshing(Boolean refreshing):设置组件的刷洗状态,显示或者隐藏刷新进度条

5、setSize(int size):设置进度圈的大小,只有两个值:DEFAULT、LARGE

6、postDelayed(new Runable(),long min) 设置刷新延迟时间

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

下拉刷新

布局,具体内容如下:


<?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" >
  <ListView 
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>

Activity核心代码如下:


swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeLayout);
 
    swipeRefreshLayout.setColorSchemeResources(R.color.swipe_color_1,
        R.color.swipe_color_2,
        R.color.swipe_color_3,
        R.color.swipe_color_4);
    swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);;
    swipeRefreshLayout.setProgressBackgroundColor(R.color.swipe_background_color);
    //swipeRefreshLayout.setPadding(20, 20, 20, 20);
    //swipeRefreshLayout.setProgressViewOffset(true, 100, 200);
    //swipeRefreshLayout.setDistanceToTriggerSync(50);
    swipeRefreshLayout.setProgressViewEndTarget(true, 100);
    swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
      @Override
      public void onRefresh() {
        new Thread(new Runnable() {
          @Override
          public void run() {
            data.clear();
            for(int i=0;i<20;i++){
              data.add("SwipeRefreshLayout下拉刷新"+i);
            }
            try {
              Thread.sleep(5000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            mHandler.sendEmptyMessage(1);
          }
        }).start();
      }
    });
  //handler
  private Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      switch (msg.what) {
      case 1:
        swipeRefreshLayout.setRefreshing(false);
        adapter.notifyDataSetChanged();
        //swipeRefreshLayout.setEnabled(false);
        break;
      default:
        break;
      }
    }
  };

原生实现上拉效果

通过监听滚动事件,对listview添加底部的组件实现上拉


implements AbsListView.OnScrollListener {···
 ···
 
    footerView = getLayoutInflater().inflate(R.layout.refresh_footview_layout, null);
    lv.addFooterView(footerView);
 
  lv.setOnScrollListener(this);
  
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (adapter.getCount() == visibleLastIndex && scrollState == SCROLL_STATE_IDLE) {
      Toast.makeText(this, "加载更多完成", Toast.LENGTH_SHORT).show();
      footerView.setVisibility(View.GONE);
      
//      new LoadDataThread().start();
    }else {
      footerView.setVisibility(View.VISIBLE);
//      Toast.makeText(this, "加载更多...", Toast.LENGTH_SHORT).show();
    }
  }

2.自定义支持上拉刷新的组件

上拉刷新

实现下拉和上拉监听


···AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,
    RefreshLayout.OnLoadListener {
    ···

下拉和原先一样用法:


 //下拉监听
    swipeLayout.setOnRefreshListener(this);
    //上拉监听
    swipeLayout.setOnLoadListener(this);
     
  @Override
  public void onRefresh() {
    swipeLayout.postDelayed(new Runnable() {
      @Override
      public void run() {
        // 更新数据 更新完后调用该方法结束刷新
        list.clear();
        for (int i = 0; i < 8; i++) {
          HashMap<String, String> map = new HashMap<String, String>();
          map.put("itemImage", i + "刷新");
          map.put("itemText", i + "刷新");
          list.add(map);
        }
        adapter.notifyDataSetChanged();
        swipeLayout.setRefreshing(false);
      }
    }, 2000);
  }
  
  @Override
  public void onLoad() {
    swipeLayout.postDelayed(new Runnable() {
      @Override
      public void run() {
        // 更新数据 更新完后调用该方法结束刷新
        swipeLayout.setLoading(false);
        for (int i = 1; i < 10; i++) {
          HashMap<String, String> map = new HashMap<String, String>();
          map.put("itemImage", i + "更多");
          map.put("itemText", i + "更多");
          list.add(map);
        }
        adapter.notifyDataSetChanged();
      }
    }, 2000);
  }

自定义组件如下:




public class RefreshLayout extends SwipeRefreshLayout implements
    OnScrollListener {
  
  private int mTouchSlop;
  
  private ListView mListView;
  
  private OnLoadListener mOnLoadListener;
  
  private View mListViewFooter;
  
  private int mYDown;
  
  private int mLastY;
  
  private boolean isLoading = false;
  
  public RefreshLayout(Context context) {
    this(context, null);
  }
  @SuppressLint("InflateParams")
  public RefreshLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    mListViewFooter = LayoutInflater.from(context).inflate(
        R.layout.listview_footer, null, false);
  }
  @Override
  protected void onLayout(boolean changed, int left, int top, int right,
              int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    // 初始化ListView对象
    if (mListView == null) {
      getListView();
    }
  }
  
  private void getListView() {
    int childs = getChildCount();
    if (childs > 0) {
      View childView = getChildAt(0);
      if (childView instanceof ListView) {
        mListView = (ListView) childView;
        // 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
        mListView.setOnScrollListener(this);
        Log.d(VIEW_LOG_TAG, "### 找到listview");
      }
    }
  }
  
  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        // 按下
        mYDown = (int) event.getRawY();
        break;
      case MotionEvent.ACTION_MOVE:
        // 移动
        mLastY = (int) event.getRawY();
        break;
      case MotionEvent.ACTION_UP:
        // 抬起
        if (canLoad()) {
          loadData();
        }
        break;
      default:
        break;
    }
    return super.dispatchTouchEvent(event);
  }
  
  private boolean canLoad() {
    return isBottom() && !isLoading && isPullUp();
  }
  
  private boolean isBottom() {
    if (mListView != null && mListView.getAdapter() != null) {
      return mListView.getLastVisiblePosition() == (mListView
          .getAdapter().getCount() - 1);
    }
    return false;
  }
  
  private boolean isPullUp() {
    return (mYDown - mLastY) >= mTouchSlop;
  }
  
  private void loadData() {
    if (mOnLoadListener != null) {
      // 设置状态
      setLoading(true);
      //
      mOnLoadListener.onLoad();
    }
  }
  
  public void setLoading(boolean loading) {
    isLoading = loading;
    if (isLoading) {
      mListView.addFooterView(mListViewFooter);
    } else {
      mListView.removeFooterView(mListViewFooter);
      mYDown = 0;
      mLastY = 0;
    }
  }
  
  public void setOnLoadListener(OnLoadListener loadListener) {
    mOnLoadListener = loadListener;
  }
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
  }
  @Override
  public void onScroll(AbsListView view, int firstVisibleItem,
             int visibleItemCount, int totalItemCount) {
    // 滚动时到了最底部也可以加载更多
    if (canLoad()) {
      loadData();
    }
  }
  
  public static void setRefreshing(SwipeRefreshLayout refreshLayout,
                   boolean refreshing, boolean notify) {
    Class<? extends SwipeRefreshLayout> refreshLayoutClass = refreshLayout
        .getClass();
    if (refreshLayoutClass != null) {
      try {
        Method setRefreshing = refreshLayoutClass.getDeclaredMethod(
            "setRefreshing", boolean.class, boolean.class);
        setRefreshing.setAccessible(true);
        setRefreshing.invoke(refreshLayout, refreshing, notify);
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
  
  public static interface OnLoadListener {
    public void onLoad();
  }
}

3.自定义支持ViewPage的刷新组件VPSwipeRefreshLayout


支持viewpager刷新

原生SwipeRefreshLayout会存在以下问题:

1、 SwipeRefreshLayout会吃掉ViewPager的滑动事件。

2、 SwipeRefreshLayout需要套在ScrollView和ListView上的时候才表现的比较友好,在其他ViewGroup上有点问题

重写后的SwipeRefreshLayout,直接复制到项目就可以使用了。


public class VpSwipeRefreshLayout extends SwipeRefreshLayout {
private float startY;
private float startX;
// 记录viewPager是否拖拽的标记
private boolean mIsVpDragger;
private final int mTouchSlop;
public VpSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// 记录手指按下的位置
startY = ev.getY();
startX = ev.getX();
// 初始化标记
mIsVpDragger = false;
break;
case MotionEvent.ACTION_MOVE:
// 如果viewpager正在拖拽中,那么不拦截它的事件,直接return false;
if(mIsVpDragger) {
return false;
}
// 获取当前手指位置
float endY = ev.getY();
float endX = ev.getX();
float distanceX = Math.abs(endX - startX);
float distanceY = Math.abs(endY - startY);
// 如果X轴位移大于Y轴位移,那么将事件交给viewPager处理。
if(distanceX > mTouchSlop && distanceX > distanceY) {
mIsVpDragger = true;
return false;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// 初始化标记
mIsVpDragger = false;
break;
}
// 如果是Y轴位移大于X轴,事件交给swipeRefreshLayout处理。
return super.onInterceptTouchEvent(ev);
}
}

4.RecyclerView+SwpieRefreshLayout实现下拉刷新效果同时实现上拉功能


RecyclerView+SwpieRefreshLayout

RecyclerView实现的列表,默认情况下面是不带下拉刷新和上拉记载更多效果的,但是我在我们的实际项目当中,为了提高用户体验,这种效果一般都需要实现

SwipeRefreshLayout本身自带下拉刷新的效果,那么我们可以选择在RecyclerView布局外部嵌套一层SwipeRefreshLayout布局即可,具体布局文件如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_recycle_view_refresh"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.hsy.refresh.ui.RecycleViewRefreshActivity">
  <include
    layout="@layout/common_top_bar_layout"
    android:visibility="gone" />
  <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/demo_swiperefreshlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">
    <android.support.v7.widget.RecyclerView
      android:id="@+id/demo_recycler"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    </android.support.v7.widget.RecyclerView>
  </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

在Activity中引用这个布局并初始化


 @Override
  protected void onCreate(Bundle savedInstanceState) {
    //去除系统标题
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycle_view_refresh);
    ButterKnife.bind(this);
    initView();
  }
  
  private LinearLayoutManager linearLayoutManager;
  MyRecyclerViewAdapter adapter;
  private int lastVisibleItem;//记录滚动位置
  
  private void initView() {
        topBarTitle.setText("RecyclerView 刷新");
    //设置刷新时动画的颜色,可以设置4个
    swiperefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
    swiperefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
        android.R.color.holo_red_light, android.R.color.holo_orange_light,
        android.R.color.holo_green_light);
    // 这句话是为了,第一次进入页面的时候显示加载进度条
    swiperefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
        .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
            .getDisplayMetrics()));
    //设置竖直方向
    linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
    //设置管理器
    recylerview.setLayoutManager(linearLayoutManager);
    //添加分隔线
    recylerview.addItemDecoration(new AdvanceDecoration(this, OrientationHelper.VERTICAL));
    recylerview.setAdapter(adapter = new MyRecyclerViewAdapter(this));
    
    swiperefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
            List<String> newDatas = new ArrayList<String>();
            for (int i = 0; i < 5; i++) {
              int index = i + 1;
              newDatas.add("new item" + index);
            }
            adapter.addItem(newDatas);
            swiperefreshLayout.setRefreshing(false);
            Toast.makeText(RecycleViewRefreshActivity.this, "更新了五条数据...", Toast.LENGTH_SHORT).show();
          }
        }, 3000);
      }
    });
    
    recylerview.setOnScrollListener(new RecyclerView.OnScrollListener() {
      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        //当滚动到底部时刷新数据
        if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
          new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
              List<String> newDatas = new ArrayList<String>();
              for (int i = 0; i < 5; i++) {
                int index = i + 1;
                newDatas.add("more item" + index);
              }
              adapter.addMoreItem(newDatas);
              swiperefreshLayout.setRefreshing(false);
            }
          }, 1000);
        }
      }
      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        //获取滚动的最后位置
        lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
      }
    });
  }

实现下拉刷新用SwipeRefreshLayout 自带的进度条, 上拉刷新用类似ListView的刷新 提示“加载中”等信息。

我们可以给RecyclerView 也添加一个类似FooterView的item。

我们在Adapter中实现:



  class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {//自定义viewHoder
    List<String> datas;
    Context context;
    
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    public MyRecyclerViewAdapter(Context context) {
      this.context = context;
      
      this.datas = new ArrayList<String>();
      for (int i = 0; i < 20; i++) {
        int index = i + 1;
        datas.add("item" + index);
      }
      
    }
    //自定义的ViewHolder,持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
      public ViewHolder(View view) {
        super(view);
      }
    }
    //自定义的ViewHolder,持有每个Item的的所有界面元素
    public class ItemViewHolder extends ViewHolder {
      public TextView item_tv;
      public ItemViewHolder(View view) {
        super(view);
        item_tv = (TextView) view.findViewById(R.id.text);
      }
    }
    
    class FooterViewHolder extends ViewHolder {
      public FooterViewHolder(View view) {
        super(view);
      }
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      if (viewType == TYPE_FOOTER) {
        final View view = LayoutInflater.from(context).inflate(R.layout.refresh_footview_layout, parent, false);
//        view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
//            RecyclerView.LayoutParams.WRAP_CONTENT));
        FooterViewHolder viewHolder = new FooterViewHolder(view);
        return viewHolder;
      } else if (viewType == TYPE_ITEM) {
        final View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_layout, parent, false);
        //这边可以做一些属性设置,甚至事件监听绑定
        //view.setBackgroundColor(Color.RED);
        ItemViewHolder viewHolder = new ItemViewHolder(view);
        return viewHolder;
      }
      return null;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
      if (holder instanceof ItemViewHolder) {
        //设置数据
        ((ItemViewHolder) holder).item_tv.setText(datas.get(position));
        ((ItemViewHolder) holder).item_tv.setTag(position);
      }
    }
    
    @Override
    public int getItemViewType(int position) {
      // 最后一个item设置为footerView
      if (position + 1 == getItemCount()) {
        return TYPE_FOOTER;
      } else {
        return TYPE_ITEM;
      }
    }
    // RecyclerView的count设置为数据总条数+ 1(footerView)
    @Override
    public int getItemCount() {
      return datas.size() + 1;
    }
    //添加数据
    public void addItem(List<String> newDatas) {
      //mTitles.add(position, data);
      //notifyItemInserted(position);
      newDatas.addAll(datas);
      datas.removeAll(datas);
      datas.addAll(newDatas);
      notifyDataSetChanged();
    }
    
    public void addMoreItem(List<String> newDatas) {
      datas.addAll(newDatas);
      adapter.notifyDataSetChanged();
    }
  }

refresh_footview_layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal">
  <ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/refresh_text" />
</LinearLayout>

item_recycler_layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

项目源码:SwipeRefresh_jb51.rar

您可能感兴趣的文章:Android SwipeRefreshLayout下拉刷新组件示例android组件SwipeRefreshLayout下拉小球式刷新效果Android实现SwipeRefreshLayout首次进入自动刷新Android SwipeRefreshLayout下拉刷新源码解析Android下拉刷新SwipeRefreshLayout控件使用方法Android官方下拉刷新控件SwipeRefreshLayout使用详解Android自定义SwipeRefreshLayout高仿微信朋友圈下拉刷新Android下拉刷新控件SwipeRefreshLayout源码解析


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯