Giter Site home page Giter Site logo

donkingliang / labelsview Goto Github PK

View Code? Open in Web Editor NEW
1.0K 15.0 157.0 355 KB

Android的标签列表控件。可以设置标签的选中效果。 可以设置标签的选中类型:不可选中、单选、限数量多选和不限数量多选等, 并支持设置必选项、单行显示、最大显示行数等功能。

License: Apache License 2.0

Java 100.00%
labels tabs android custom-view android-library android-ui

labelsview's Introduction

LabelsView

标签列表控件的使用介绍。

1、引入依赖 在Project的build.gradle在添加以下代码

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

在Module的build.gradle在添加以下代码

dependencies {
    implementation 'com.github.donkingliang:LabelsView:1.6.5'
}

2、编写布局:

   <com.donkingliang.labels.LabelsView 
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:id="@+id/labels"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:labelBackground="@drawable/label_bg"     //标签的背景
       app:labelTextColor="@drawable/label_text_color" //标签的字体颜色 可以是一个颜色值
       app:labelTextSize="14sp"      //标签的字体大小
       app:labelTextPaddingBottom="5dp"   //标签的上下左右边距
       app:labelTextPaddingLeft="10dp"
       app:labelTextPaddingRight="10dp"
       app:labelTextPaddingTop="5dp"
       app:lineMargin="10dp"   //行与行的距离
       app:wordMargin="10dp"   //标签与标签的距离
       app:selectType="SINGLE"   //标签的选择类型 有单选(可反选)、单选(不可反选)、多选、不可选四种类型
       app:maxLines="3"    // 设置最大显示行数,小于等于0则不限行数。
       app:maxColumns="5"  // 设置最大显示列数,小于等于0则不限行数。
       app:maxSelect="5"   //标签的最大选择数量,只有多选的时候才有用,0为不限数量
       app:minSelect="1"   //标签的最少选择数量,只有多选的时候才有用,0为不限数量
       app:isIndicator="true" />   //设置为指示器模式,不能手动改变标签的选中状态

<!-- 其他属性 -->
app:labelTextWidth="wrap_content"  // 标签项宽
app:labelTextHeight="wrap_content"  // 标签项高
app:labelGravity="center"  // 标签项的文本显示方向
app:labelTextPadding="5dp"  // 标签的Padding
app:singleLine="true"  // 单行显示,默认false
app:isTextBold="true" // 是否粗字体,默认false

这里有两个地方需要说明一下:

1)标签的正常样式和选中样式是通过drawable来实现的。比如下面两个drawable。

<!-- 标签的背景 label_bg -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 标签选中时的背景 -->
    <item android:state_selected="true">
        <shape>
            <stroke android:width="2dp" android:color="#fb435b" />
            <corners android:radius="8dp" />
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <!-- 标签的正常背景 -->
    <item>
        <shape>
            <stroke android:width="2dp" android:color="#656565" />
            <corners android:radius="8dp" />
            <solid android:color="@android:color/white" />
        </shape>
    </item>
</selector>
<!-- 标签的文字颜色 label_text_color -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 标签选中时的文字颜色 -->
    <item android:color="#fb435b" android:state_selected="true" />
    <!-- 标签的正常文字颜色 -->
    <item android:color="#2d2b2b" />
</selector>

TextView的textColor属性除了可以设置一个颜色值以外,也可以通过资源来设置的,这一点很多同学都不知道。

2)标签的选择类型有四种:

NONE :标签不可选中,也不响应选中事件监听,这是默认值。

SINGLE:单选(可反选)。这种模式下,可以一个也不选。

SINGLE_IRREVOCABLY:单选(不可反选)。这种模式下,有且只有一个是选中的。默认是第一个。

MULTI:多选,可以通过设置maxSelect限定选择的最大数量,0为不限数量。maxSelect只有在多选的时候才有效。多选模式下可以设置一些标签为必选项。必选项的标签默认选中,且不能取消。

3、设置标签:

labelsView = (LabelsView) findViewById(labels);
ArrayList<String> label = new ArrayList<>();
label.add("Android");
label.add("IOS");
label.add("前端");
label.add("后台");
label.add("微信开发");
label.add("游戏开发");
labelsView.setLabels(label); //直接设置一个字符串数组就可以了。

//LabelsView可以设置任何类型的数据,而不仅仅是String。
ArrayList<TestBean> testList = new ArrayList<>();
testList.add(new TestBean("Android",1));
testList.add(new TestBean("IOS",2));
testList.add(new TestBean("前端",3));
testList.add(new TestBean("后台",4));
testList.add(new TestBean("微信开发",5));
testList.add(new TestBean("游戏开发",6));
labelsView.setLabels(testList, new LabelsView.LabelTextProvider<TestBean>() {
    @Override
    public CharSequence getLabelText(TextView label, int position, TestBean data) {
    
    	// label就是标签项,在这里可以对标签项单独设置一些属性,比如文本样式等。
    
    	//根据data和position返回label需要显示的数据。
        return data.getName();
    }
});

4、设置事件监听:(如果需要的话)

//标签的点击监听
labelsView.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
    @Override
    public void onLabelClick(TextView label, Object data, int position) {
         //label是被点击的标签,data是标签所对应的数据,position是标签的位置。
    }
});

// 标签的长按监听
labelsView.setOnLabelLongClickListener(new LabelsView.OnLabelLongClickListener() {
    @Override
    public boolean onLabelLongClick(TextView label, Object data, int position) {
        return false;
    }
});

//标签的选中监听
labelsView.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
    @Override
    public void onLabelSelectChange(TextView label, Object data, boolean isSelect, int position) {
        //label是被选中的标签,data是标签所对应的数据,isSelect是是否选中,position是标签的位置。
    }
});

5、设置标签选中事件拦截:(如果需要的话)

当希望某个标签在特定条件下不被选中/取消选中时,可以使用事件拦截。只有用户点击改变标签选中状态时才会回调拦截,用其他方法改变时不会回调这个方法,不会被拦截。点击选中/取消选中时,拦截事件,返回true时,表示事件被拦截,不会改变标签的选中状态。

//设置标签选中状态的点击改变拦截器
labelsView.setOnSelectChangeIntercept(new LabelsView.OnSelectChangeIntercept() {
    @Override
    public boolean onIntercept(TextView label, Object data, boolean oldSelect, boolean newSelect, int position) {
        if (position == 0){
            //拦截第一个标签的状态改变事件,不能选中和反选
            return true;
        } else {
            return false;
        }
    }
});

6、常用方法

//设置选中标签。
//positions是个可变类型,表示被选中的标签的位置。
//比喻labelsView.setSelects(1,2,5);选中第1,3,5个标签。如果是单选的话,只有第一个参数有效。
public void setSelects(int... positions);
public void setSelects(List<Integer> positions);

//获取选中的标签(返回的是所有选中的标签的位置)。返回的是一个Integer的数组,表示被选中的标签的下标。如果没有选中,数组的size等于0。
public ArrayList<Integer> getSelectLabels();
//获取选中的label(返回的是所有选中的标签的数据)。如果没有选中,数组的size等于0。T表示标签的数据类型。
public <T> List<T> getSelectLabelDatas();

//取消所有选中的标签。
public void clearAllSelect();

//设置标签的选择类型,有NONE、SINGLE、SINGLE_IRREVOCABLY和MULTI四种类型。
public void setSelectType(SelectType selectType);

//设置最大的选择数量,只有selectType等于MULTI时有效。
public void setMaxSelect(int maxSelect);

//设置最少的选择数量,只有selectType等于MULTI时有效。
//注意:minSelect只限制用户手动点击取消选中时的效果。调用setSelects()、clearAllSelect()等方法改变标签的选中状态时,不受minSelect影响。
public void setMinSelect(int minSelect);

//设置为指示器模式,只能看,不能手动操作。这种模式下,用户不能通过手动点击改变标签的选中状态。
//但是仍然可以通过调用setSelects()、clearAllSelect()等方法改变标签的选中状态。
public void setIndicator(boolean indicator)

//设置必选项,只有在多项模式下,这个方法才有效
public void setCompulsorys(int... positions)
public void setCompulsorys(List<Integer> positions)

//清空必选项,只有在多项模式下,这个方法才有效
public void clearCompulsorys()

//设置标签背景
public void setLabelBackgroundResource(int resId);

//设置标签的文字颜色
public void setLabelTextColor(int color);
public void setLabelTextColor(ColorStateList color);

//设置标签的文字大小(单位是px)
public void setLabelTextSize(float size);

//设置标签内边距
public void setLabelTextPadding(int left, int top, int right, int bottom);

//设置行间隔
public void setLineMargin(int margin);

//设置标签的间隔
public void setWordMargin(int margin);

// 设置最大显示行数,小于等于0则不限行数。
public void setMaxLines(int maxLines);

// 设置最大显示列数,小于等于0则不限行数。
public void setMaxColumns(int maxColumns)

// 设置标签文本显示方向
public void setLabelGravity(int gravity);

// 设置是否单行显示
public void setMaxLines(int maxLines);

// 设置标签字体是否为粗体
public void setTextBold(boolean isBold);

// 获取标签的行数
public int getLines();

所有的set方法都有对应的get方法,这里就不说了。

效果图:

效果图  

想要了解该控件的具体实现的同学,欢迎访问我的博客
Android自定义标签列表控件LabelsView解析

labelsview's People

Contributors

donkingliang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

labelsview's Issues

点击

能否设置了默认选中后不可点击

设置最多展示行数

您好,我在代码中没有找到对最大行数的设置方法,请问 怎么设置最大显示行数?

正常样式和选中样式不生效

您好,我根据您的文档,在xml中配置了
app:labelBackground="@drawable/label_bg"
app:labelTextColor="@drawable/label_text_color"
但是在点击label的时候并没有出现设置的效果,没有任何反应,

setOnLabelClickListener 这个监听是有效的.

角标的奇怪BUG

setOnLabelClickListener 我给标签设置了点击事件,点击时根据position去移动对应位置的label,但是当我删除第一个,也就是角标是0的view时,之后删除第一个,角标全部变成了1,然后删到最后只剩最后个VIew时就报空了

少一条数据

小米note3,高度设置成wrap_content,9条数据的时候少了一条。当高度设置成固定高度的时候能显示出来!

Dear 大佬

您的方法里有选择之后的函数,但当我选择之后 突然有几个点了取消,但是这个取消的事件们可以同步到获取的data的那个list 上吗,,简而言之就是好像只能第一次全部点击正确?

只设置字符串不方便

您好:提个建议哈。能否设置label是Arraylist 呢,因为只设置String实在不方便,这个字符串的id、类型等等属性需要上传。然后获取的时候直接获取选中条目的对象就方便多了。

没有代码提示

xml里面不会自动补全代码,每次都要到github上复制粘贴过去

com.donkingliang.labels.LabelsView里边的一排tags如何让他居中?

image
作者你好,我在使用你的控件,但是发现了一个问题,就是我设置了一排4个lables,控件宽度设置成了android:layout_width="match_parent"
但是加载出来的lables不居中,而是左路对齐,请问这是bug么?还是需要设置什么属性,我找了一下没找到,烦请告知,不胜感激!
代码粘贴如下:
<com.donkingliang.labels.LabelsView
android:id="@+id/labvMessageType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:isIndicator="false"
app:labelBackground="@drawable/label_bg"
app:labelTextColor="@drawable/label_text_color"
app:labelTextPaddingBottom="5dp"
app:labelTextPaddingLeft="10dp"
app:labelTextPaddingRight="10dp"
app:labelTextPaddingTop="5dp"
app:labelTextSize="14sp"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:lineMargin="10dp"
app:maxLines="3"
app:maxSelect="4"
app:minSelect="0"
app:selectType="MULTI"
app:wordMargin="10dp"
android:background="@color/red"
/>

不显示

运行后labelsView 一闪而过,看不到标签,设置具体的宽高后也是一样不显示

点击事件问题

在RecyclerView中使用。LabelsView的点击事件会被LabelsView 拦截。不会传递到LabelsView的子控件中。只有LabelsView的空白部分,才能传递到子控件上,这个该怎么处理呢?

goodsSpecAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@OverRide
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
ToastUtils.showShort("xxxxx");
LabelsView labelsView = view.findViewById(R.id.label_view);
labelsView.setOnLabelSelectChangeListener(new
LabelsView.OnLabelSelectChangeListener() {
@OverRide
public void onLabelSelectChange(TextView label, Object data, boolean isSelect, int position) {
ToastUtils.showShort(position+"==");
}
});
}
});

默认选中

SINGLE_IRREVOCABLY模式下,选中第三个后,重新获取数据,怎么保持之前默认选中位置3,重新获取数据后总是会重置当前选中状态为第一个0

动态添加labelsView问题

您好,使用new LabelsView(this,null, 0) ,初始化完labelsView后,设置了set完各个属性后 setLabels()添加数据后,使用viewGroup.add(LablesView)时 lablesView的item 的宽高都很小,大小不正常 ,这个怎么修改才能正常显示呢

标签在布局居中

大神,能设置标签在居中吗?现在是左边窄右边宽,看起来不美观呀

对于数据一多,个别情况下无法展示全数据的问题

在onMeasure的时候
if (maxWidth <= lineWidth + view.getMeasuredWidth()) {
contentHeight += mLineMargin;
contentHeight += maxItemHeight;
maxItemHeight = 0;
maxLineWidth = Math.max(maxLineWidth, lineWidth);
lineWidth = 0;
begin = true;
}

这个判断,没有加上下个view所需的wordMargin,导致无法正确计算该行是不是可以正确显示下一条数据。

更正后应该是:
if (maxWidth <= lineWidth + view.getMeasuredWidth() + mWordMargin) {
contentHeight += mLineMargin;
contentHeight += maxItemHeight;
maxItemHeight = 0;
maxLineWidth = Math.max(maxLineWidth, lineWidth);
lineWidth = 0;
begin = true;
}

属性名字与appcompat-v7包冲突

你好,我使用appcompat-v7-28.0.0的包的时候,与LabelsView发送了资源命名冲突,错误如下

 duplicate value for resource 'attr/labelTextColor' with config ''.

建议属性的名称使用你自己的特殊名字作为前缀比较合适

导入项目,运行报错

android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.donkingliang.labels.LabelsView

找不到该控件。导包也没有报错

您好,我有一个新需求

您好,我是LabelsView控件的使用者,,感谢您的开源,我有一个需求,是这样的
LabelsView的每一个子条目标签的颜色都不一样
比如第一个标签颜色为红 第二个为黑 第三个为绿,我自己定义了一个数组存放,这样颜色值,通过随机数的方式来设置,这些颜色值,但是LabelsView 目前好像只支持LabelsView 整体标签的颜色是吗? 如果不是 如何设置呢?

可能超出边界

onLayout方法中,contentWidth = right - left,如果有设置了paddingRight的情况下,contentWidth应该再减去paddingRight的距离,才是右边的边界X

有没有取消选中的单个API

看到有个clearAll的 有没有取消单个的?这里有个需求是个其他个列表做联动的,其他列表删除了,这边选中的状态要改为未选中

当增加新标签时出现的显示问题

自己做了新增标签的功能,在添加新标签时会出现测量错误的问题
解决方案:
在labelsView.java的140行
if (maxWidth <= lineWidth + view.getMeasuredWidth() + mWordMargin) {
判断是否能够容纳一行中最后一个标签的时候,需要包括标签和之前的空隙
之后添加的新标签就能正常显示了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.