Skip to content

Commit

Permalink
添加在列表中使用标签的例子
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Apr 7, 2021
1 parent f1afeb2 commit 66a7826
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
})
implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:recyclerview-v7:26.1.0'

implementation project(":labels")
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RecyclerViewActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.donkingliang.labelsviewdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.donkingliang.labels.LabelsView;

import java.util.ArrayList;
import java.util.List;

/**
* @Author teach liang
* @Description
* @Date 2021/4/7
*/
public class LabelAdapter extends RecyclerView.Adapter<LabelAdapter.ViewHolder> {

private List<ListTestBean> list;
private Context mContext;

public LabelAdapter(Context context, List<ListTestBean> data) {
this.mContext = context;
this.list = data;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_label, parent, false));
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final ListTestBean bean = list.get(position);

// 设置数据前先移除OnLabelSelectChangeListener监听
// holder.labelsView.setOnLabelSelectChangeListener(null);

// 设置数据
holder.labelsView.setLabels(bean.getLabels());

// 设置标签状态监听
// holder.labelsView.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
// @Override
// public void onLabelSelectChange(TextView label, Object data, boolean isSelect, int position) {
// // 状态发生变化,保存选中状态
// bean.setSelects(holder.labelsView.getSelectLabels());
//
// // 使用1 .6 .4 以下版本,要这样写
// List<Integer> selects = new ArrayList<>();
// selects.addAll(holder.labelsView.getSelectLabels());
// bean.setSelects(selects);
// }
// });

// 设置标签点击监听(如果标签的状态只会通过点击标签切换,推荐使用这种方式)
holder.labelsView.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
@Override
public void onLabelClick(TextView label, Object data, int position) {
bean.setSelects(holder.labelsView.getSelectLabels());

// 使用1.6.4以下版本,要这样写
// List<Integer> selects = new ArrayList<>();
// selects.addAll(holder.labelsView.getSelectLabels());
// bean.setSelects(selects);
}
});

// 恢复选中状态,放在最后
holder.labelsView.setSelects(bean.getSelects());
}

@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}

static class ViewHolder extends RecyclerView.ViewHolder {

LabelsView labelsView;

public ViewHolder(View itemView) {
super(itemView);
labelsView = itemView.findViewById(R.id.labels);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.donkingliang.labelsviewdemo;

import java.util.List;

/**
* Depiction:
* Author:lry
* Date:2018/1/20
*/

public class ListTestBean {

private List<String> labels;
private List<Integer> selects;

public List<String> getLabels() {
return labels;
}

public void setLabels(List<String> labels) {
this.labels = labels;
}

public List<Integer> getSelects() {
return selects;
}

public void setSelects(List<Integer> selects) {
this.selects = selects;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.donkingliang.labelsviewdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.donkingliang.labels.LabelsView;

import java.util.ArrayList;
import java.util.List;

import static com.donkingliang.labelsviewdemo.R.id.labels;

Expand Down Expand Up @@ -38,6 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {
btns.add("指示器模式");
btns.add("取消选中");
btns.add("点击");
btns.add("在列表中使用");
btnLabels.setLabels(btns);
btnLabels.setOnLabelClickListener(this);

Expand Down Expand Up @@ -86,8 +85,10 @@ public CharSequence getLabelText(TextView label, int position, TestBean data) {

@Override
public void onLabelClick(TextView label, Object data, int position) {
labelsView.setOnLabelClickListener(null);
labelsView.clearCompulsorys();
if (position != 10) {
labelsView.setOnLabelClickListener(null);
labelsView.clearCompulsorys();
}
switch (position) {
case 0:
labelsView.setSelectType(LabelsView.SelectType.NONE);
Expand Down Expand Up @@ -151,6 +152,9 @@ public void onLabelClick(TextView label, Object data, int position) {
}
});
break;
case 10:
startActivity(new Intent(this,RecyclerViewActivity.class));
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.donkingliang.labelsviewdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
* @Author teach liang
* @Description
* @Date 2021/4/7
*/
public class RecyclerViewActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);

RecyclerView recyclerView = findViewById(R.id.recyclerView);

recyclerView.setLayoutManager(new LinearLayoutManager(this));


List<ListTestBean> data = new ArrayList<>();
for (int i = 1;i <= 15; i++){
ListTestBean bean = new ListTestBean();
List<String> labels = new ArrayList<>();
for (int y = 1;y <= 5; y++){
labels.add("第" + i + "组第" + y + "个");
}
bean.setLabels(labels);
data.add(bean);
}

recyclerView.setAdapter(new LabelAdapter(this,data));

}
}
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_recycler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>
33 changes: 33 additions & 0 deletions app/src/main/res/layout/adapter_label.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:layout_margin="10dp"
android:orientation="vertical">

<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"
android:layout_marginTop="10dp"
android:minHeight="25dp"
android:padding="5dp"
app:labelBackground="@drawable/label_bg"
app:labelTextColor="@drawable/label_text_color"
app:labelTextHeight="wrap_content"
app:labelTextPaddingBottom="5dp"
app:labelTextPaddingLeft="10dp"
app:labelTextPaddingRight="10dp"
app:labelTextPaddingTop="5dp"
app:labelTextSize="14sp"
app:labelTextWidth="wrap_content"
app:lineMargin="10dp"
app:maxSelect="0"
app:minSelect="1"
app:selectType="MULTI"
app:wordMargin="10dp"
app:isTextBold="true" />

</LinearLayout>

0 comments on commit 66a7826

Please sign in to comment.