zoukankan      html  css  js  c++  java
  • 使用SwipeRefreshLayout和RecyclerView实现仿“简书”下拉刷新和上拉载入很多其它

    一、概述

    本篇博客介绍的是怎样使用SwipeRefreshLayout和RecyclerView实现高仿简书Android端的下拉刷新和上拉载入很多其它的效果。

    这里写图片描写叙述

    依据效果图能够发现,本案例实现了例如以下效果:

    • 第一次进入页面显示SwipeRefreshLayout的下拉刷新效果
    • 当内容铺满屏幕时,向下滑动显示“载入中…”效果并载入很多其它数据
    • 当SwipeRefreshLayout正在下拉刷新时,将屏蔽载入很多其它操作
    • 当载入很多其它数据时,屏蔽有可能的反复的上拉操作
    • 当向上滑动RecyclerView时。隐藏Toolbar以获得更好的用户体验

    二、代码实现

    • MainActivity
    package com.leohan.refresh;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.View;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import butterknife.ButterKnife;
    import butterknife.InjectView;
    
    /**
     * @author Leo
     */
    public class MainActivity extends AppCompatActivity {
    
    
        @InjectView(R.id.toolbar)
        Toolbar toolbar;
        @InjectView(R.id.recyclerView)
        RecyclerView recyclerView;
        @InjectView(R.id.SwipeRefreshLayout)
        SwipeRefreshLayout swipeRefreshLayout;
    
        boolean isLoading;
        private List<Map<String, Object>> data = new ArrayList<>();
        private MyAdapter adapter = new MyAdapter(this, data);
        private Handler handler = new Handler();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_notice);
            ButterKnife.inject(this);
            initView();
            initData();
        }
    
        public void initView() {
            setSupportActionBar(toolbar);
            toolbar.setTitle(R.string.notice);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
    
            swipeRefreshLayout.setColorSchemeResources(R.color.blueStatus);
            swipeRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(true);
                }
            });
    
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            data.clear();
                            getData();
                        }
                    }, 2000);
                }
            });
            final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    Log.d("test", "StateChanged = " + newState);
    
    
                }
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    Log.d("test", "onScrolled");
    
                    int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                    if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                        Log.d("test", "loading executed");
    
                        boolean isRefreshing = swipeRefreshLayout.isRefreshing();
                        if (isRefreshing) {
                            adapter.notifyItemRemoved(adapter.getItemCount());
                            return;
                        }
                        if (!isLoading) {
                            isLoading = true;
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    getData();
                                    Log.d("test", "load more completed");
                                    isLoading = false;
                                }
                            }, 1000);
                        }
                    }
                }
            });
    
            //加入点击事件
            adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Log.d("test", "item position = " + position);
                }
    
                @Override
                public void onItemLongClick(View view, int position) {
    
                }
            });
        }
    
    
        public void initData() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    getData();
                }
            }, 1500);
    
        }
    
        /**
         * 获取測试数据
         */
        private void getData() {
            for (int i = 0; i < 6; i++) {
                Map<String, Object> map = new HashMap<>();
                data.add(map);
            }
            adapter.notifyDataSetChanged();
            swipeRefreshLayout.setRefreshing(false);
            adapter.notifyItemRemoved(adapter.getItemCount());
        }
    
    
    }
    
    • RecyclerViewAdapter
    package com.leohan.refresh;
    
    import android.content.Context;
    import android.support.v7.widget.RecyclerView.Adapter;
    import android.support.v7.widget.RecyclerView.ViewHolder;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class RecyclerViewAdapter extends Adapter<ViewHolder> {
    
        private static final int TYPE_ITEM = 0;
        private static final int TYPE_FOOTER = 1;
        private Context context;
        private List data;
    
        public interface OnItemClickListener {
            void onItemClick(View view, int position);
    
            void onItemLongClick(View view, int position);
        }
    
        private OnItemClickListener onItemClickListener;
    
        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }
    
        @Override
        public int getItemCount() {
            return data.size() == 0 ?

    0 : data.size() + 1; } @Override public int getItemViewType(int position) { if (position + 1 == getItemCount()) { return TYPE_FOOTER; } else { return TYPE_ITEM; } } public RecyclerViewAdapter(Context context, List data) { this.context = context; this.data = data; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_ITEM) { View view = LayoutInflater.from(context).inflate(R.layout.item_notice, parent, false); return new ItemViewHolder(view); } else if (viewType == TYPE_FOOTER) { View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent, false); return new FootViewHolder(view); } return null; } @Override public void onBindViewHolder(final ViewHolder holder, int position) { if (holder instanceof ItemViewHolder) { //holder.tv.setText(data.get(position)); if (onItemClickListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getLayoutPosition(); onItemClickListener.onItemClick(holder.itemView, position); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int position = holder.getLayoutPosition(); onItemClickListener.onItemLongClick(holder.itemView, position); return false; } }); } } } static class ItemViewHolder extends ViewHolder { TextView tv; public ItemViewHolder(View view) { super(view); tv = (TextView) view.findViewById(R.id.tv_date); } } static class FootViewHolder extends ViewHolder { public FootViewHolder(View view) { super(view); } } }

    • item_base.xml
    <?

    xml version="1.0" encoding="utf-8"?

    > <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_10" android:layout_marginRight="@dimen/margin_10" android:foreground="?android:attr/selectableItemBackgroundBorderless" android:layout_marginTop="6dp" android:orientation="vertical" app:cardBackgroundColor="@color/line" app:cardPreventCornerOverlap="true" app:cardUseCompatPadding="true" app:contentPadding="6dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_date" style="@style/NormalTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2015-12-11 12:00" /> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:cardBackgroundColor="@color/white" app:cardPreventCornerOverlap="true" app:cardUseCompatPadding="true" app:contentPadding="10dp"> <TextView android:id="@+id/tv_title" style="@style/SmallGreyTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="2" android:text="视线好转,0729出口开通,0621进口开通。视线好转,0729出口开通,0621进口开通。" /> </android.support.v7.widget.CardView> </LinearLayout> </android.support.v7.widget.CardView>

    • item_foot.xml
    <?

    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:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:orientation="horizontal" > <ProgressBar android:layout_marginRight="6dp" android:id="@+id/progressBar" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView style="@style/SmallGreyTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/loading" /> </LinearLayout>

    三、代码分析

    • 上拉载入很多其它数据通过监听RecyclerView的滚动事件RecyclerView.OnScrollListener()实现的。它提供了两个方法:
            /**
             * 当RecyclerView的滑动状态改变时触发
             */
            public void onScrollStateChanged(RecyclerView recyclerView, int newState){}
    
            /**
             * 当RecyclerView滑动时触发
             * 相似点击事件的MotionEvent.ACTION_MOVE
             */
            public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
    • RecyclerView的滑动状态有例如以下三种:
        /**
         * The RecyclerView is not currently scrolling.
         * 手指离开屏幕
         */
        public static final int SCROLL_STATE_IDLE = 0;
    
        /**
         * The RecyclerView is currently being dragged by outside input such as user touch input.
         * 手指触摸屏幕
         */
        public static final int SCROLL_STATE_DRAGGING = 1;
    
        /**
         * The RecyclerView is currently animating to a final position while not under
         * outside control.
         * 手指加速滑动并放开,此时滑动状态伴随SCROLL_STATE_IDLE
         */
        public static final int SCROLL_STATE_SETTLING = 2;
    • 因为简书APP的上拉载入很多其它的是在滑动到最后一个item时自己主动触发的,与手指是否在屏幕上无关。即与滑动状态无关。

      因此,实现这样的效果仅仅须要在public void onScrolled(RecyclerView recyclerView, int dx, int dy) 方法中操作,无需关注当时的滑动状态:

      @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    Log.d("test", "onScrolled");
    
                    int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                    if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                        Log.d("test", "loading executed");
    
                        boolean isRefreshing = swipeRefreshLayout.isRefreshing();
                        if (isRefreshing) {
                            adapter.notifyItemRemoved(adapter.getItemCount());
                            return;
                        }
                        if (!isLoading) {
                            isLoading = true;
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    getData();
                                    Log.d("test", "load more completed");
                                    isLoading = false;
                                }
                            }, 1000);
                        }
                    }
                }
    • 假设要实现当且仅当滑动到最后一项而且手指上拉抛出时才运行上拉载入很多其它效果的话。须要配合onScrollStateChanged(RecyclerView recyclerView, int newState的使用,能够将代码改为:
     @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    Log.d("test", "StateChanged = " + newState);
                    if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                        Log.d("test", "loading executed");
    
                        boolean isRefreshing = swipeRefreshLayout.isRefreshing();
                        if (isRefreshing) {
                            adapter.notifyItemRemoved(adapter.getItemCount());
                            return;
                        }
                        if (!isLoading) {
                            isLoading = true;
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    getData();
                                    Log.d("test", "load more completed");
                                    isLoading = false;
                                }
                            }, 1000);
                        }
                    }
                }
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    Log.d("test", "onScrolled");
    
                    lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
    
                }
    • 载入很多其它的效果能够通过item_foot.xml自己定义,滑动到最后一项时显示该item并运行载入很多其它。当载入数据完成时须要将该item移除掉
    adapter.notifyItemRemoved(adapter.getItemCount());

    以下的代码就是RecyclerView的多个item布局的实现方法:

     @Override
        public int getItemCount() {
            return data.size() == 0 ? 0 : data.size() + 1;
        }
    
        @Override
        public int getItemViewType(int position) {
            if (position + 1 == getItemCount()) {
                return TYPE_FOOTER;
            } else {
                return TYPE_ITEM;
            }
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_ITEM) {
                View view = LayoutInflater.from(context).inflate(R.layout.item_base, parent,
                        false);
                return new ItemViewHolder(view);
            } else if (viewType == TYPE_FOOTER) {
                View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                        false);
                return new FootViewHolder(view);
            }
            return null;
        }

    该案例源代码:https://github.com/leoleohan/RefreshDemo,欢迎Star、Fork。

  • 相关阅读:
    [LUOGU] P2196 挖地雷
    [LUOGU] P1020 导弹拦截
    [LUOGU] P2543 [AHOI2004]奇怪的字符串
    [LUOGU] P2759 奇怪的函数
    [LUOGU] P1048 采药
    [LUOGU] P1396 营救
    [LUOGU] P1196 [NOI2002]银河英雄传说
    [LUOGU] 2820 局域网
    知识点
    swich使用
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7140572.html
Copyright © 2011-2022 走看看