评价标签类似淘宝评价效果功能实现
2017-10-23 15:32
731 查看
需求:默认显示两行标签内容,点击展开显示全部标签内容,点击收起显示两行内容
![](http://img.blog.csdn.net/20171023152139148?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzQ1ODExMDI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](http://img.blog.csdn.net/20171023152205904?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzQ1ODExMDI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
主要实现代码:
MainActivity代码
LabelFlowLayout代码
LabelAdapter 代码
activity_main.xml
item_label.xml
主要实现代码:
MainActivity代码
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代码
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 代码
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
<?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
<?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"/>
相关文章推荐
- Android控件之RatingBar(类似淘宝实现滑动星级评价效果)
- 使用jQuery和PHP实现类似360功能开关效果
- 带长按扫描二维码功能的WebView,实现类似微信效果
- jquery实现类似淘宝星星评分功能有截图
- Span使用之利用自定义Span解析Html中特殊标签实现类似微博@效果
- ExtJs 中实现类似淘宝机器人效果
- Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- 使用Fragment实现类似TabHost标签栏的效果
- 【Unity快速实现小功能】实现一个类似跑马灯的效果
- jquery插件实现鼠标经过图片右侧显示大图的效果(类似淘宝)
- Python 实现类似PHP的strip_tags函数功能,并且可以自定义设置保留标签
- jQuery实现类似标签风格的导航菜单效果代码
- 使用Fragment实现类似TabHost标签栏的效果
- 分享一个jquery插件:实现类似淘宝上鼠标经过图片右侧显示大图的效果
- jquery实现类似淘宝星星评分功能实例
- Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- js实现类似淘宝继续拖动查看详情的弹簧效果
- 基于JSON+JQuery实现的多条件筛选功能(类似京东和淘宝功能)
- 用php实现类似淘宝,拍拍,易趣的最近浏览商品的功能模型代码