/
MultiModeFragment.java
207 lines (175 loc) · 7.94 KB
/
MultiModeFragment.java
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
package me.khrystal.circlerecyclerviewdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
//import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import me.khrystal.library.widget.CircleRecyclerView;
import me.khrystal.library.widget.CircularHorizontalMode;
import me.khrystal.library.widget.CircularHorizontalBTTMode;
import me.khrystal.library.widget.CircularViewMode;
import me.khrystal.library.widget.CircularViewRTLMode;
import me.khrystal.library.widget.ItemViewMode;
import me.khrystal.library.widget.RotateXScaleYViewMode;
import me.khrystal.library.widget.RotateYScaleXViewMode;
import me.khrystal.library.widget.ScaleXViewMode;
import me.khrystal.library.widget.ScaleYViewMode;
/**
* usage:
* author: kHRYSTAL
* create time: 16/9/15
* update time:
* email: 723526676@qq.com
*/
public class MultiModeFragment extends Fragment {
private CircleRecyclerView mCircleRecyclerView;
private ItemViewMode mItemViewMode;
private LinearLayoutManager mLayoutManager;
private List<Integer> mImgList;
private boolean mIsNotLoop;
private Integer[] mImgs = {
R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8,
R.drawable.img_9, R.drawable.img_10, R.drawable.img_11, R.drawable.img_12
};
public static MultiModeFragment newInstance(@ModeType.ModeTypeChecker int modeType) {
MultiModeFragment fragment = new MultiModeFragment();
Bundle bundle = new Bundle();
bundle.putInt("mode_type", modeType);
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int modeType = getArguments().getInt("mode_type");
mCircleRecyclerView = view.findViewById(R.id.circle_rv);
// find itemViewMode and layoutManager
switch (modeType) {
case 1:
mItemViewMode = new CircularViewMode();
mLayoutManager = new LinearLayoutManager(getContext());
break;
case 2:
mItemViewMode = new ScaleXViewMode();
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
break;
case 3:
mItemViewMode = new ScaleYViewMode();
mLayoutManager = new LinearLayoutManager(getContext());
break;
case 4:
mItemViewMode = new RotateXScaleYViewMode();
mLayoutManager = new LinearLayoutManager(getContext());
break;
case 5:
mItemViewMode = new RotateYScaleXViewMode();
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
break;
case 6:
mItemViewMode = new CircularViewMode();
mLayoutManager = new LinearLayoutManager(getContext());
mIsNotLoop = true;
break;
case 7:
mItemViewMode = new CircularHorizontalMode();
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
break;
case 8:
mItemViewMode = new CircularViewRTLMode();
mLayoutManager = new LinearLayoutManager(getContext());
break;
case 9:
// notice when use horizontal layoutManager and item in recyclerView bottom
// the item xml file is not useful set marginTop and set alginParentBottom,
// u can use the first argument to control the item position of the recyclerView
// and this offset need use some density util to resolve adaptation
// and the offset is relative recyclerView top abs distance
mItemViewMode = new CircularHorizontalBTTMode(600, false);
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
break;
}
mCircleRecyclerView.setLayoutManager(mLayoutManager);
mCircleRecyclerView.setViewMode(mItemViewMode);
mCircleRecyclerView.setNeedCenterForce(true);
mCircleRecyclerView.setNeedLoop(!mIsNotLoop);
mCircleRecyclerView.setOnCenterItemClickListener(new CircleRecyclerView.OnCenterItemClickListener() {
@Override
public void onCenterItemClick(View v) {
Toast.makeText(getContext(), "Center Clicked", Toast.LENGTH_SHORT).show();
}
});
mImgList = Arrays.asList(mImgs);
Collections.shuffle(mImgList);
mCircleRecyclerView.setAdapter(new A());
}
class A extends RecyclerView.Adapter<VH> {
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
VH h = null;
if (mCircleRecyclerView.getLayoutManager().canScrollHorizontally()) {
h = new VH(LayoutInflater.from(getContext())
.inflate(R.layout.item_h, parent, false));
} else if (mCircleRecyclerView.getLayoutManager().canScrollVertically()) {
if (mItemViewMode instanceof CircularViewMode)
h = new VH(LayoutInflater.from(getContext())
.inflate(R.layout.item_c_v, parent, false));
else if (mItemViewMode instanceof CircularViewRTLMode)
h = new VH(LayoutInflater.from(getContext())
.inflate(R.layout.item_c_rtl_v, parent, false));
else if (mItemViewMode instanceof CircularHorizontalBTTMode) {
h = new VH(LayoutInflater.from(getContext())
.inflate(R.layout.item_h_btt, parent, false));
}
else
h = new VH(LayoutInflater.from(getContext())
.inflate(R.layout.item_v, parent, false));
}
return h;
}
@Override
public void onBindViewHolder(VH holder, int position) {
holder.tv.setText("Number :" + (position % mImgList.size()));
// Glide.with(requireContext())
// .load(mImgList.get(position % mImgList.size()))
// .apply(RequestOptions.bitmapTransform(new BlurTransformation()))
// .into(holder.iv);
Glide.with(requireContext())
.load(mImgList.get(position % mImgList.size()))
.apply(RequestOptions.circleCropTransform())
.into(holder.iv);
}
@Override
public int getItemCount() {
return mIsNotLoop ? mImgList.size() : Integer.MAX_VALUE;
}
}
static class VH extends RecyclerView.ViewHolder {
TextView tv;
ImageView iv;
public VH(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.item_text);
iv = itemView.findViewById(R.id.item_img);
}
}
}