版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoshuxgh/article/details/85062616
效果图:
点击规格根据返回的json数组里面作比较,然后代理颜色变化 ,需求就是有代理资格的话,代理这显示进货按钮,咨询代理商显示的是价格,没代理资格的情况显示代理,及咨询代理商。这是个去年写过的需求,去年的加入购物车选规格,类似天猫,今年需求变化,修改选取规格加入购物车类似京东。需求订了,那就改呗
布局还是一个RecycleView,item是flowlayout+textview
<?xml version="1.0" encoding="utf-8"?>
<com.zhy.autolayout.AutoRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.zhy.autolayout.AutoRelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="885px"
android:layout_alignParentBottom="true"
android:background="@color/white">
<!--headIcon-->
<com.zhy.autolayout.AutoRelativeLayout
android:id="@+id/relative_head_icon"
android:layout_width="200px"
android:layout_height="200px"
android:layout_marginBottom="40px"
android:layout_marginLeft="40px"
android:padding="10px">
<!--<ImageView-->
<!--android:id="@+id/iv_user_header"-->
<!--android:layout_width="200px"-->
<!--android:layout_height="200px"-->
<!--android:src="@drawable/default_head_icon"-->
<!---->
<!-- />-->
</com.zhy.autolayout.AutoRelativeLayout>
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40px"
android:layout_marginTop="30px"
android:layout_toRightOf="@+id/relative_head_icon"
android:text="¥799"
android:textColor="#ffe66363" />
<ImageView
android:id="@+id/iv_close"
android:layout_width="40px"
android:layout_height="40px"
android:layout_alignParentRight="true"
android:layout_gravity="center_horizontal"
android:layout_marginRight="40px"
android:layout_marginTop="15px"
android:src="@drawable/close" />
<TextView
android:id="@+id/tv_goods_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_price"
android:layout_below="@+id/tv_price"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10px"
android:gravity="center"
android:text=""
android:textColor="#ff9b9b9b"
android:textSize="10sp" />
<TextView
android:id="@+id/tv_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_price"
android:layout_below="@+id/tv_goods_count"
android:layout_marginTop="10px"
android:ellipsize="end"
android:maxEms="18"
android:singleLine="true"
android:text="选择 套餐类型 颜色分类"
android:textColor="@color/black"
android:textSize="26px" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignLeft="@+id/relative_head_icon"
android:layout_below="@+id/relative_head_icon"
android:background="#E2E2E2" />
<ScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="550px"
android:layout_below="@+id/view"
android:scrollbars="none">
<com.zhy.autolayout.AutoLinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/gr_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/relative_head_icon"
android:layout_marginTop="20px" />
<com.zhy.autolayout.AutoRelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="80px"
android:layout_above="@+id/view2"
android:layout_marginTop="15px"
android:background="@color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="40px"
android:text="数量"
android:textColor="#4a4a4a" />
<TextView
android:id="@+id/tv_add"
android:layout_width="50px"
android:layout_height="50px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="36px"
android:background="@drawable/tv_add_bg"
android:gravity="center"
android:text="+"
android:textSize="46px" />
<EditText
android:id="@+id/et_gp_num"
android:layout_width="50px"
android:layout_height="50px"
android:layout_centerVertical="true"
android:layout_marginRight="20px"
android:layout_toLeftOf="@+id/tv_add"
android:background="@drawable/tv_add_bg"
android:cursorVisible="false"
android:gravity="center"
android:inputType="number"
android:text="1"
android:textSize="32px" />
<TextView
android:id="@+id/tv_jian"
android:layout_width="50px"
android:layout_height="50px"
android:layout_centerVertical="true"
android:layout_marginRight="20px"
android:layout_toLeftOf="@+id/et_gp_num"
android:background="@drawable/tv_add_bg"
android:gravity="center"
android:text="-"
android:textSize="46px" />
</com.zhy.autolayout.AutoRelativeLayout>
</com.zhy.autolayout.AutoLinearLayout>
</ScrollView>
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignLeft="@+id/relative_head_icon"
android:layout_below="@+id/sc"
android:background="#E2E2E2" />
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_above="@+id/ll"
android:background="#E2E2E2" />
<com.zhy.autolayout.AutoLinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="90px"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<com.zhy.autolayout.AutoRelativeLayout
android:id="@+id/rl_cart"
android:layout_width="200px"
android:layout_height="match_parent"
android:padding="5px">
<ImageView
android:id="@+id/mall_purche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8px"
android:src="@drawable/mall_cart_gray" />
<TextView
android:id="@+id/iv_red_count"
android:layout_width="28px"
android:layout_height="28px"
android:layout_toRightOf="@+id/mall_purche"
android:background="@drawable/shape_hint_bg"
android:gravity="center"
android:padding="2px"
android:text="99"
android:textColor="@color/white"
android:textSize="8sp"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mall_purche"
android:layout_centerHorizontal="true"
android:layout_marginTop="5px"
android:text="购物车"
android:textColor="#ff4a4a4a"
android:textSize="10sp" />
</com.zhy.autolayout.AutoRelativeLayout>
<TextView
android:id="@+id/tv_pay"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffe66363"
android:gravity="center"
android:text="咨询购买"
android:textColor="@color/white"
android:textSize="16sp" />
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#E2E2E2" />
<TextView
android:id="@+id/tv_add_cart"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FDD223"
android:gravity="center"
android:text="代理"
android:textColor="@color/white"
android:textSize="16sp" />
</com.zhy.autolayout.AutoLinearLayout>
</com.zhy.autolayout.AutoRelativeLayout>
<ImageView
android:id="@+id/iv_user_header"
android:layout_width="200px"
android:layout_height="200px"
android:layout_above="@id/relative"
android:layout_marginBottom="-150px"
android:layout_marginLeft="40px"
android:src="@drawable/accountdefault" />
</com.zhy.autolayout.AutoRelativeLayout>
布局文件已经贴上。请自行查看。
下面稍微复杂的就是选取规格,并进行拼接,比较,下面控件颜色显示
下面贴一下实现代码:代码看起来稍微复杂一些,后面再优化:
try {
final ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.mTvTopic.setText(strKey.get(position));
if (strName.size() != 0) {
strName.clear();
}
listModel = new ArrayList<>();
for (int i = 0; i < mallProBean.getJSONArray(strKey.get(position)).length(); i++) {
listModel.add(mallProBean.getJSONArray(strKey.get(position)).get(i).toString());
}
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
strAll = "";
for(int j=0;j<strKey.size();j++){
if (strAll.equals("")) {
strAll = object.getString(strKey.get(j)) + "_";
} else {
strAll = strAll + object.getString(strKey.get(j))+"_";
}
mapAll.put(strAll,object.getString("skuId"));
}
}
for (int i = 0; i < listModel.size(); i++) {
final TextView textView = new TextView(context);
textView.setText(listModel.get(i));
if (listModel.get(i).equals(mapSelected.get(strKey.get(position)).toString())) {
textView.setTextColor(Color.WHITE);
textView.setBackgroundResource(R.drawable.round_rectangle_bg1);
textView.setTag(true);
strName.put(strKey.get(position), listModel.get(i));
} else {
textView.setTextColor(Color.parseColor("#4A4A4A"));
textView.setBackgroundResource(R.drawable.round_rectangle_bg2);
textView.setTag(false);
}
mapId.put(textView, (Boolean) textView.getTag());
for(int j=0;j<strKey.size();j++){
for(String key:mapSelected.keySet()){
if(strKey.get(j).equals(key)){
Model_Value.put(key, mapSelected.get(key));
}
}
}
for (String key : Model_Value.keySet()) {
value = Model_Value.get(key);
if (!TextUtils.isEmpty(value)) {
if (type.equals("")) {
type = value + "_";
} else {
type = type + value + "_";
}
}
}
for(String key :mapAll.keySet()) {
if (key.equals(type)) {
Log.e("JSONArray12", mapAll.get(key));
proId = mapAll.get(key);
} else {
}
}
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
marginLayoutParams.topMargin = 20;
marginLayoutParams.rightMargin = 20;
textView.setLayoutParams(marginLayoutParams);
textView.setTextSize(10);
textView.setPadding(25, 15, 25, 15);
attributeListMap.put(listModel.get(i), textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int finalI = 0;
boolean flag1 = (boolean) textView.getTag();
for (String key : mapStyle.keySet()) {
JSONArray array1 = null;
try {
array1 = mapStyle.get(key).getJSONArray(key);
if(strKey.get(position).equals(key)){
listModel.clear();
for (int i = 0; i < array1.length(); i++) {
listModel.add(array1.get(i).toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.e("flag1", flag1 + "");
if (flag1) {
textView.setTag(false);
textView.setTextColor(Color.parseColor("#4A4A4A"));
textView.setBackgroundResource(R.drawable.round_rectangle_bg2);
Model_Value.put(strKey.get(position), "");
strName.put(strKey.get(position), "");
mapSelected.put(strKey.get(position), "");
mapId.put(textView,flag1);
} else {
boolean flag = (boolean) textView.getTag();
textView.setTag(!flag);
if (!flag) {
for (int i = 0; i < listModel.size(); i++) {
// Log.e("JSONArray8", listModel.get(i));
for(TextView str : mapId.keySet()){
if(listModel.get(i).equals(str.getText().toString())){
if(mapId.get(str)){
str.setTag(false);
str.setTextColor(Color.parseColor("#4A4A4A"));
str.setBackgroundResource(R.drawable.round_rectangle_bg2);
mapId.put(str,false);
}
}
}
}
Log.e("flag1??", flag1 + "");
textView.setTextColor(Color.WHITE);
textView.setBackgroundResource(R.drawable.round_rectangle_bg1);
mapSelected.put(strKey.get(position), textView.getText().toString());
mapId.put(textView, !flag);
strName.put(strKey.get(position), textView.getText().toString());
Model_Value.put(strKey.get(position), textView.getText().toString());
} else {
textView.setTextColor(Color.parseColor("#4A4A4A"));
textView.setBackgroundResource(R.drawable.round_rectangle_bg2);
}
}
type = "";
typeName = "";
str = "";
for (String key : Model_Value.keySet()) {
value = Model_Value.get(key);
if (!TextUtils.isEmpty(value)) {
if (type.equals("")) {
type = value + "_";
} else {
type = type + value + "_";
}
}
}
for (String key : strName.keySet()) {
typeName = strName.get(key);
if (str.equals("")) {
str = typeName + "";
} else {
str = str + " " + typeName;
}
}
mTvSelect.setText("选择 " + str);
Log.e("TAGresponse=", str + " type " + type);
try {
for(String key :mapAll.keySet()){
if(key.equals(type)){
proId = mapAll.get(key);
getGoodPrice( mFromMember_ID, Pro_InfoId, shopInfoId);
}else{
mTvAddCart.setBackgroundColor(Color.parseColor("#9B9B9B"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
viewHolder.flowLayout.addView(textView);
}
} catch (JSONException e) {
e.printStackTrace();
}
主要是recycleview里面实现的代码逻辑,后续会有一个小demo供参考,未完待续。。。。