Android 类似评论功能的控件

1:效果如下:
 

 2:代码:

public abstract class MyAppStart extends LinearLayout {

    private OnItemSelectListener onItemSelectListener;

    private int count;

    public int colorId;

    public int getColorId() {
        return colorId;
    }

    public void setColorId(int colorId) {
        this.colorId = colorId;
    }

    public MyAppStart(Context context, AttributeSet attrs) {

        super(context, attrs);

        for (int i = 0 ; i < max() ; i ++) {

            View view = getView(new View(context));

            view.setTag(i);

            if (isClick()) {

                view.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        setSelect((Integer) v.getTag());

                    }

                });

            }

            addView(view);

        }

        setSelect(count = 0);

    }

    public void setSelect(int count) {

        for (int i = 0 ; i < getChildCount() ; i ++) {

            getChildAt(i).setBackgroundResource(i <= (this.count = count) ? selectYes() : selectNo());

            BitmapDrawable p = (BitmapDrawable) getChildAt(i).getBackground();

            if (i <= (this.count = count) && isReColor()){

                p.setColorFilter(new PorterDuffColorFilter(getColorId(), PorterDuff.Mode.SRC_ATOP));

            } else {

                p.setColorFilter(null);

            }

        }

        if (onItemSelectListener != null) {

            onItemSelectListener.onItemSelect(this.count = count);

        }

    }

    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener) {

        this.onItemSelectListener = onItemSelectListener;
        
        onItemSelectListener.onItemSelect(count);

    }
    protected boolean isReColor(){

        return false;
    }
    protected abstract boolean isClick();

    protected abstract int max();

    protected abstract int selectYes();

    protected abstract int selectNo();

    protected abstract View getView(View view);

    public interface OnItemSelectListener {

        void onItemSelect(int index);

    }

}

 

public class EvaluateStartView extends MyAppStart {

    private LinearLayout.LayoutParams params;

    public EvaluateStartView(Context context, AttributeSet attrs) {

        super(context, attrs);


    }

    @Override
    protected boolean isClick() {

        return true;

    }



    @Override
    protected int max() {

        return 5;

    }

    @Override
    protected int selectYes() {


        return R.mipmap.evaluate_start_on;

    }

    @Override
    protected int selectNo() {

        return R.mipmap.evaluate_start_off;

    }

    @Override
    protected View getView(View view) {

        params = new LinearLayout.LayoutParams(Utils.dpToPx(23), Utils.dpToPx(23));

        params.setMargins(0, 0, Utils.dpToPx(5), 0);

        view.setLayoutParams(params);

        return view;

    }



}

 3:使用说明:

在activity中直接使用

eva.setSelect(2);0-4(分别对应的选中效果是1-5个)。

当要监听选中了第几个的时候调用如下方法:

 eva.setOnItemSelectListener(new MyAppStart.OnItemSelectListener() {
            @Override
            public void onItemSelect(int index) {
                Toast.makeText(TagFlowLayoutActivity.this,index+"",Toast.LENGTH_SHORT).show();
            }
        });

 当不想让图片可点击时把

EvaluateStartView方法中的isClick()方法的返回值改成false就可以了。
 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_今晚不加班

谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值