Skip to content

Commit

Permalink
增加支持单选
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjiemonbile committed Jul 17, 2019
1 parent 7ed76d4 commit 2fcf26a
Show file tree
Hide file tree
Showing 13 changed files with 602 additions and 17 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
// implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation project(':flexboxlayoututils')
implementation 'com.google.android:flexbox:1.1.0'
}
43 changes: 41 additions & 2 deletions app/src/main/java/com/chh/smartflexboxlayout/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
package com.chh.smartflexboxlayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
import androidx.appcompat.app.AppCompatActivity;

import com.chh.flexboxlayoututils.interfaces.setOnItemClickListener;
import com.chh.flexboxlayoututils.widget.FlowLayout;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private List<String> dataList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setData();
final FlowLayout mSmartFlexboxLayout = findViewById(R.id.mSmartFlexboxLayout);
mSmartFlexboxLayout.setData(this,dataList);
mSmartFlexboxLayout.setListener(new setOnItemClickListener() {
@Override
public void onItemClick(View view, int position, boolean isCheck) {
Log.d("chh","selectedData onItemClick position:"+ position +" isCheck:"+isCheck);
}
});

Button mButtonSelected = findViewById(R.id.mButtonSelected);
mButtonSelected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> selectedData = mSmartFlexboxLayout.getSelectedData();
for (int i = 0; i < selectedData.size(); i++) {
Log.d("chh","selectedData:"+ selectedData.get(i));
}
}
});
}

private void setData(){
for (int i = 0; i < 10; i++) {
if (i == 1||i==4||i==7) {
dataList.add("长长的标签" + i);
} else {
dataList.add("标签" + i);
}
}
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/shape_default_drawable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<stroke android:color="#999999" android:width="1dp"/>
<solid android:color="#ffffff"/>
</shape>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/shape_selected_drawable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<solid android:color="#ff6600"/>
</shape>
30 changes: 19 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
<Button
android:id="@+id/mButtonSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
android:text="获取选中的数据"/>
<com.chh.flexboxlayoututils.widget.FlowLayout
android:id="@+id/mSmartFlexboxLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:default_color="#555555"
app:selected_textColor="#ffffff"
app:default_drawable="@drawable/shape_default_drawable"
app:selected_drawable="@drawable/shape_selected_drawable"
app:text_size="14sp"
app:checked_enable="true"
app:mode="Mulit"
app:max_num="3"/>
</LinearLayout>
9 changes: 6 additions & 3 deletions flexboxlayoututils/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.15037305972' //注意com.github.后面的为github账号
group='com.github.huahuiPj' //注意com.github.后面的为github账号
android {
compileSdkVersion 28

Expand All @@ -27,8 +27,11 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:28.0.0'
// implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
// androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.android:flexbox:1.1.0'
// implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.chh.flexboxlayoututils.adapter;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chh.flexboxlayoututils.R;
import com.chh.flexboxlayoututils.interfaces.setOnItemClickListener;
import com.chh.flexboxlayoututils.widget.FlowLayout;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FlexboxLayoutAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private int layoutRes;
private View view ;
private List<String> data ;
private Map<Integer,String> selectedMap = new HashMap<>();
private Map<Integer,Boolean> sMap = new HashMap<>();
private int Oldposition = -1;
private TextView OldTextView;
public FlexboxLayoutAdapter(Context context,int layoutRes) {
this.context = context;
this.layoutRes = layoutRes;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
view = LayoutInflater.from(context).inflate(layoutRes, parent,false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
if(data!=null&&data.size()>0){
((MyViewHolder) holder).mTextItems.setText(data.get(position));
((MyViewHolder) holder).mTextItems.setTextColor(defaultTextColor);
((MyViewHolder) holder).mTextItems.setBackgroundResource(defauleDrawable);
if(textsize!=0){
((MyViewHolder) holder).mTextItems.setTextSize(TypedValue.COMPLEX_UNIT_PX,textsize);
}
sMap.put(position,false);
((MyViewHolder) holder).mTextItems.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView = (TextView) v;
String str = data.get(position);
if (checkEnable) {
boolean isSelected = sMap.get(position);
if (SelectModel == MulitModel) { //多选
if (isSelected) {
textView.setTextColor(defaultTextColor);
textView.setBackgroundResource(defauleDrawable);
sMap.put(position, false);
selectedMap.remove(position);
} else {
if (selectedMap.size() < maxSelection) {
textView.setTextColor(selectedTextColor);
textView.setBackgroundResource(selectedDrawable);
sMap.put(position, true);
selectedMap.put(position, str);
} else {
// Toast.makeText(context,"不能超出最大值"+maxSelection+"个",Toast.LENGTH_LONG).show();
}
}
}else if (SelectModel == SingelModel){ //单选
if (!isSelected) {
if (selectedMap.size() > 0) {
OldTextView.setTextColor(defaultTextColor);
OldTextView.setBackgroundResource(defauleDrawable);
sMap.put(Oldposition, false);
selectedMap.remove(Oldposition);
}
textView.setTextColor(selectedTextColor);
textView.setBackgroundResource(selectedDrawable);
sMap.put(position, true);
selectedMap.put(position, str);

OldTextView = textView;
Oldposition = position;
}
}
}
if (clickListener != null) {
clickListener.onItemClick(v, position, sMap.get(position));
}
}
});
}
}

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

public class MyViewHolder extends RecyclerView.ViewHolder{
TextView mTextItems;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mTextItems = itemView.findViewById(R.id.mTextItems);
}
}

public void setDataList(List<String> dataList){
//设置数据
if (this.data == null) {
this.data = new ArrayList<>();
}
this.data.addAll(dataList) ;
notifyDataSetChanged();
}

public List<String> getSelectedData(){
Collection<String> values = selectedMap.values();
List<String> list = new ArrayList<>(values);
return list;
}

private setOnItemClickListener clickListener;
public void setListener(setOnItemClickListener clickListener){
this.clickListener = clickListener;
}

//可选的最大数量
private int maxSelection ;
//选择模式
private int SelectModel;
//多选模式
public static final int MulitModel = 0;
//单选模式
public static final int SingelModel = 1;
//字体大小
private float textsize;
//默认字体颜色
private int defaultTextColor;
//选中时的字体颜色
private int selectedTextColor;
//默认时的样式
private int defauleDrawable;
//选中时的样式
private int selectedDrawable;
//设置是否可以选中
private boolean checkEnable;
public void setFlexboxLayoutView(FlowLayout flexboxLayout){
if (flexboxLayout != null) {
maxSelection = flexboxLayout.getMaxSelection();
SelectModel = flexboxLayout.getSelectModel();
defaultTextColor = flexboxLayout.getDefaultTextColor();
selectedTextColor = flexboxLayout.getSelectedTextColor();
defauleDrawable = flexboxLayout.getDefauleDrawable();
selectedDrawable =flexboxLayout.getSelectedDrawable();
textsize = flexboxLayout.getTextsize();
checkEnable = flexboxLayout.isCheckEnable();
if (SelectModel==SingelModel) {
maxSelection = 1;
} else if (SelectModel==MulitModel&&maxSelection==0) {
maxSelection=1;
}
} else {
throw new NullPointerException("SmartFlexboxLayout 未初始化");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.chh.flexboxlayoututils.interfaces;

import android.view.View;
public interface setOnItemClickListener {
void onItemClick(View view, int position, boolean isCheck);
}
Loading

0 comments on commit 2fcf26a

Please sign in to comment.