横向RecycleView,左拉更新的、切换页面

转载请标明出处: 【明明爱的博客】

概述

目前recycleview很火热,基本可以代替ListView、GridView。横向listView更是在目前很多app中有在运用,但横向recycleview模式一直往左拉数据太多交互就不舒服了,其实更好的是有个向以前下拉更新似的功能,这里就是:左拉更新,上图。

这里写图片描述

分析

其实这里要做的就是怎样滑动的时候出现左拉更新,像以前我们都是加个footView,其实可以用ScrollView做,但这里我是用RecycleView来做。

  1. recycleView怎样加上footView(默认是没有addFootView方法的)
  2. 怎样监测已经滑到最后一个
  3. 滑动的距离怎么算
  4. 滑动到最后一个时,如果我往回滑,或者手放开怎样监测
  5. 滑动时怎样做出像ios的弹簧效果

好了,带着这些疑问我们来一步步解开。
良好的编程习惯,当你对某些控件不熟悉的时候,除了百度、google之外,最好的就是去看源码。

public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild {
   }

其实RecyclerView 也是继承ViewGroup而已,说明流程还是像我们写自定义view一样的,为什么它那么强大,所以更要好好看看究竟是怎么写的,从中学习,因为我们不是为了学习RecycleView,所以我就直接说了。

    /**
     * An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
     * has occurred on that RecyclerView.
     * <p>
     * @see RecyclerView#addOnScrollListener(OnScrollListener)
     * @see RecyclerView#clearOnChildAttachStateChangeListeners()
     *
     */
    public abstract static class OnScrollListener {
   
        /**
         * Callback method to be invoked when RecyclerView's scroll state changes.
         *
         * @param recyclerView The RecyclerView whose scroll state has changed.
         * @param newState     The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
         *                     {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
         */
        public void onScrollStateChanged(RecyclerView recyclerView, int newState){}

        /**
         * Callback method to be invoked when the RecyclerView has been scrolled. This will be
         * called after the scroll has completed.
         * <p>
         * This callback will also be called if visible item range changes after a layout
         * calculation. In that case, dx and dy will be 0.
         *
         * @param recyclerView The RecyclerView which scrolled.
         * @param dx The amount of horizontal scroll.
         * @param dy The amount of vertical scroll.
         */
        public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
    }

这里的接口很像我们以前用ListView的方法,没错,滑动的距离怎么算、目前RecycleView有没有在滑动都能得出来(根据newState字段)。但这里并不能知道当滑动时MotionEvent_UP、MotionEvent_MOVE状态,这时我们很容易想到继承RecycleView来写OnTouchEvent()事件,强大的RecycleView当然会想到用户会用到滑动事件,根据主体不变扩展的设计模式,一定会有接口的。果然:

    public static interface OnItemTouchListener {
        /**
         * Silently observe and/or take over touch events sent to the RecyclerView
         * before they are handled by either the RecyclerView itself or its child views.
         *
         * <p>The onInterceptTouchEvent methods of each attached OnItemTouchListener will be run
         * in the order in which each listener was added, before any other touch processing
         * by the RecyclerView itself or child views occurs.</p>
         *
         * @param e MotionEvent describing the touch event. All coordinates are in
         *          the RecyclerView's coordinate system.
         * @return true if this OnItemTouchListener wishes to begin intercepting touch events, false
         *         to 
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android中的RecyclerView是一种非常常用的布局控件,可以实现各种各样的列表展示效果。其中,横向轮播是一种常见的布局效果,可以让多个项以横向滚动的方式展示在屏幕上。 要实现横向轮播的效果,首先我们需要在RecyclerView的LayoutManager中设置一个横向滚动的布局管理器,可以使用LinearLayoutManager,并将其方向设置为HORIZONTAL,这样列表项就会在水平方向上滚动。 实现横向轮播还需要用到RecyclerView的适配器(Adapter),我们可以自定义一个继承自RecyclerView.Adapter的适配器类,并在其中重写必要的方法,比如onCreateViewHolder()、onBindViewHolder()和getItemCount()等。在getItemCount()方法中,我们可以设置列表项的个数,根据实际需求来决定。 接下来,我们需要在onBindViewHolder()方法中将数据与列表项进行绑定。这意味着我们需要给每个列表项设置数据,比如图片、文字等。可以使用一个集合来存储需要展示的数据,在onBindViewHolder()方法中,可以根据当前列表项的位置来获取对应的数据,并将其设置到列表项控件中。 最后,在MainActivity或者其他的类中,将RecyclerView与自定义的适配器进行关联,然后将数据传递给适配器,并将适配器设置给RecyclerView。这样,当RecyclerView显示在屏幕上时,适配器会根据数据源来创建列表项,并在每次滚动时更新列表项的内容。 总之,通过设置RecyclerView的LayoutManager为横向滚动的布局管理器,并自定义一个适配器来设置数据与列表项的绑定,就可以实现Android中RecyclerView横向轮播效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值