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

概述

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

这里写图片描述

这里写图片描述

主要实现代码:

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"/>

最后

以上就是无辜招牌为你收集整理的评价标签类似淘宝评价效果功能实现的全部内容,希望文章能够帮你解决评价标签类似淘宝评价效果功能实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部