您当前的位置: 首页 >  android

Kevin-Dev

暂无认证

  • 0浏览

    0关注

    544博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android -- 开源库】FlowLayout 的基本使用

Kevin-Dev 发布时间:2022-03-29 09:10:40 ,浏览量:0

在这里插入图片描述

目前最新版本是 1.1.2。 GitHub:https://github.com/hongyangAndroid/FlowLayout

一、 简介

Android 流式布局,支持单选、多选等,适合用于产品标签等。

  • setAdapter 形式注入数据
  • 直接设置selectorbackground即可完成标签选则的切换,类似CheckBox
  • 支持控制选择的Tag数量,比如:单选、多选
  • 支持setOnTagClickListener,当点击某个Tag回调
  • 支持setOnSelectListener,当选择某个Tag后回调
  • 支持adapter.notifyDataChanged
  • Activity重建(或者旋转)后,选择的状态自动保存
二、引入框架

1. 在 app/build.gradle 中添加依赖

implementation 'com.hyman:flowlayout-lib:1.1.2'

2. 效果图 Screenshot_2021-05-20-13-42-50-832_com.hk.hksale.jpg

三、使用

1. 布局文件




    

    

    

    

    
        

        
    

2. tag_txt_level.xml





3. 重要代码

private void showPopupWindow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }

        popupWindow = new CommonPopupWindow.Builder(getActivity())
                .setView(R.layout.popup_filter)
                .setAnimationStyle(R.style.AnimDown)
                .setBackGroundLevel(0.5f)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        final LayoutInflater mInflater = LayoutInflater.from(getActivity());
                        TagFlowLayout flowLayout = (TagFlowLayout) view.findViewById(R.id.flowlayout_type);
                        TagFlowLayout flowLayout_level = (TagFlowLayout) view.findViewById(R.id.flowlayout_level);
                        TextView txt_ok = (TextView) view.findViewById(R.id.txt_ok);
                        TextView txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);

                        flowLayout.setMaxSelectCount(1);
                        flowLayout_level.setMaxSelectCount(1);
                        flowLayout.setAdapter(new TagAdapter(types) {
                            @Override
                            public View getView(FlowLayout parent, int position, String text) {
                                TextView tv_date = (TextView)mInflater.inflate(R.layout.tag_txt_level,flowLayout,false);
                                tv_date.setText(text);
                                return tv_date;
                            }
                        });

                        flowLayout_level.setAdapter(new TagAdapter(levels) {
                            @Override
                            public View getView(FlowLayout parent, int position, String text) {
                                TextView tv_date = (TextView)mInflater.inflate(R.layout.tag_txt,flowLayout_level,false);
                                tv_date.setText(text);
                                return tv_date;
                            }
                        });

                        flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                            @Override
                            public boolean onTagClick(View view, int position, FlowLayout parent) {
                                ToastUtils.showToast(getActivity(),types.get(position));
                                return true;
                            }
                        });

                        flowLayout_level.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                            @Override
                            public boolean onTagClick(View view, int position, FlowLayout parent) {
                                ToastUtils.showToast(getActivity(),levels.get(position));
                                return true;
                            }
                        });

                        txt_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (popupWindow != null) {
                                    popupWindow.dismiss();
                                }
                            }
                        });

                        txt_ok.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // todo 接口文档
                                if (popupWindow != null) {
                                    popupWindow.dismiss();
                                }
                            }
                        });

                    }
                })
                .setOutsideTouchable(true)
                .create();

        popupWindow.showAsDropDown(mTitleBar);
    }
关注
打赏
1658837700
查看更多评论
立即登录/注册

微信扫码登录

0.3270s