kimi
2021-02-18 749a5510a9f014446a3cd6ba57b3cb0cc8148dc1
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package com.mylhyl.circledialog.view;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
 
import com.mylhyl.circledialog.internal.BackgroundHelper;
import com.mylhyl.circledialog.internal.Controller;
import com.mylhyl.circledialog.callback.CircleItemLabel;
import com.mylhyl.circledialog.callback.CircleItemViewBinder;
import com.mylhyl.circledialog.params.DialogParams;
import com.mylhyl.circledialog.params.ItemsParams;
import com.mylhyl.circledialog.res.drawable.CircleDrawableSelector;
import com.mylhyl.circledialog.res.values.CircleColor;
import com.mylhyl.circledialog.view.listener.ItemsView;
import com.mylhyl.circledialog.view.listener.OnRvItemClickListener;
 
import java.util.Arrays;
import java.util.List;
 
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
 
/**
 * Created by hupei on 2018/4/18.
 */
 
class BodyRecyclerView extends RecyclerView implements ItemsView {
    protected DialogParams mDialogParams;
    private ItemsParams mItemsParams;
    private Adapter mAdapter;
    private LayoutManager mLayoutManager;
    private Context mContext;
 
    public BodyRecyclerView(Context context, ItemsParams itemsParams, DialogParams dialogParams) {
        super(context);
        mContext = context;
        this.mItemsParams = itemsParams;
        this.mDialogParams = dialogParams;
        init();
    }
 
    @Override
    public void refreshItems() {
        mAdapter.notifyDataSetChanged();
    }
 
    @Override
    public void regOnItemClickListener(AdapterView.OnItemClickListener listener) {
 
    }
 
    @Override
    public void regOnItemClickListener(OnRvItemClickListener listener) {
        if (mAdapter != null && mAdapter instanceof ItemsAdapter) {
            ((ItemsAdapter) mAdapter).setOnItemClickListener(listener);
        }
    }
 
    @Override
    public View getView() {
        return this;
    }
 
    private void init() {
        configBackground();
        createLayoutManager();
        createItemDecoration();
        createAdapter();
    }
 
    private void configBackground() {
        //如果没有背景色,则使用默认色
        int backgroundColor = mItemsParams.backgroundColor != 0 ?
                mItemsParams.backgroundColor : mDialogParams.backgroundColor;
        setBackgroundColor(backgroundColor);
    }
 
    private void createLayoutManager() {
 
        if (mItemsParams.layoutManager == null) {
            mLayoutManager = new LinearLayoutManager(mContext, mItemsParams.linearLayoutManagerOrientation, false);
        } else if (mItemsParams.layoutManager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager staggeredGridLayoutManager =
                    (StaggeredGridLayoutManager) mItemsParams.layoutManager;
            mLayoutManager = new StaggeredGridLayoutManagerWrapper(staggeredGridLayoutManager);
        } else if (mItemsParams.layoutManager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) mItemsParams.layoutManager;
            if (gridLayoutManager.getSpanCount() == 1) {
                mLayoutManager = new LinearLayoutManager(mContext, mItemsParams.linearLayoutManagerOrientation, false);
            } else {
                mLayoutManager = new GridLayoutManagerWrapper(mContext, gridLayoutManager);
            }
        } else if (mItemsParams.layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mItemsParams.layoutManager;
            mLayoutManager = new LinearLayoutManagerWrapper(mContext, linearLayoutManager);
        } else {
            mLayoutManager = mItemsParams.layoutManager;
        }
        setLayoutManager(mLayoutManager);
        setHasFixedSize(true);
    }
 
    private void createItemDecoration() {
        if (mItemsParams.dividerHeight > 0 && (mLayoutManager instanceof LayoutManager)) {
            if (mLayoutManager instanceof GridLayoutManager
                    && mItemsParams.itemDecoration == null) {
                mItemsParams.itemDecoration = new GridItemDecoration(new ColorDrawable(CircleColor.divider),
                        Controller.dp2px(mContext, mItemsParams.dividerHeight));
            } else if (mLayoutManager instanceof LinearLayoutManager
                    && mItemsParams.itemDecoration == null) {
                int orientation = ((LinearLayoutManager) mLayoutManager).getOrientation();
                mItemsParams.itemDecoration = new LinearItemDecoration(new ColorDrawable(CircleColor.divider),
                        Controller.dp2px(mContext, mItemsParams.dividerHeight), orientation);
            }
            addItemDecoration(mItemsParams.itemDecoration);
        }
    }
 
    private void createAdapter() {
        mAdapter = mItemsParams.adapterRv;
        if (mAdapter == null) {
            mAdapter = new ItemsAdapter(mContext, mItemsParams, mDialogParams, mLayoutManager);
            if (mLayoutManager instanceof GridLayoutManager) {
                final GridLayoutManager gridLayoutManager = (GridLayoutManager) mLayoutManager;
                if (gridLayoutManager.getSpanSizeLookup() instanceof GridLayoutManager.DefaultSpanSizeLookup) {
                    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                        @Override
                        public int getSpanSize(int position) {
                            int itemCount = mAdapter.getItemCount();
                            int spanCount = gridLayoutManager.getSpanCount();
                            int mod = itemCount % spanCount;
                            if (mod == 0 || position < itemCount - 1) {
                                return 1;
                            } else {
                                return spanCount - mod + 1;
                            }
                        }
                    });
                }
            }
        }
        setAdapter(mAdapter);
    }
 
    static class ItemsAdapter<T> extends Adapter<ItemsAdapter.Holder> {
        private OnRvItemClickListener mItemClickListener;
        private Context mContext;
        private List<T> mItems;
        private ItemsParams mItemsParams;
        private int mBackgroundColorPress;
        private LayoutManager mLayoutManager;
 
        public ItemsAdapter(Context context, ItemsParams itemsParams, DialogParams dialogParams,
                            LayoutManager layoutManager) {
            this.mContext = context;
            this.mItemsParams = itemsParams;
            this.mLayoutManager = layoutManager;
            mBackgroundColorPress = itemsParams.backgroundColorPress != 0 ?
                    itemsParams.backgroundColorPress : dialogParams.backgroundColorPress;
            Object entity = itemsParams.items;
            if (entity != null && entity instanceof Iterable) {
                this.mItems = (List<T>) entity;
            } else if (entity != null && entity.getClass().isArray()) {
                this.mItems = Arrays.asList((T[]) entity);
            } else if (entity != null) {
                throw new IllegalArgumentException("entity must be an Array or an Iterable.");
            }
        }
 
        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView textView = new TextView(mContext);
            textView.setGravity(Gravity.CENTER);
            if (mLayoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mLayoutManager;
                if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
                    textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    if (mItemsParams.padding != null) {
                        textView.setPadding(Controller.dp2px(mContext, mItemsParams.padding[0]),
                                Controller.dp2px(mContext, mItemsParams.padding[1]),
                                Controller.dp2px(mContext, mItemsParams.padding[2]),
                                Controller.dp2px(mContext, mItemsParams.padding[3]));
                    } else {
                        textView.setPadding(10, 0, 10, 0);
                    }
                } else {
                    if (mItemsParams.padding != null) {
                        textView.setPadding(Controller.dp2px(mContext, mItemsParams.padding[0]),
                                Controller.dp2px(mContext, mItemsParams.padding[1]),
                                Controller.dp2px(mContext, mItemsParams.padding[2]),
                                Controller.dp2px(mContext, mItemsParams.padding[3]));
                    }
                    textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                }
            } else if (mLayoutManager instanceof StaggeredGridLayoutManager || mLayoutManager instanceof GridLayoutManager) {
                textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            } else {
                textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            }
            textView.setTextSize(mItemsParams.textSize);
            textView.setTextColor(mItemsParams.textColor);
            if (mItemsParams.textGravity != Gravity.NO_GRAVITY) {
                textView.setGravity(mItemsParams.textGravity);
            }
            textView.setHeight(Controller.dp2px(mContext, mItemsParams.itemHeight));
            Holder holder = new Holder(textView, mItemClickListener);
            return holder;
        }
 
        @Override
        public void onBindViewHolder(Holder holder, int position) {
            CircleDrawableSelector circleDrawableSelector = new CircleDrawableSelector(Color.TRANSPARENT,
                    mBackgroundColorPress);
            BackgroundHelper.handleBackground(holder.item, circleDrawableSelector);
 
            String label;
            T item = mItems.get(position);
            if (item instanceof CircleItemLabel) {
                label = ((CircleItemLabel) item).getItemLabel();
            } else {
                label = item.toString();
            }
            holder.item.setText(String.valueOf(label));
 
            CircleItemViewBinder viewBinder = mItemsParams.viewBinder;
            if (viewBinder != null) {
                viewBinder.onBinder(holder.item, item, position);
            }
        }
 
        @Override
        public int getItemCount() {
            return mItems == null ? 0 : mItems.size();
        }
 
        public void setOnItemClickListener(OnRvItemClickListener listener) {
            this.mItemClickListener = listener;
        }
 
        static class Holder extends RecyclerView.ViewHolder implements OnClickListener {
            OnRvItemClickListener mOnRvItemClickListener;
            TextView item;
 
            public Holder(TextView itemView, OnRvItemClickListener listener) {
                super(itemView);
                item = itemView;
                mOnRvItemClickListener = listener;
                itemView.setOnClickListener(this);
            }
 
            @Override
            public void onClick(View v) {
                if (mOnRvItemClickListener != null) {
                    mOnRvItemClickListener.onItemClick(v, getAdapterPosition());
                }
            }
        }
    }
 
    static class GridItemDecoration extends RecyclerView.ItemDecoration {
 
        private Drawable mDivider;
        private int mDividerHeight;
 
        public GridItemDecoration(Drawable divider, int dividerHeight) {
            mDivider = divider;
            mDividerHeight = dividerHeight;
        }
 
        private static boolean isLastColumn(RecyclerView parent, int pos, int spanCount, int childCount) {
            LayoutManager layoutManager = parent.getLayoutManager();
            if (layoutManager instanceof GridLayoutManager) {
                // 如果是最后一列,则不需要绘制右边
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
                if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                    // 如果是最后一列,则不需要绘制右边
                    if ((pos + 1) % spanCount == 0) {
                        return true;
                    }
                } else {
                    childCount = childCount - childCount % spanCount;
                    // 如果是最后一列,则不需要绘制右边
                    if (pos >= childCount) {
                        return true;
                    }
                }
            }
            return false;
        }
 
        private static boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
            LayoutManager layoutManager = parent.getLayoutManager();
            if (layoutManager instanceof GridLayoutManager) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行,则不需要绘制底部
                if (pos >= childCount) {
                    return true;
                }
            } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
                // StaggeredGridLayoutManager 且纵向滚动
                if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                    childCount = childCount - childCount % spanCount;
                    // 如果是最后一行,则不需要绘制底部
                    if (pos >= childCount) {
                        return true;
                    }
                }
                // StaggeredGridLayoutManager 且横向滚动
                else {
                    // 如果是最后一行,则不需要绘制底部
                    if ((pos + 1) % spanCount == 0) {
                        return true;
                    }
                }
            }
            return false;
        }
 
        private static int getSpanCount(RecyclerView parent) {
            // 列数
            int spanCount;
            LayoutManager layoutManager = parent.getLayoutManager();
            if (layoutManager instanceof GridLayoutManager) {
                spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
            } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
            } else {
                spanCount = layoutManager.getItemCount();
            }
            return spanCount;
        }
 
        @Override
        public void onDraw(Canvas c, RecyclerView parent, State state) {
            drawHorizontal(c, parent);
            drawVertical(c, parent);
        }
 
        @Override
        public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
            int spanCount = getSpanCount(parent);
            int childCount = parent.getAdapter().getItemCount();
            // 如果是最后一行,则不需要绘制底部
            if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
                outRect.set(0, 0, mDividerHeight, 0);
            }
            // 如果是最后一列,则不需要绘制右边
            else if (isLastColumn(parent, itemPosition, spanCount, childCount)) {
                outRect.set(0, 0, 0, mDividerHeight);
            } else {
                outRect.set(0, 0, mDividerHeight, mDividerHeight);
            }
        }
 
        private void drawHorizontal(Canvas c, RecyclerView parent) {
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int left = child.getLeft() - params.leftMargin;
                final int top = child.getBottom() + params.bottomMargin;
                final int right = child.getRight() + params.rightMargin + mDividerHeight;
                final int bottom = top + mDividerHeight;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
 
        private void drawVertical(Canvas c, RecyclerView parent) {
            final int childCount = parent.getChildCount() - 1;
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
 
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int left = child.getRight() + params.rightMargin;
                final int top = child.getTop() - params.topMargin;
                final int right = left + mDividerHeight;
                final int bottom = child.getBottom() + params.bottomMargin;
 
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }
 
    static class LinearItemDecoration extends RecyclerView.ItemDecoration {
 
        private Drawable mDivider;
        private int mDividerHeight;
        private int mOrientation;
 
        public LinearItemDecoration(Drawable divider, int dividerHeight, int orientation) {
            this.mDivider = divider;
            this.mDividerHeight = dividerHeight;
            this.mOrientation = orientation;
        }
 
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            if (mOrientation == LinearLayoutManager.VERTICAL) {
                drawVertical(c, parent);
            } else {
                drawHorizontal(c, parent);
            }
        }
 
        @Override
        public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
            if (mOrientation == LinearLayoutManager.VERTICAL)
                outRect.set(0, 0, 0, mDividerHeight);
            else {
                //最后一列不画
                if (itemPosition != parent.getAdapter().getItemCount() - 1) {
                    outRect.set(0, 0, mDividerHeight, 0);
                }
            }
        }
 
        private void drawVertical(Canvas c, RecyclerView parent) {
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            int childCount = parent.getChildCount() - 1;
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                LayoutParams params = (LayoutParams) child.getLayoutParams();
                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + mDividerHeight;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
 
        private void drawHorizontal(Canvas c, RecyclerView parent) {
            final int top = parent.getPaddingTop();
            final int bottom = parent.getHeight() - parent.getPaddingBottom();
            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int left = child.getRight() + params.rightMargin;
                final int right = left + mDividerHeight;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
 
 
    }
 
    static class LinearLayoutManagerWrapper extends LinearLayoutManager {
 
        public LinearLayoutManagerWrapper(Context context, LinearLayoutManager layoutManager) {
            super(context, layoutManager.getOrientation(), layoutManager.getReverseLayout());
        }
    }
 
    static class GridLayoutManagerWrapper extends GridLayoutManager {
 
        public GridLayoutManagerWrapper(Context context, GridLayoutManager layoutManager) {
            super(context, layoutManager.getSpanCount(), layoutManager.getOrientation(),
                    layoutManager.getReverseLayout());
        }
    }
 
    static class StaggeredGridLayoutManagerWrapper extends StaggeredGridLayoutManager {
 
 
        public StaggeredGridLayoutManagerWrapper(StaggeredGridLayoutManager layoutManager) {
            super(layoutManager.getSpanCount(), layoutManager.getOrientation());
        }
    }
}