Skip to content

Commit 766aff5

Browse files
author
Bonny
committed
update viewBinding
1 parent 0b85533 commit 766aff5

File tree

2 files changed

+47
-90
lines changed

2 files changed

+47
-90
lines changed

library_tagflow/src/main/java/com/framework/tagflow/MultiTagFlowLayout.kt

Lines changed: 39 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,30 @@ import android.view.LayoutInflater
1111
import android.view.View
1212
import android.view.ViewTreeObserver
1313
import android.view.animation.AccelerateDecelerateInterpolator
14-
import android.widget.*
1514
import androidx.annotation.IdRes
1615
import androidx.annotation.IntDef
1716
import androidx.constraintlayout.widget.ConstraintLayout
17+
import androidx.core.content.withStyledAttributes
18+
import androidx.core.util.isNotEmpty
19+
import androidx.core.util.size
20+
import androidx.core.view.isGone
21+
import androidx.core.view.isNotEmpty
1822
import androidx.core.view.marginBottom
1923
import androidx.core.view.marginTop
2024
import androidx.core.view.updateLayoutParams
21-
import androidx.recyclerview.widget.*
22-
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
25+
import androidx.recyclerview.widget.GridLayoutManager
26+
import androidx.recyclerview.widget.LinearLayoutManager
27+
import androidx.recyclerview.widget.RecyclerView
28+
import androidx.recyclerview.widget.SimpleItemAnimator
2329
import com.framework.tagflow.adapter.BaseTagAdapter
24-
import com.framework.tagflow.bean.BaseTagBean
30+
import com.framework.tagflow.databinding.TagFlowLayoutBinding
2531
import com.framework.tagflow.interfac.OnTagClickListener
2632
import com.framework.tagflow.interfac.OnTagSelectedListener
2733
import com.framework.tagflow.tags.MutSelectedTagView
2834
import com.framework.tagflow.tags.NonTouchableRecyclerView
2935
import com.framework.tagflow.utils.DensityUtils
30-
import com.framework.tagflow.view.ControlScrollView
3136
import com.framework.tagflow.view.FlowLayout
3237
import kotlin.math.ceil
33-
import androidx.core.view.isNotEmpty
34-
import androidx.core.content.withStyledAttributes
35-
import androidx.core.util.isNotEmpty
36-
import androidx.core.util.size
37-
import androidx.core.view.isGone
38-
import com.framework.tagflow.databinding.TagFlowLayoutBinding
39-
import kotlin.compareTo
40-
import kotlin.text.clear
41-
import kotlin.text.toFloat
4238

4339
/**
4440
* @author: xiaxueyi
@@ -63,19 +59,9 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
6359
private const val LAYOUT_TYPE_DEFAULT = 0
6460
private const val LAYOUT_MANAGER_MODE_DEFAULT = 0
6561
private const val DEFAULT_ROWS = 3
66-
private const val DEFAULT_COLUMN = 3
6762
private const val MAX_FLOW_LAYOUT_LINES = 6
6863
}
6964

70-
private lateinit var mControlScrollView: ControlScrollView //标签容器
71-
72-
private lateinit var mIvArrowMore: ImageView //箭头
73-
74-
private lateinit var mTvMoreHint: TextView //提示文本
75-
76-
private lateinit var mRlShowMore: LinearLayout //展开收起布局
77-
78-
private lateinit var mEmptyViewContainer:FrameLayout;
7965

8066
//是否折叠起来
8167
//true:折叠起来了
@@ -244,15 +230,7 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
244230
* 初始化组件
245231
*/
246232
private fun initWidget() {
247-
inflate(context, R.layout.tag_flow_layout, this)
248-
mControlScrollView = findViewById(R.id.hsv_tag_content)
249-
mIvArrowMore = findViewById(R.id.iv_arrow_more)
250-
mTvMoreHint = findViewById(R.id.tv_more_hint)
251-
mRlShowMore = findViewById(R.id.rl_show_more)
252-
mEmptyViewContainer= findViewById(R.id.fl_empty)
253-
254-
255-
mRlShowMore.setOnClickListener {
233+
mBinding.rlShowMore.setOnClickListener {
256234
//真实数量
257235
var realCount =0
258236
var finalLine = 0
@@ -320,7 +298,7 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
320298
animator.duration = animationDuration.toLong()
321299
animator.addUpdateListener { animation ->
322300
val value = animation.animatedValue as Int
323-
mControlScrollView.updateLayoutParams {
301+
mBinding.hsvTagContent.updateLayoutParams {
324302
this.height=value
325303
}
326304
}
@@ -374,14 +352,13 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
374352
} else {
375353
180f
376354
}
377-
ObjectAnimator.ofFloat(mIvArrowMore, "rotation", rotationStart, rotationEnd).start()
355+
ObjectAnimator.ofFloat(mBinding.ivArrowMore, "rotation", rotationStart, rotationEnd).start()
378356
animate(animateStart, animateEnd)
379-
mTvMoreHint.text = if (isFolded) {
357+
mBinding.tvMoreHint.text = if (isFolded) {
380358
expandHint
381359
} else {
382360
foldHint
383361
}
384-
mControlScrollView.setCanScroll(false)
385362
isFolded = !isFolded
386363
}
387364

@@ -414,7 +391,7 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
414391
mRecyclerView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
415392

416393
override fun onGlobalLayout() {
417-
mControlScrollView.updateLayoutParams {
394+
mBinding.hsvTagContent.updateLayoutParams {
418395
this.height = calculateControlScrollViewHeight(itemCount) //计算 ControlScrollView 的高度
419396
}
420397
mRecyclerView.viewTreeObserver.removeOnGlobalLayoutListener(this)
@@ -434,22 +411,22 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
434411
override fun onGlobalLayout() {
435412
val flowLayoutLines = mFlowLayout.getLineSize()
436413
val controlScrollViewHeight = if (mHasMore) { // 有展开更多的布局
437-
if (mRlShowMore.isGone){
438-
mRlShowMore.visibility = VISIBLE
439-
mRlShowMore.isClickable = true
414+
if (mBinding.rlShowMore.isGone){
415+
mBinding.rlShowMore.visibility = VISIBLE
416+
mBinding.rlShowMore.isClickable = true
440417
}
441418
if (flowLayoutLines <= DEFAULT_ROWS) {
442419
getLineHeight() * flowLayoutLines
443420
} else {
444421
getLineHeight() * DEFAULT_ROWS
445422
}
446423
} else { // 没有展开更多的布局
447-
mRlShowMore.visibility = View.GONE
448-
mRlShowMore.isClickable = false // 避免点击事件
424+
mBinding.rlShowMore.visibility = View.GONE
425+
mBinding.rlShowMore.isClickable = false // 避免点击事件
449426
getLineHeight() * flowLayoutLines
450427
}
451428

452-
mControlScrollView.updateLayoutParams {
429+
mBinding.hsvTagContent.updateLayoutParams {
453430
this.height=controlScrollViewHeight
454431
}
455432
mFlowLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
@@ -515,16 +492,16 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
515492
* 实例化布局类型
516493
*/
517494
private fun initLayoutType() {
518-
if (mControlScrollView.isNotEmpty()) {
519-
mControlScrollView.removeAllViews()
495+
if (mBinding.hsvTagContent.isNotEmpty()) {
496+
mBinding.hsvTagContent.removeAllViews()
520497
}
521498
when (mLayoutType) {
522499
LayoutTypeMode.RecyclerView.index -> {
523-
mControlScrollView.addView(mRecyclerView)
500+
mBinding.hsvTagContent.addView(mRecyclerView)
524501
}
525502

526503
LayoutTypeMode.FlowLayout.index -> {
527-
mControlScrollView.addView(mFlowLayout)
504+
mBinding.hsvTagContent.addView(mFlowLayout)
528505
}
529506
}
530507
}
@@ -762,17 +739,17 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
762739
*
763740
*/
764741
private fun updateLayoutForEmptyData() {
765-
mControlScrollView.updateLayoutParams {
742+
mBinding.hsvTagContent.updateLayoutParams {
766743
this.height = 0
767744
}
768-
val childCount:Int= mEmptyViewContainer.childCount
745+
val childCount:Int= mBinding.flEmpty.childCount
769746
if(childCount>0){
770-
mEmptyViewContainer.removeAllViews()
747+
mBinding.flEmpty.removeAllViews()
771748
}
772-
mEmptyViewContainer.addView(mEmptyView)
773-
mEmptyViewContainer.visibility= VISIBLE
774-
mRlShowMore.visibility = INVISIBLE
775-
mRlShowMore.isClickable = false
749+
mBinding.flEmpty.addView(mEmptyView)
750+
mBinding.flEmpty.visibility= VISIBLE
751+
mBinding.rlShowMore.visibility = INVISIBLE
752+
mBinding.rlShowMore.isClickable = false
776753
}
777754

778755

@@ -798,10 +775,10 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
798775
val controlScrollViewHeight= if (mHasMore) { // 有展开更多的布局
799776
if (isNotifyData) {
800777
if (finalLine <= 3) {
801-
mRlShowMore.visibility = GONE
778+
mBinding.rlShowMore.visibility = GONE
802779
getLineHeight() * finalLine
803780
} else {
804-
mRlShowMore.visibility = VISIBLE
781+
mBinding.rlShowMore.visibility = VISIBLE
805782
if (isFolded) {
806783
getLineHeight() * finalLine
807784
} else {
@@ -810,15 +787,15 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
810787
}
811788
} else {
812789
if (finalLine <= 3) {
813-
mRlShowMore.visibility = GONE
790+
mBinding.rlShowMore.visibility = GONE
814791
getLineHeight() * finalLine
815792
} else {
816-
mRlShowMore.visibility = VISIBLE
793+
mBinding.rlShowMore.visibility = VISIBLE
817794
getLineHeight() * defaultRows
818795
}
819796
}
820797
} else { // 没有展开更多的布局
821-
mRlShowMore.visibility = GONE
798+
mBinding.rlShowMore.visibility = GONE
822799
getLineHeight() * itemCount // 使用之前获取的 itemCount
823800
}
824801

@@ -859,7 +836,7 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
859836
fun setFoldHint(foldHint: String) {
860837
this.foldHint = foldHint
861838
if (isFolded) {
862-
mTvMoreHint.text = foldHint
839+
mBinding.tvMoreHint.text = foldHint
863840
}
864841
}
865842

@@ -880,7 +857,7 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
880857
fun setExpandHint(expandHint: String) {
881858
this.expandHint = expandHint
882859
if (!isFolded) {
883-
mTvMoreHint.text = expandHint
860+
mBinding.tvMoreHint.text = expandHint
884861
}
885862
}
886863

@@ -901,25 +878,6 @@ open class MultiTagFlowLayout @JvmOverloads constructor(
901878
this.animationDuration = animationDuration
902879
}
903880

904-
/**
905-
*
906-
* @return Boolean
907-
*/
908-
fun isCanScroll(): Boolean {
909-
return mControlScrollView.isCanScroll()
910-
}
911-
912-
/**
913-
* 设置是否退出内部标签容器
914-
* @param can Boolean
915-
*/
916-
fun setCanScroll(can: Boolean) {
917-
mControlScrollView.setCanScroll(can)
918-
}
919-
920-
fun setForceFixed(fixed: Boolean) {
921-
mControlScrollView.setForceFixed(fixed)
922-
}
923881

924882
/**
925883
* 获取FlowLayout 点击模式

library_tagflow/src/main/res/layout/tag_flow_layout.xml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
<merge xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
5-
android:layout_height="wrap_content">
5+
android:layout_height="wrap_content"
6+
tools:parentTag="android.widget.LinearLayout">
67

78
<LinearLayout
8-
android:id="@+id/ll_tag_layout"
99
android:layout_width="match_parent"
1010
android:layout_height="wrap_content"
1111
android:orientation="vertical">
1212

1313
<FrameLayout
14-
android:id="@+id/fl_empty"
14+
android:id="@+id/flEmpty"
1515
android:layout_width="match_parent"
1616
android:layout_height="match_parent"
1717
android:layout_gravity="center"
1818
android:visibility="gone"/>
1919

20-
<com.framework.tagflow.view.ControlScrollView
21-
android:id="@+id/hsv_tag_content"
20+
<FrameLayout
21+
android:id="@+id/hsvTagContent"
2222
android:layout_width="match_parent"
2323
android:layout_height="wrap_content"
24-
android:scrollbars="none"
2524
tools:layout_height="@dimen/dp_105"/>
2625

2726
<LinearLayout
28-
android:id="@+id/rl_show_more"
27+
android:id="@+id/rlShowMore"
2928
android:orientation="vertical"
3029
android:layout_width="wrap_content"
3130
android:layout_height="wrap_content"
@@ -46,7 +45,7 @@
4645
android:background="@drawable/tag_flow_layout_arrow_bg">
4746

4847
<TextView
49-
android:id="@+id/tv_more_hint"
48+
android:id="@+id/tvMoreHint"
5049
android:layout_width="wrap_content"
5150
android:layout_height="wrap_content"
5251
android:text="@string/foldHint"
@@ -60,7 +59,7 @@
6059
android:visibility="visible" />
6160

6261
<ImageView
63-
android:id="@+id/iv_arrow_more"
62+
android:id="@+id/ivArrowMore"
6463
android:layout_width="wrap_content"
6564
android:layout_height="wrap_content"
6665
android:layout_gravity="center"

0 commit comments

Comments
 (0)