@@ -43,8 +43,6 @@ public class FaceTextInputLayout extends LinearLayout {
43
43
44
44
private int mFaceTextViewRightMargin ;
45
45
46
- private int mFaceTextViewHeight ;
47
-
48
46
private LinearLayout .LayoutParams mFaceTextContainerLayoutParams ;
49
47
50
48
private TextView mTargetFaceTextView ; // 用于测量颜文字长度的“TextView”
@@ -81,17 +79,25 @@ public void setOnFaceTextClickListener(OnFaceTextClickListener onFaceTextClickLi
81
79
}
82
80
83
81
public void updateUI () {
84
- // 如果用户未设置“颜文字source”,辣么 return
85
- if (mFaceTextProvider == null ) {
86
- return ;
87
- }
88
82
89
- // TODO: 生成页面在主线程,需要放到非 UI线程
90
- List <RecyclerView > allPageList = generateAllPage ();
91
- mMyPagerAdapter .setFaceTextInputPageList (allPageList );
92
- mViewPager .setOffscreenPageLimit (mMyPagerAdapter .getCount ());
93
- mViewPager .setAdapter (mMyPagerAdapter );
94
- mDotViewLayout .setViewPager (mViewPager );
83
+ // 利用 View 的 post 方法机智地获取 View 的高度
84
+ post (new Runnable () {
85
+ @ Override public void run () {
86
+
87
+ if (mFaceTextProvider == null ) { // 如果用户未设置“颜文字source”,抛出异常
88
+ throw new IllegalStateException ("mFaceTextProvider can't be null !!!" );
89
+ }
90
+
91
+ mFaceTextContainerLayoutParams = generateFaceTextContainerLayoutParams ();
92
+
93
+ // TODO: 生成页面在主线程,需要放到非 UI线程
94
+ List <RecyclerView > allPageList = generateAllPage ();
95
+ mMyPagerAdapter .setFaceTextInputPageList (allPageList );
96
+ mViewPager .setOffscreenPageLimit (mMyPagerAdapter .getCount ());
97
+ mViewPager .setAdapter (mMyPagerAdapter );
98
+ mDotViewLayout .setViewPager (mViewPager );
99
+ }
100
+ });
95
101
}
96
102
97
103
@ Override protected void onDetachedFromWindow () {
@@ -144,12 +150,6 @@ private void applyXMLAttributes(AttributeSet attrs, int defStyleAttr, int defSty
144
150
typedArray .getResourceId (R .styleable .FaceTextInputLayout_faceTextViewLeftMargin ,
145
151
DensityUtil .dip2px (getContext (), 2 ));
146
152
147
- mFaceTextViewHeight =
148
- typedArray .getResourceId (R .styleable .FaceTextInputLayout_faceTextViewLeftMargin ,
149
- DensityUtil .dip2px (getContext (), 48 ));
150
-
151
- mFaceTextContainerLayoutParams = generateFaceTextContainerLayoutParams ();
152
-
153
153
typedArray .recycle ();
154
154
}
155
155
@@ -251,13 +251,25 @@ private RecyclerView generateEachPage(List<List<FaceText>> faceTextList) {
251
251
* 生成每个“颜文字” item 对应的 layoutParams
252
252
*/
253
253
private LinearLayout .LayoutParams generateFaceTextContainerLayoutParams () {
254
+
254
255
LinearLayout .LayoutParams layoutParams =
255
- new LinearLayout .LayoutParams (0 , mFaceTextViewHeight , 1.0f );
256
+ new LinearLayout .LayoutParams (0 , calculateFaceTextViewHeight () , 1.0F );
256
257
layoutParams .leftMargin = mFaceTextViewLeftMargin ;
257
258
layoutParams .rightMargin = mFaceTextViewRightMargin ;
258
259
return layoutParams ;
259
260
}
260
261
262
+ /**
263
+ * 计算“颜文字”的高度,父容器的高度减去竖直方向上的 padding(2 * 4dp)和底部的留白(8dp)
264
+ *
265
+ * @return “颜文字item”的高度
266
+ */
267
+ private int calculateFaceTextViewHeight () {
268
+ return getMeasuredHeight () / PAGE_MAX_LINE_NUM
269
+ - 2 * DensityUtil .dip2px (getContext (), 4.0F )
270
+ - DensityUtil .dip2px (getContext (), 8.0F );
271
+ }
272
+
261
273
/**
262
274
* 能否在单行中摆放
263
275
*
@@ -294,7 +306,7 @@ private TextView inflateTargetFaceTextView() {
294
306
}
295
307
296
308
/**
297
- * 测量 颜文字 的长度
309
+ * 测量“ 颜文字” 的长度
298
310
*/
299
311
private int measureFaceTextWidth (TextView faceTextView , FaceText faceText ) {
300
312
if (faceTextView == null || faceText == null ) {
0 commit comments