实现一个"综合"按钮类

一个item有很多按钮,可以实现一个按钮类,包含这些按钮,通过init(int type)来初始化特定的按钮。

 

图2中这是用代码来实现了selector。 

 使用、初始化:

            mDelete.init(HiSingListClickView.VIEW_TYPE_DELETE);
            mCollect.init(HiSingListClickView.VIEW_TYPE_COLLECT);
            mKtv.init(HiSingListClickView.VIEW_TYPE_SING);
            mTop.init(HiSingListClickView.VIEW_TYPE_TOP);

 

全部代码:

public class HiSingListClickView extends HiSingShadowLinearLayout {

    private static final String TAG = "HiSingListClickView";
    private ImageView mIcon;
    private TextView mTitleTv;
    //K歌
    public static final int VIEW_TYPE_PLAY = 0;
    //点歌
    public static final int VIEW_TYPE_ADD = 1;
    //收藏
    public static final int VIEW_TYPE_COLLECT = 2;
    //置顶
    public static final int VIEW_TYPE_TOP = 3;
    //删除
    public static final int VIEW_TYPE_DELETE = 4;
    //分享
    public static final int VIEW_TYPE_SHARE = 5;
    //作品播放
    public static final int VIEW_TYPE_SING = 6;
    //点赞
    public static final int VIEW_TYPE_LIKE = 7;

    public HiSingListClickView(Context context) {
        super(context);
        initViews(context);
    }

    public HiSingListClickView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    public HiSingListClickView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(context);
    }

    private void initViews(Context context) {
        LayoutInflater.from(context).inflate(R.layout.hising_list_click_view, this);
        mIcon = findViewById(R.id.hising_click_image);
        mTitleTv = findViewById(R.id.hising_click_tv);
        setGravity(Gravity.CENTER);
        setPadding(getResources().getDimensionPixelOffset(R.dimen.dimen_22),
                0,
                getResources().getDimensionPixelOffset(R.dimen.dimen_22),
                0);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (mIcon != null) {
            mIcon.setSelected(selected);
            mIcon.setVisibility(VISIBLE);
        }
        HiSingViewUtil.setVisible(mTitleTv, selected);
        setBackgroundResource(selected ? R.drawable.hising_add_shape_new : 0);
    }

    /**
     * 是否选中状态
     *
     * @param selected   选中
     * @param canVisible icon是否可见
     */
    public void setSelected(boolean selected, boolean canVisible) {
        super.setSelected(selected);
        if (mIcon != null) {
            mIcon.setSelected(selected);
            if (canVisible) {
                mIcon.setVisibility(VISIBLE);
            }
        }
        HiSingViewUtil.setVisible(mTitleTv, selected);
        setBackgroundResource(selected ? R.drawable.hising_add_shape_new : 0);
    }

    public void setIconVisible(boolean visible) {
        if (mIcon != null) {
            mIcon.setVisibility(visible ? VISIBLE : INVISIBLE);
        }
    }

    public void init(int type) {
        if (mIcon == null || mTitleTv == null) {
            return;
        }
        StateListDrawable stateListDrawable = new StateListDrawable();
        switch (type) {
            case VIEW_TYPE_PLAY:
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.PLAY_FOCUS));
                stateListDrawable.addState(new int[]{},
                        getResources().getDrawable(R.drawable.hising_play));
                mIcon.setBackground(stateListDrawable);
                HiSingViewUtil.setText(mTitleTv, getResources().getString(R.string.hising_guide_tip_play));
                break;
            case VIEW_TYPE_ADD:
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.ADD_FOCUS));
                stateListDrawable.addState(new int[]{},
                        getResources().getDrawable(R.drawable.hising_search_add_unfocus));
                mIcon.setBackground(stateListDrawable);
                HiSingViewUtil.setText(mTitleTv, getResources().getString(R.string.hising_guide_tip_add));
                break;
            case VIEW_TYPE_COLLECT:
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.COLLECT_FOCUS));
                stateListDrawable.addState(new int[]{},
                        getResources().getDrawable(R.drawable.hising_search_collect_unfocus));
                mIcon.setBackground(stateListDrawable);
                HiSingViewUtil.setText(mTitleTv, getResources().getString(R.string.hising_guide_tip_favorite));
                break;
            case VIEW_TYPE_SHARE:
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.SHARE_FOCUS));
                stateListDrawable.addState(new int[]{},
                        getResources().getDrawable(R.drawable.hising_share));
                mIcon.setBackground(stateListDrawable);
                HiSingViewUtil.setText(mTitleTv, getResources().getString(R.string.hising_guide_tip_share));
                break;
            case VIEW_TYPE_DELETE:
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.DELETE_FOCUS));
                stateListDrawable.addState(new int[]{},
                        getResources().getDrawable(R.drawable.hising_picked_delete_unfocused));
                mIcon.setBackground(stateListDrawable);
                HiSingViewUtil.setText(mTitleTv, getResources().getString(R.string.hising_guide_tip_delete));
                break;
            case VIEW_TYPE_SING:
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.SING_KTV));
                stateListDrawable.addState(new int[]{},
                        getResources().getDrawable(R.drawable.hising_search_ktv_unfocus));
                mIcon.setBackground(stateListDrawable);
                HiSingViewUtil.setText(mTitleTv, getResources().getString(R.string.hising_sing_tip));
                break;
            case VIEW_TYPE_LIKE:
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.COMPETITION_LIKED_FOCUS_ICON));
                stateListDrawable.addState(new int[]{},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.COMPETITION_LIKED_UNFOCUS_ICON));
                mIcon.setBackground(stateListDrawable);
                HiSingViewUtil.setText(mTitleTv, getResources().getString(R.string.hising_guide_tip_like));
                break;
            case VIEW_TYPE_TOP:
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(), HiSingThemeUtil.HiSingThemeEnum.UP_TO_TOP_FOCUS));
                stateListDrawable.addState(new int[]{},
                        getResources().getDrawable(R.drawable.hising_picked_up_to_top_unfocus));
                mIcon.setBackground(stateListDrawable);
                HiSingViewUtil.setText(mTitleTv, getResources().getString(R.string.hising_guide_tip_topping));
                break;
            default:
                break;
        }
    }

    /**
     * 更新收藏按钮文案和图标
     */
    public void updateCollectState(boolean collect) {
        if (mIcon != null) {
            StateListDrawable stateListDrawable = new StateListDrawable();
            stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                    HiSingThemeUtil.getDrawable(getContext(), collect ?
                            HiSingThemeUtil.HiSingThemeEnum.COLLECTED_FOCUS :
                            HiSingThemeUtil.HiSingThemeEnum.COLLECT_FOCUS));
            stateListDrawable.addState(new int[]{}, getResources().getDrawable(collect ?
                    R.drawable.hising_search_collect_selected_unfocus :
                    R.drawable.hising_search_collect_unfocus));
            mIcon.setBackground(stateListDrawable);
        }
        HiSingViewUtil.setText(mTitleTv,
                getResources().getString(collect ?
                        R.string.hising_unite_collect :
                        R.string.hising_guide_tip_favorite
                ));
    }

    public void updateLikeState(boolean like) {
        if (mIcon != null) {
            if (like) {
                StateListDrawable stateListDrawable = new StateListDrawable();
                stateListDrawable.addState(new int[]{android.R.attr.state_selected},
                        HiSingThemeUtil.getDrawable(getContext(),
                                HiSingThemeUtil.HiSingThemeEnum.COMPETITION_LIKED_FOCUS_ICON));
                stateListDrawable.addState(new int[]{},
                        HiSingThemeUtil.getDrawable(getContext(),
                                HiSingThemeUtil.HiSingThemeEnum.COMPETITION_LIKED_UNFOCUS_ICON));
                mIcon.setBackground(stateListDrawable);
            } else {
                mIcon.setBackgroundResource(
                        R.drawable.hising_competition_ranklist_like_btn);
            }
        }
        HiSingViewUtil.setText(mTitleTv,
                getResources().getString(like ?
                        R.string.hising_competition_like_havelike :
                        R.string.hising_guide_tip_like
                ));
    }
}
View Code

 

另外,setvisible()这样实现在工具类里使用比较方便,不用一遍一遍的写判空条件了:

HiSingViewUtil.setVisible(mTitleTv, selected);
    public static void setVisible(View view, boolean show) {
        if (view == null) {
            return;
        }
        view.setVisibility(show ? View.VISIBLE : View.GONE);
    }

 

public class HiSingViewUtil {
    /**
     * 设置文本颜色
     *
     * @param view   文本
     * @param select 是否设置遗留焦点色
     */
    public static void setTextSelectColor(TextView view, boolean select) {
        if (view != null) {
            view.setTextColor(HiSingUiUtil.getColor(select ? IDrawableService.ColorEnum.SELECTED :
                    IDrawableService.ColorEnum.TEXT_NORMAL));
        }
    }

    public static void setText(TextView view, String text) {
        if (view == null) {
            return;
        }
        view.setText(text);
    }

    public static void setVisible(View view, boolean show) {
        if (view == null) {
            return;
        }
        view.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public static void setVisible(View view, int visibility) {
        if (view == null) {
            return;
        }
        view.setVisibility(visibility);
    }

    /**
     * view获取焦点,适配触控模式点击获取焦点
     *
     * @param view 组件view
     */
    public static boolean requestFocus(View view) {
        if (view != null) {
            view.setFocusableInTouchMode(true);
            return view.requestFocus();
        }
        return false;
    }
}
View Code

 

----

试一下,一个view包含多个按钮,点击事件的响应是怎样的,按钮能否响应点击事件。 

一口吃不成胖子,在于一点一滴的积累。

 

posted @ 2025-06-11 16:20  touchmore  阅读(8)  评论(0)    收藏  举报