-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
teach
committed
Apr 7, 2021
1 parent
f1afeb2
commit 66a7826
Showing
8 changed files
with
222 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/main/java/com/donkingliang/labelsviewdemo/LabelAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/donkingliang/labelsviewdemo/ListTestBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/donkingliang/labelsviewdemo/RecyclerViewActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |