diff --git a/app/src/main/java/com/lzy/flowlayoutdemo/MainActivity.java b/app/src/main/java/com/lzy/flowlayoutdemo/MainActivity.java index efa041f..c955d7f 100644 --- a/app/src/main/java/com/lzy/flowlayoutdemo/MainActivity.java +++ b/app/src/main/java/com/lzy/flowlayoutdemo/MainActivity.java @@ -1,13 +1,13 @@ package com.lzy.flowlayoutdemo; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import com.lzy.flowlayout.CateBean; import com.lzy.flowlayout.FlowLayoutDialog; -import com.lzy.flowlayout.Product; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { @@ -17,50 +17,30 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - List classifies = new ArrayList<>(); - classifies.add(new Product.Classify("年级", Arrays.asList( - new Product.Classify.Des("一年级"), - new Product.Classify.Des("二年级"), - new Product.Classify.Des("三年级"), - new Product.Classify.Des("四年级"), - new Product.Classify.Des("五年级"), - new Product.Classify.Des("六年级")))); - classifies.add(new Product.Classify("科目", Arrays.asList(new Product.Classify.Des("180"), - new Product.Classify.Des("175"), - new Product.Classify.Des("170"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("160"), - new Product.Classify.Des("155"), - new Product.Classify.Des("150")))); - classifies.add(new Product.Classify("科目", Arrays.asList(new Product.Classify.Des("180"), - new Product.Classify.Des("175"), - new Product.Classify.Des("170"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("160"), - new Product.Classify.Des("155"), - new Product.Classify.Des("150")))); - classifies.add(new Product.Classify("科目", Arrays.asList(new Product.Classify.Des("180"), - new Product.Classify.Des("175"), - new Product.Classify.Des("170"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("160"), - new Product.Classify.Des("155"), - new Product.Classify.Des("150")))); - classifies.add(new Product.Classify("款式", - Arrays.asList(new Product.Classify.Des("男款"), new Product.Classify.Des("语文"), - new Product.Classify.Des("数学"), - new Product.Classify.Des("英语")))); - - FlowLayoutDialog flowLayoutDialog = new FlowLayoutDialog(this, classifies); + List cateBeanList = new ArrayList<>(); + CateBean cateBean = new CateBean(); + cateBean.setCcid(1); + cateBean.setIdx(1); + cateBean.setName("年级"); + + List childcatelist = new ArrayList<>(); + CateBean.ChildcatelistBean childcatelistBean = new CateBean.ChildcatelistBean(); + childcatelistBean.setCccid(1); + childcatelistBean.setIdx(1); + childcatelistBean.setName("一年级"); + childcatelist.add(childcatelistBean); + + cateBean.setChildcatelist(childcatelist); + cateBeanList.add(cateBean); + + + FlowLayoutDialog flowLayoutDialog = new FlowLayoutDialog(this, cateBeanList); + flowLayoutDialog.setChooseResultListenner(new FlowLayoutDialog.ChooseResultListenner() { + @Override + public void onChooseResult(String cateids) { + Log.e("cateids", cateids); + } + }); flowLayoutDialog.show(); diff --git a/flowlayout/src/main/java/com/lzy/flowlayout/CateBean.java b/flowlayout/src/main/java/com/lzy/flowlayout/CateBean.java new file mode 100644 index 0000000..79c6160 --- /dev/null +++ b/flowlayout/src/main/java/com/lzy/flowlayout/CateBean.java @@ -0,0 +1,98 @@ +package com.lzy.flowlayout; + +import java.util.List; + +/** + * Created by xiangcheng on 17/9/26. + */ + +public class CateBean { + /** + * ccid : 1 + * name : 颜色 + * childcatelist : [{"name":"红","idx":1,"cccid":1},{"name":"黄","idx":2,"cccid":2}] + * idx : 1 + */ + + private int ccid; + private String name; + private int idx; + private List childcatelist; + + public int getCcid() { + return ccid; + } + + public void setCcid(int ccid) { + this.ccid = ccid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getIdx() { + return idx; + } + + public void setIdx(int idx) { + this.idx = idx; + } + + public List getChildcatelist() { + return childcatelist; + } + + public void setChildcatelist(List childcatelist) { + this.childcatelist = childcatelist; + } + + public static class ChildcatelistBean { + /** + * name : 红 + * idx : 1 + * cccid : 1 + */ + + private String name; + private int idx; + private int cccid; + private boolean isSelect; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getIdx() { + return idx; + } + + public void setIdx(int idx) { + this.idx = idx; + } + + public int getCccid() { + return cccid; + } + + public void setCccid(int cccid) { + this.cccid = cccid; + } + + public boolean isSelect() { + return isSelect; + } + + public void setSelect(boolean select) { + isSelect = select; + } + } +} diff --git a/flowlayout/src/main/java/com/lzy/flowlayout/FlowLayoutDialog.java b/flowlayout/src/main/java/com/lzy/flowlayout/FlowLayoutDialog.java index ca323b9..1a83d8d 100644 --- a/flowlayout/src/main/java/com/lzy/flowlayout/FlowLayoutDialog.java +++ b/flowlayout/src/main/java/com/lzy/flowlayout/FlowLayoutDialog.java @@ -5,6 +5,7 @@ import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; @@ -16,12 +17,14 @@ public class FlowLayoutDialog extends AlertDialog { private Context context; - private List classifies; + private List cateBeanList; + private ProductAdapter productAdapter; + private ChooseResultListenner chooseResultListenner; - public FlowLayoutDialog(Context context, List classifies) { + public FlowLayoutDialog(Context context, List cateBeanList) { super(context); this.context = context; - this.classifies = classifies; + this.cateBeanList = cateBeanList; } protected FlowLayoutDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { @@ -44,7 +47,8 @@ protected void onCreate(Bundle savedInstanceState) { view.findViewById(R.id.ll_buttom).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - dismiss(); + String cateIds = productAdapter.getSelectedCateIds(); + chooseResultListenner.onChooseResult(cateIds); } }); @@ -58,10 +62,20 @@ public void onClick(View v) { RecyclerView productView = (RecyclerView) findViewById(R.id.product_view); productView.setLayoutManager(new LinearLayoutManager(context)); - productView.setAdapter(new ProductAdapter(context, classifies)); + productAdapter = new ProductAdapter(context, cateBeanList); + productView.setAdapter(productAdapter); + } private int dp2px(float value) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics()); } + + public void setChooseResultListenner(ChooseResultListenner chooseResultListenner) { + this.chooseResultListenner = chooseResultListenner; + } + + public interface ChooseResultListenner { + void onChooseResult(String cateids); + } } diff --git a/flowlayout/src/main/java/com/lzy/flowlayout/Product.java b/flowlayout/src/main/java/com/lzy/flowlayout/Product.java deleted file mode 100644 index af77ea2..0000000 --- a/flowlayout/src/main/java/com/lzy/flowlayout/Product.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.lzy.flowlayout; - -import java.util.List; - -/** - * Created by xiangcheng on 17/9/26. - */ - -public class Product { - public List classify; - - static public class Classify { - public String title; - List des; - - public Classify(String title, List des) { - this.title = title; - this.des = des; - } - - public static class Des { - boolean isSelect; - String des; - - public Des(String des) { - this.des = des; - } - } - - } -} diff --git a/flowlayout/src/main/java/com/lzy/flowlayout/ProductActivity.java b/flowlayout/src/main/java/com/lzy/flowlayout/ProductActivity.java deleted file mode 100644 index c8766c0..0000000 --- a/flowlayout/src/main/java/com/lzy/flowlayout/ProductActivity.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.lzy.flowlayout; - -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.Arrays; -import java.util.List; - -/** - * Created by xiangcheng on 17/9/26. - */ - -public class ProductActivity extends AppCompatActivity { - private static final String TAG = ProductActivity.class.getSimpleName(); - // private TextView suspension; - protected RecyclerView productView; - protected List classifies = new ArrayList<>(); - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.dialog_flow_layout); - productView = (RecyclerView) findViewById(R.id.product_view); - productView.setLayoutManager(new LinearLayoutManager(this)); - - classifies.add(new Product.Classify("年级", Arrays.asList( - new Product.Classify.Des("一年级"), - new Product.Classify.Des("二年级"), - new Product.Classify.Des("三年级"), - new Product.Classify.Des("四年级"), - new Product.Classify.Des("五年级"), - new Product.Classify.Des("六年级")))); - classifies.add(new Product.Classify("科目", Arrays.asList(new Product.Classify.Des("180"), - new Product.Classify.Des("175"), - new Product.Classify.Des("170"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("160"), - new Product.Classify.Des("155"), - new Product.Classify.Des("150")))); - classifies.add(new Product.Classify("科目", Arrays.asList(new Product.Classify.Des("180"), - new Product.Classify.Des("175"), - new Product.Classify.Des("170"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("160"), - new Product.Classify.Des("155"), - new Product.Classify.Des("150")))); - classifies.add(new Product.Classify("科目", Arrays.asList(new Product.Classify.Des("180"), - new Product.Classify.Des("175"), - new Product.Classify.Des("170"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("165"), - new Product.Classify.Des("160"), - new Product.Classify.Des("155"), - new Product.Classify.Des("150")))); - classifies.add(new Product.Classify("款式", - Arrays.asList(new Product.Classify.Des("男款"), new Product.Classify.Des("语文"), - new Product.Classify.Des("数学"), - new Product.Classify.Des("英语")))); - - productView.setAdapter(new ProductAdapter(this, classifies)); - } - -} diff --git a/flowlayout/src/main/java/com/lzy/flowlayout/ProductAdapter.java b/flowlayout/src/main/java/com/lzy/flowlayout/ProductAdapter.java index dd26c1d..8553db3 100644 --- a/flowlayout/src/main/java/com/lzy/flowlayout/ProductAdapter.java +++ b/flowlayout/src/main/java/com/lzy/flowlayout/ProductAdapter.java @@ -2,6 +2,7 @@ import android.content.Context; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; @@ -16,14 +17,30 @@ public class ProductAdapter extends RecyclerView.Adapter { private static final String TAG = ProductAdapter.class.getSimpleName(); - private List classifies; + private List cateBeanList; private Context context; - public ProductAdapter(Context context, List classifies) { + public ProductAdapter(Context context, List cateBeanList) { this.context = context; - this.classifies = classifies; + this.cateBeanList = cateBeanList; } + public String getSelectedCateIds() { + String cateids = ""; + for (int i = 0; i < cateBeanList.size(); i++) { + CateBean cateBean = cateBeanList.get(i); + + for (CateBean.ChildcatelistBean childcatelistBean : cateBean.getChildcatelist()) + if (childcatelistBean.isSelect()) { + cateids += cateBean.getCcid() + ":" + childcatelistBean.getCccid() + ","; + Log.d("isel", childcatelistBean.getName() + ""); + } + } + cateids = cateids.substring(0, cateids.length() - 1); + return cateids; + } + + @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ProductHolder(View.inflate(context, R.layout.product_item, null)); @@ -32,26 +49,27 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { final ProductHolder productHolder = (ProductHolder) holder; - Product.Classify classify = classifies.get(position); + CateBean cateBean = cateBeanList.get(position); final FlowLayoutManager flowLayoutManager = new FlowLayoutManager(); - productHolder.title.setText(classify.title); + productHolder.title.setText(cateBean.getName()); if (productHolder.itemView.getTag() == null) { productHolder.des.addItemDecoration(new SpaceItemDecoration(dp2px(7))); productHolder.itemView.setTag("item"); } // productHolder.des.addItemDecoration(new SpaceItemDecoration(dp2px(10))); productHolder.des.setLayoutManager(flowLayoutManager); - productHolder.des.setAdapter(new FlowAdapter(classify.des)); + FlowAdapter flowAdapter = new FlowAdapter(cateBean.getChildcatelist()); + productHolder.des.setAdapter(flowAdapter); } public String getTitle(int position) { - return classifies.get(position).title; + return cateBeanList.get(position).getName(); } @Override public int getItemCount() { - return classifies.size(); + return cateBeanList.size(); } class ProductHolder extends RecyclerView.ViewHolder { @@ -67,11 +85,13 @@ public ProductHolder(View itemView) { class FlowAdapter extends RecyclerView.Adapter { - private List list; - private Product.Classify.Des selectDes; + private List childcatelistBeanList; + + + private CateBean.ChildcatelistBean selectCate; - public FlowAdapter(List list) { - this.list = list; + public FlowAdapter(List childcatelistBeanList) { + this.childcatelistBeanList = childcatelistBeanList; } @Override @@ -83,25 +103,25 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { TextView textView = ((MyHolder) holder).text; - final Product.Classify.Des des = list.get(position); - if (des.isSelect) { + final CateBean.ChildcatelistBean childcatelistBean = childcatelistBeanList.get(position); + if (childcatelistBean.isSelect()) { textView.setTextColor(context.getResources().getColor(R.color.text_select_color)); textView.setBackground(context.getResources().getDrawable(R.drawable.product_item_select_back)); } else { textView.setTextColor(context.getResources().getColor(R.color.text_normal_color)); textView.setBackground(context.getResources().getDrawable(R.drawable.product_item_back)); } - textView.setText(des.des); + textView.setText(childcatelistBean.getName()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (des != selectDes) { - if (selectDes != null) { - selectDes.isSelect = false; + if (childcatelistBean != selectCate) { + if (selectCate != null) { + selectCate.setSelect(false); } } - des.isSelect = true; - selectDes = des; + childcatelistBean.setSelect(true); + selectCate = childcatelistBean; notifyDataSetChanged(); } }); @@ -109,7 +129,7 @@ public void onClick(View v) { @Override public int getItemCount() { - return list.size(); + return childcatelistBeanList.size(); } class MyHolder extends RecyclerView.ViewHolder { @@ -121,6 +141,10 @@ public MyHolder(View itemView) { text = (TextView) itemView.findViewById(R.id.flow_text); } } + + public CateBean.ChildcatelistBean getSelectCate() { + return selectCate; + } }