我是靠谱客的博主 无辜招牌,这篇文章主要介绍评价标签类似淘宝评价效果功能实现,现在分享给大家,希望可以做个参考。

需求:默认显示两行标签内容,点击展开显示全部标签内容,点击收起显示两行内容

这里写图片描述

这里写图片描述

主要实现代码:

MainActivity代码

复制代码
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
package it520view.com.labeldemo; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import butterknife.Bind; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { @Bind(R.id.lfl_label) LabelFlowLayout mLflLabel; @Bind(R.id.tv_shrink) TextView mTvShrink; @Bind(R.id.ll_export) LinearLayout mLlExport; private ArrayList<String> mData; private boolean isExport;//判断展开收起的标志 private LabelAdapter mLabelAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); initData(); initListener(); mLabelAdapter = new LabelAdapter(this,mData); mLflLabel.setAdapter(mLabelAdapter); } private void initData() { mData = new ArrayList<String>(); for (int i = 0; i < 7; i++) { mData.add("服务很周到,洗的很干净" + i); } for (int i = 0; i < 13; i++) { mData.add("洗的很干净"+i); } } private void initListener() { mLlExport.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isExport = !isExport; refreshLabel(); } }); } private void refreshLabel() { if (!isExport) { mLflLabel.setLimitLines(0); changeDrawbleWithString("收起", getResources().getDrawable(R.drawable.icon_d01)); } else { mLflLabel.setLimitLines(2); changeDrawbleWithString("展开", getResources().getDrawable(R.drawable.icon_d02)); } mLflLabel.postInvalidate(); } private void changeDrawbleWithString(String str,Drawable drawable) { mTvShrink.setText(str); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); mTvShrink.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null); } }

LabelFlowLayout代码

复制代码
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
package it520view.com.labeldemo; import android.content.Context; import android.database.DataSetObserver; import android.util.AttributeSet; import android.util.SparseIntArray; import android.view.View; import android.widget.AdapterView; import android.widget.ListAdapter; /** * */ public class LabelFlowLayout extends AdapterView<ListAdapter> implements View.OnClickListener { private ListAdapter mAdapter; /** * 上下行间距 */ public int veticalSpace = 30; /** * Item间距 */ public int horSpace = 30; /** * 每一行有多少个view */ private SparseIntArray mLineViews = new SparseIntArray(); private AdapterDataSetObserver mDataSetObserver; /** * 一行的高度由最高的item决定 */ private int lineHeight; //限制行数 private int mLimitLines = -1; private int curLines; public LabelFlowLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public ListAdapter getAdapter() { return mAdapter; } @Override public void setAdapter(ListAdapter adapter) { if (mAdapter != null && mDataSetObserver != null) { mAdapter.unregisterDataSetObserver(mDataSetObserver); } mAdapter = adapter; if (mAdapter != null) { mDataSetObserver = new AdapterDataSetObserver(); mAdapter.registerDataSetObserver(mDataSetObserver); makeView(); } else { removeAllViewsInLayout(); invalidate(); } } private void makeView() { removeAllViewsInLayout(); requestLayout(); int count = mAdapter.getCount(); for (int i = 0; i < count; i++) { View child = mAdapter.getView(i, null, this); LayoutParams params = child.getLayoutParams(); if (params == null) params = generateDefaultLayoutParams(); addViewInLayout(child, i, params); } } /** * 设置显示行数,设置完成后会自动刷新 * 如由限制改为不限制,设为0 * * @param lines */ public void setLimitLines(int lines) { this.mLimitLines = lines; requestLayout(); } /** * 获取当前行数,注意第一次绘制前可能该数未被初始化 * @return */ public int getCurLines() { return curLines; } private class AdapterDataSetObserver extends DataSetObserver { @Override public void onChanged() { super.onChanged(); makeView(); requestLayout(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); int layoutWidth = 0; int layoutHeight = 0; int lineWidth = getPaddingLeft() + getPaddingRight(); if (mAdapter == null) return; int count = getChildCount(); mLineViews.clear(); int lineCount = 0; int line = 0; for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); lineHeight = Math.max(lineHeight, child.getMeasuredHeight()); int childWidth = child.getMeasuredWidth(); if (lineWidth + childWidth > widthSize) { mLineViews.put(line, lineCount); lineWidth -= horSpace; layoutWidth = Math.max(layoutWidth, lineWidth); line++; lineWidth = getPaddingLeft() + getPaddingRight(); lineCount = 0; //限制行数 if(mLimitLines > 0 && line == mLimitLines){ break; } } lineCount++; lineWidth += childWidth + horSpace; if (i == count - 1) { mLineViews.put(line, lineCount); lineWidth -= horSpace; layoutWidth = Math.max(layoutWidth, lineWidth); lineCount = 0; } } if (modeWidth == MeasureSpec.EXACTLY) { layoutWidth = widthSize; } int lines = mLineViews.size(); curLines = lines; if (lines > 0) { if (modeHeight == MeasureSpec.EXACTLY) { layoutHeight = heightSize; } else { layoutHeight = getPaddingTop() + getPaddingBottom() + lines * lineHeight + (lines - 1) * veticalSpace; } } setMeasuredDimension(layoutWidth, layoutHeight); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int leftPos = 0; int topPos = 0; int lines = mLineViews.size(); int lineCount; int position = 0; for (int i = 0; i < lines; i++) { lineCount = mLineViews.get(i); View child; topPos = getPaddingTop() + i * veticalSpace + i * lineHeight; leftPos = getPaddingLeft(); for (int j = 0; j < lineCount; j++) { child = getChildAt(position); if (child == null) return; position++; child.setOnClickListener(this); int childWidth = child.getMeasuredWidth(); child.layout(leftPos, topPos, leftPos + childWidth, topPos + lineHeight); leftPos += childWidth + horSpace; } } } @Override public void onClick(View v) { int pos = indexOfChild(v); performItemClick(v, pos, 0); } @Override public void setSelection(int position) { // TODO Auto-generated method stub } @Override public View getSelectedView() { // TODO Auto-generated method stub return null; } }

LabelAdapter 代码

复制代码
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
package it520view.com.labeldemo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class LabelAdapter extends BaseAdapter { private LayoutInflater mInflater; private List<String> list; public LabelAdapter(Context context, List<String> list) { super(); this.mInflater = LayoutInflater.from(context); this.list = list; } static class ViewHolder { TextView TextView; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = mInflater.inflate(R.layout.item_label, parent,false); holder = new ViewHolder(); holder.TextView = (TextView) convertView.findViewById(R.id.name_tv); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.TextView.setText(list.get(position)); return convertView; } }

activity_main.xml

复制代码
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
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <it520view.com.labeldemo.LabelFlowLayout android:id="@+id/lfl_label" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:padding="10dp"/> <LinearLayout android:id="@+id/ll_export" android:layout_width="match_parent" android:layout_height="40dp" android:background="#FFFFFF" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp"> <TextView android:id="@+id/tv_shrink" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="3dp" android:text="展开" android:textColor="#000000" android:textSize="12sp"/> </LinearLayout> </LinearLayout>

item_label.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/labelshape" android:gravity="center" android:layout_margin="3dp" android:lines="1" android:padding="8dp" android:textColor="@color/textColorforItemTitle" android:textSize="12dp" android:ellipsize="end"/>

最后

以上就是无辜招牌最近收集整理的关于评价标签类似淘宝评价效果功能实现的全部内容,更多相关评价标签类似淘宝评价效果功能实现内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部