lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cn.sinata.xldutils.view;
 
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
import cn.sinata.xldutils.R;
import cn.sinata.xldutils.adapter.HFRecyclerAdapter;
import cn.sinata.xldutils.utils.DensityUtil;
 
/**
 *
 * Created by liaoxiang on 16/3/28.
 */
public class SwipeRefreshRecyclerLayout extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView mRecyclerView;
    private TextView loadMoreView;
    private RecyclerView.Adapter mAdapter;
    private boolean isLoadMore = false;
    private Mode mode = Mode.Both;
    private OnRefreshListener onRefreshListener;
    private int lastVisibleItem = 0;
    private int[] lastPositions ;
 
    public SwipeRefreshRecyclerLayout(Context context) {
        super(context);
        init();
    }
 
    public SwipeRefreshRecyclerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    void init(){
        mRecyclerView = new RecyclerView(getContext());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
 
        addView(mRecyclerView);
        setOnRefreshListener(this);
    }
 
    private RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            RecyclerView.Adapter adapter =recyclerView.getAdapter();
            resetLastVisibleItem();
            if (newState == RecyclerView.SCROLL_STATE_IDLE && adapter != null
                    && lastVisibleItem + 1 == adapter.getItemCount() && !isRefreshing() && !isLoadMore) {
                if (adapter instanceof HFRecyclerAdapter){
 
                    if (loadMoreView!=null ){
                        loadMoreView.setText("载入更多...");
                        loadMoreView.setVisibility(View.VISIBLE);
                    }
                    isLoadMore = true;
                    if (onRefreshListener!=null){
                        onRefreshListener.onLoadMore();
                    }
                }
            }
        }
    };
 
    public RecyclerView getRecyclerView(){
        return mRecyclerView;
    }
 
    public void setAdapter(RecyclerView.Adapter adapter){
        mAdapter = adapter;
        mRecyclerView.setAdapter(mAdapter);
        if (mode == Mode.Both){
            initLoadMore();
        }
    }
 
    public void setLayoutManager(RecyclerView.LayoutManager layoutManager){
        mRecyclerView.setLayoutManager(layoutManager);
    }
 
    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration){
        mRecyclerView.addItemDecoration(itemDecoration);
    }
 
    public void setOnRefreshListener(OnRefreshListener onRefreshListener){
        this.onRefreshListener = onRefreshListener;
    }
 
    public void setMode(Mode mode){
        this.mode = mode;
        if (mode == Mode.Both){
            mRecyclerView.addOnScrollListener(onScrollListener);
            initLoadMore();
        }else {
            mRecyclerView.removeOnScrollListener(onScrollListener);
        }
    }
 
    private void initLoadMore(){
        if (mAdapter instanceof HFRecyclerAdapter) {
            if (loadMoreView!=null){
                return;
            }
            loadMoreView = new TextView(getContext());
            loadMoreView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//                loadMoreView.setHeight(DensityUtil.dip2px(this,48));
            loadMoreView.setBackgroundColor(Color.WHITE);
            loadMoreView.setText("载入更多...");
            loadMoreView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            loadMoreView.setTextColor(getResources().getColor(R.color.textColor));
            loadMoreView.setGravity(Gravity.CENTER);
            loadMoreView.setPadding(0, DensityUtil.dip2px(getContext(), 16), 0, DensityUtil.dip2px(getContext(), 16));
            ((HFRecyclerAdapter) mAdapter).setFooterView(loadMoreView);
        }
    }
 
    public void setLoadMoreText(CharSequence text){
        if (loadMoreView!=null){
            loadMoreView.setText(text);
            if (loadMoreView.getVisibility() != View.VISIBLE){
                loadMoreView.setVisibility(View.VISIBLE);
            }
        }
    }
 
    /**
     * 重新获取可见item的位置
     */
    private void resetLastVisibleItem(){
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager != null && layoutManager instanceof LinearLayoutManager) {
            lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        }else if (layoutManager!=null && layoutManager instanceof StaggeredGridLayoutManager){
            StaggeredGridLayoutManager staggeredGridLayoutManager =((StaggeredGridLayoutManager) layoutManager);
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItem = findMax(lastPositions);
        }
    }
 
    private int findMax(int[] lastPositions) {
        int max = lastPositions[0];
        for (int value : lastPositions) {
            if (value > max) {
                max = value;
            }
        }
        return max;
    }
 
    @Override
    public void onRefresh() {
        if (onRefreshListener!=null){
            onRefreshListener.onRefresh();
        }
    }
 
    /**
     * 设置刷新状态
     * @param refreshing 刷新状态
     */
    @Override
    public void setRefreshing(boolean refreshing){
        super.setRefreshing(refreshing);
        this.isLoadMore = refreshing;
        if (loadMoreView!=null ){
            loadMoreView.setVisibility(View.GONE);
        }
    }
 
    public enum Mode{
        Both,Top
    }
 
    public interface OnRefreshListener {
        void onRefresh();
        void onLoadMore();
    }
}