-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
7ed76d4
commit 2fcf26a
Showing
13 changed files
with
602 additions
and
17 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
43 changes: 41 additions & 2 deletions
43
app/src/main/java/com/chh/smartflexboxlayout/MainActivity.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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
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,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> |
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,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> |
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 |
---|---|---|
@@ -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> |
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
180 changes: 180 additions & 0 deletions
180
...boxlayoututils/src/main/java/com/chh/flexboxlayoututils/adapter/FlexboxLayoutAdapter.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,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 未初始化"); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...yoututils/src/main/java/com/chh/flexboxlayoututils/interfaces/setOnItemClickListener.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,6 @@ | ||
package com.chh.flexboxlayoututils.interfaces; | ||
|
||
import android.view.View; | ||
public interface setOnItemClickListener { | ||
void onItemClick(View view, int position, boolean isCheck); | ||
} |
Oops, something went wrong.