@@ -24,7 +24,11 @@ import java.lang.ref.WeakReference |
24 | 24 | * desc : |
25 | 25 | * </pre> |
26 | 26 | */ |
27 | | -class VoteContainerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) { |
| 27 | +class VoteContainerView @JvmOverloads constructor( |
| 28 | + context: Context, |
| 29 | + attrs: AttributeSet? = null, |
| 30 | + defStyleAttr: Int = 0 |
| 31 | +) : FrameLayout(context, attrs, defStyleAttr) { |
28 | 32 |
|
29 | 33 | private var voteViewHolders: ArrayList<VoteItemViewHolder> = arrayListOf() |
30 | 34 |
|
@@ -63,7 +67,10 @@ class VoteContainerView @JvmOverloads constructor(context: Context, attrs: Attri |
63 | 67 | setVoteStatus(data) |
64 | 68 | setVoteBtnStatus() |
65 | 69 | vote_item_ll.removeAllViews() |
66 | | - data.options?.forEachIndexed { index, voteOption -> |
| 70 | + /** |
| 71 | + * 也是没有使用什么rv之类的复杂操作,这种地方没有必要这么使用 |
| 72 | + */ |
| 73 | + data.options.forEachIndexed { index, voteOption -> |
67 | 74 | val viewHolder = onCreateViewHolder() |
68 | 75 | viewHolder.bind(index, voteOption, data) |
69 | 76 | voteViewHolders.add(viewHolder) |
@@ -80,7 +87,8 @@ class VoteContainerView @JvmOverloads constructor(context: Context, attrs: Attri |
80 | 87 | val isVoteMulti = vote.choiceType == VOTE_TYPE_MULTIPLE |
81 | 88 | val voteResult = vote.sumVoteCount ?: 0 |
82 | 89 | val voted = vote.voted |
83 | | - vote_container_vote_btn.visibility = if (voted != true && isVoteMulti) View.VISIBLE else View.GONE |
| 90 | + vote_container_vote_btn.visibility = |
| 91 | + if (voted != true && isVoteMulti) View.VISIBLE else View.GONE |
84 | 92 | vote_container_vote_result.visibility = if (voted == true) View.VISIBLE else View.GONE |
85 | 93 | setVoteResult("共${voteResult}人参与了投票") |
86 | 94 | } |
@@ -168,8 +176,8 @@ class VoteContainerView @JvmOverloads constructor(context: Context, attrs: Attri |
168 | 176 | val voteResultCount = voteOption.showCount ?: 0 |
169 | 177 |
|
170 | 178 | voteView.setVoteIsSelected(voteOption.voted ?: false) |
171 | | - .setVoteContent(voteOption.content) |
172 | | - .setVoteResultText("${voteResultCount}人").refreshView() |
| 179 | + .setVoteContent(voteOption.content) |
| 180 | + .setVoteResultText("${voteResultCount}人").refreshView() |
173 | 181 |
|
174 | 182 | if (isHaveVoted()) { |
175 | 183 | val sum = mainVote?.sumVoteCount ?: 0 |
@@ -179,13 +187,17 @@ class VoteContainerView @JvmOverloads constructor(context: Context, attrs: Attri |
179 | 187 | voteView.setProgress(progress) |
180 | 188 | } |
181 | 189 |
|
| 190 | + /** |
| 191 | + * 点击事件的处理 |
| 192 | + */ |
182 | 193 | voteView.setOnClickListener { |
| 194 | + //已经投票了就不要处理了 |
183 | 195 | if (isHaveVoted()) return@setOnClickListener |
184 | 196 | if (isVoteMulti) { |
185 | 197 | setMultiChoice(voteView, voteOption) |
186 | 198 | ref()?.onVoteClickListener?.onVoteItemClick(mainVote, data) |
187 | 199 | } else { |
188 | | - if (ref()?.getOptionIdsSize() ?: 0 > 0) return@setOnClickListener |
| 200 | + if ((ref()?.getOptionIdsSize() ?: 0) > 0) return@setOnClickListener |
189 | 201 | setSingleChoice(voteView, voteOption) |
190 | 202 | ref()?.onVoteClickListener?.onVoteItemClick(mainVote, data) |
191 | 203 | } |
@@ -229,7 +241,8 @@ class VoteContainerView @JvmOverloads constructor(context: Context, attrs: Attri |
229 | 241 | voteOption.voted = true |
230 | 242 | ref()?.addOptionIds(voteOption.id ?: 0) |
231 | 243 | } else { |
232 | | - Toast.makeText(ref()?.context, "最多可选${maxSelect}个", Toast.LENGTH_SHORT).show() |
| 244 | + Toast.makeText(ref()?.context, "最多可选${maxSelect}个", Toast.LENGTH_SHORT) |
| 245 | + .show() |
233 | 246 | } |
234 | 247 | } |
235 | 248 | ref()?.setVoteBtnStatus() |
@@ -240,36 +253,42 @@ class VoteContainerView @JvmOverloads constructor(context: Context, attrs: Attri |
240 | 253 | voteOption.voted = true |
241 | 254 | ref()?.addOptionIds(voteOption.id ?: 0) |
242 | 255 | voteView.setVoteIsSelected(voteOption.voted ?: false).refreshView() |
243 | | - ref()?.onVoteClickListener?.onVoteCommitBtnClick(mainVote, ref()?.optionIds |
244 | | - ?: arrayListOf()) |
| 256 | + ref()?.onVoteClickListener?.onVoteCommitBtnClick( |
| 257 | + mainVote, ref()?.optionIds |
| 258 | + ?: arrayListOf() |
| 259 | + ) |
245 | 260 | } |
246 | 261 |
|
247 | 262 | } |
248 | 263 |
|
249 | 264 | private fun getVoteView(context: Context): VoteView { |
250 | 265 | val voteView = VoteView(context) |
251 | 266 | voteView.setVoteTextSize(voteView.sp2px(15)) |
252 | | - .setVoteUncheckedContentTextColor(resources.getColor(R.color.unchecked_content_text_color)) |
253 | | - .setVoteCheckedContentTextColor(resources.getColor(R.color.checked_content_text_color)) |
254 | | - .setVoteUncheckedResultTextColor(resources.getColor(R.color.unchecked_result_text_color)) |
255 | | - .setVoteCheckedResultTextColor(resources.getColor(R.color.checked_result_text_color)) |
256 | | - .setVoteUncheckedProgressColor(resources.getColor(R.color.unchecked_progress_color)) |
257 | | - .setVoteCheckedProgressColor(resources.getColor(R.color.checked_progress_color)) |
258 | | - .setVoteCheckedIcon(resources.getDrawable(R.mipmap.icon_vote_check)) |
259 | | - .setVoteBorderRadius(voteView.dp2px(3f)) |
260 | | - .setVoteBorderColor(resources.getColor(R.color.border_color)) |
261 | | - .setVoteRightIconSize(voteView.dp2px(18f).toInt()) |
262 | | - .setVoteAnimDuration(2000L) |
| 267 | + .setVoteUncheckedContentTextColor(resources.getColor(R.color.unchecked_content_text_color)) |
| 268 | + .setVoteCheckedContentTextColor(resources.getColor(R.color.checked_content_text_color)) |
| 269 | + .setVoteUncheckedResultTextColor(resources.getColor(R.color.unchecked_result_text_color)) |
| 270 | + .setVoteCheckedResultTextColor(resources.getColor(R.color.checked_result_text_color)) |
| 271 | + .setVoteUncheckedProgressColor(resources.getColor(R.color.unchecked_progress_color)) |
| 272 | + .setVoteCheckedProgressColor(resources.getColor(R.color.checked_progress_color)) |
| 273 | + .setVoteCheckedIcon(resources.getDrawable(R.mipmap.icon_vote_check)) |
| 274 | + .setVoteBorderRadius(voteView.dp2px(3f)) |
| 275 | + .setVoteBorderColor(resources.getColor(R.color.border_color)) |
| 276 | + .setVoteRightIconSize(voteView.dp2px(18f).toInt()) |
| 277 | + .setVoteAnimDuration(2000L) |
263 | 278 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
264 | 279 | voteView.clipToOutline = true |
| 280 | + //裁剪view的操作 |
265 | 281 | voteView.outlineProvider = object : ViewOutlineProvider() { |
266 | 282 | override fun getOutline(view: View, outline: Outline) { |
267 | 283 | outline.setRoundRect(0, 0, view.width, view.height, voteView.dp2px(3f)) |
268 | 284 | } |
269 | 285 | } |
270 | 286 | } |
271 | 287 |
|
272 | | - val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, voteView.dp2px(40f).toInt()) |
| 288 | + val layoutParams = LinearLayout.LayoutParams( |
| 289 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 290 | + voteView.dp2px(40f).toInt() |
| 291 | + ) |
273 | 292 | layoutParams.bottomMargin = voteView.dp2px(12f).toInt() |
274 | 293 | voteView.layoutParams = layoutParams |
275 | 294 | return voteView |
|