• 上拉更新,下拉刷新


    第三方控件 :https://github.com/scwang90/SmartRefreshLayout

    首先添加依赖

    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'

    //布局
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
            android:layout_below ="@id/include"
            android:id="@+id/srl_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srlEnableAutoLoadMore="false">
    //下拉刷新
            <com.scwang.smartrefresh.layout.header.ClassicsHeader
                app:srlTextRelease="@string/Refresh"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <androidx.recyclerview.widget.RecyclerView
                android:scrollbars="none"
                android:layout_below ="@id/include"
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fadeScrollbars="false"
                android:nestedScrollingEnabled="false"
                android:visibility="visible"/>
    //上拉加载更多
            <com.scwang.smartrefresh.layout.footer.ClassicsFooter
                app:srlTextRelease="@string/Release"
                app:srlTextPulling="@string/up"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </com.scwang.smartrefresh.layout.SmartRefreshLayout>
    

      

    使用框架
     /**
         * 这个是上拉刷新和加载框架
         */
        private RefreshLayout srl_layout;
    //记录刷新时间
        Date delta = null;
    //服务器获取数据的页码
        private int pageNum = 1;
     //判断是否是第一次进入
        private static boolean isFirstEnter = true;
    
    //声明
     srl_layout = findViewById(R.id.srl_layout);
    
    //需要时间的话写这个
     mClassicsHeader = (ClassicsHeader) srl_layout.getRefreshHeader();
    //获取现在的时间
            Date nowDate = new Date(System.currentTimeMillis());
    //如果是第一次进入那就记录现在的时间为下次刷新做准备
            if (isFirstEnter) {
                mClassicsHeader.setLastUpdateTime(nowDate);
                delta = nowDate;
                isFirstEnter = false;
    //不是第一次就走这个
            } else {
    如果第一次进入没有刷新那没就没有刷新时间那就用现在获取到的 否则就需要用现在获取到的时间和上次刷新的时间做减法算出上次是什么时间刷新的
                if (delta == null) {
                    mClassicsHeader.setLastUpdateTime(nowDate);
                    delta = nowDate;
                } else {
                    Date daltaDate = new Date(System.currentTimeMillis() - delta.getTime());
                    delta = daltaDate;
                    mClassicsHeader.setLastUpdateTime(daltaDate);
                }
            }
            mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA));
            mClassicsHeader.setTimeFormat(new DynamicTimeFormat("最后更新: %s"));
    
    
    //进入刷新
    srl_layout.autoRefresh();
    
    //监听事件
    //下拉刷新
    srl_layout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    myFeedback.clear();
                    pageNum = 1;
                    getData();
                    mAdapter.notifyDataSetChanged();
                    refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                }
            });
    //上拉加载更多
            srl_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(RefreshLayout refreshlayout) {
                    pageNum++;
                    getData();
                    mAdapter.notifyDataSetChanged();
                    refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                }
            });
    

      

    时间格式类(具体怎么实现不清楚这是借用别人的)

    /**
     * 动态时间格式化
     * Created by scwang on 2017/6/17.
     */
    public class DynamicTimeFormat extends SimpleDateFormat {
    
        private static Locale locale = Locale.CHINA;
        private static String weeks[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        private static String moments[] = {"中午", "凌晨", "早上", "下午", "晚上"};
    
        private String mFormat = "%s";
    
        public DynamicTimeFormat() {
            this("%s", "yyyy年", "M月d日", "HH:mm");
        }
    
        public DynamicTimeFormat(String format) {
            this();
            this.mFormat = format;
        }
    
        public DynamicTimeFormat(String yearFormat, String dateFormat, String timeFormat) {
            super(String.format(locale, "%s %s %s", yearFormat, dateFormat, timeFormat), locale);
        }
    
        public DynamicTimeFormat(String format, String yearFormat, String dateFormat, String timeFormat) {
            this(yearFormat, dateFormat, timeFormat);
            this.mFormat = format;
        }
    
        @Override
        public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {
            toAppendTo = super.format(date, toAppendTo, pos);
    
            Calendar otherCalendar = calendar;
            Calendar todayCalendar = Calendar.getInstance();
    
            int hour = otherCalendar.get(Calendar.HOUR_OF_DAY);
    
            String[] times = toAppendTo.toString().split(" ");
            String moment = hour == 12 ? moments[0] : moments[hour / 6 + 1];
            String timeFormat = moment + " " + times[2];
            String dateFormat = times[1] + " " + timeFormat;
            String yearFormat = times[0] + dateFormat;
            toAppendTo.delete(0, toAppendTo.length());
    
            boolean yearTemp = todayCalendar.get(Calendar.YEAR) == otherCalendar.get(Calendar.YEAR);
            if (yearTemp) {
                int todayMonth = todayCalendar.get(Calendar.MONTH);
                int otherMonth = otherCalendar.get(Calendar.MONTH);
                if (todayMonth == otherMonth) {//表示是同一个月
                    int temp = todayCalendar.get(Calendar.DATE) - otherCalendar.get(Calendar.DATE);
                    switch (temp) {
                        case 0:
                            toAppendTo.append(timeFormat);
                            break;
                        case 1:
                            toAppendTo.append("昨天 ");
                            toAppendTo.append(timeFormat);
                            break;
                        case 2:
                            toAppendTo.append("前天 ");
                            toAppendTo.append(timeFormat);
                            break;
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                            int dayOfMonth = otherCalendar.get(Calendar.WEEK_OF_MONTH);
                            int todayOfMonth = todayCalendar.get(Calendar.WEEK_OF_MONTH);
                            if (dayOfMonth == todayOfMonth) {//表示是同一周
                                int dayOfWeek = otherCalendar.get(Calendar.DAY_OF_WEEK);
                                if (dayOfWeek != 1) {//判断当前是不是星期日     如想显示为:周日 12:09 可去掉此判断
                                    toAppendTo.append(weeks[otherCalendar.get(Calendar.DAY_OF_WEEK) - 1]);
                                    toAppendTo.append(' ');
                                    toAppendTo.append(timeFormat);
                                } else {
                                    toAppendTo.append(dateFormat);
                                }
                            } else {
                                toAppendTo.append(dateFormat);
                            }
                            break;
                        default:
                            toAppendTo.append(dateFormat);
                            break;
                    }
                } else {
                    toAppendTo.append(dateFormat);
                }
            } else {
                toAppendTo.append(yearFormat);
            }
    
            int length = toAppendTo.length();
            toAppendTo.append(String.format(locale, mFormat, toAppendTo.toString()));
            toAppendTo.delete(0, length);
            return toAppendTo;
        }
    
    }
    

      



  • 相关阅读:
    centos6.5 升级安装pcre 8.39版本
    解决MongoDB磁盘IO问题的三种方法
    javascript 利用匿名函数对象给你异步回调方法传参数
    spring mvc fastJson 自定义类型转换(返回数据) 实现对ObjectId类型转换
    Java BigDecimal 加减乘除运算
    OKHTTP 3.0
    Chrome 开发者工具中的命令菜单
    vuex 基础:教程和说明
    RxJS 简介:可观察对象、观察者与操作符
    CreateJs入门必知必会
  • 原文地址:https://www.cnblogs.com/wang-jingyuan/p/12173975.html
Copyright © 2020-2023  润新知