规格

package com.example.joe.seven_1days;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;


import com.example.joe.seven_1days.GoodsDetails.Attr_listList;
import com.example.joe.seven_1days.GoodsDetails.Detail_listList;
import com.example.joe.seven_1days.GoodsDetails.GoodsDetails;
import com.example.joe.seven_1days.GoodsDetails.GoodsDetailsData;
import com.example.joe.seven_1days.GoodsDetails.ListList;


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


/**
 * Created by joe on 2018/2/26.
 */


public class PropertyAdapter extends BaseAdapter {






    private Context mContext;
    private List<Attr_listList> Attr_listListData;
    private List<Detail_listList> Detail_listListData;
    //    private ArrayList<HashMap<String,Object>> mList;
    private ArrayList<HashMap<String, TextView[]>> mViewList;
    private Drawable drawableNormal;
    private Drawable drawablePressed;
    private Handler mHandler;


    private LayoutInflater mLayoutInflater;


    private OnClickMyTextView mOnClickMyTextView;




    //用于保存用户的属性集合
    private HashMap<String, String> selectProMap = new HashMap<String, String>();
    private HashMap<String, String> selectTvMap = new HashMap<String, String>();




    /**
     * 返回选中的属性
     *
     * @return
     */
    public HashMap<String, String> getSelectProMap() {
        return selectProMap;
    }


    public void setSelectProMap(HashMap<String, String> selectProMap) {
        this.selectProMap = selectProMap;
    }


    public PropertyAdapter(Context context, List<Attr_listList> list, List<Detail_listList> list2) {


        super();
        mLayoutInflater = LayoutInflater.from(context);
//        this.mHandler=handler;
        this.mContext = context;
        this.Attr_listListData = list;
        this.Detail_listListData = list2;
        mViewList=new ArrayList<HashMap<String,TextView[]>>();
        drawableNormal = mContext.getResources().getDrawable(R.drawable.textshape12);
    }




    @Override
    public int getCount() {
        return Attr_listListData.size();
    }


    @Override
    public Object getItem(int position) {
        return Attr_listListData.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;


        Log.e("ccccccc", String.valueOf(position));
        if (convertView == null) {
            // 获取list_item布局文件的视图
            convertView = LayoutInflater.from(this.mContext).inflate(R.layout.lv_property_item, null, true);
            holder = new ViewHolder();


            // 获取控件对象
            holder.tvPropName = (TextView) convertView.findViewById(R.id.tv_property_name);
            holder.vgPropContents = (MyViewGroup) convertView.findViewById(R.id.myviewgroup);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Log.e("myname", "2121");
//        if (this.Attr_listListData != null) {
            HashMap<String,TextView[]> mapView=new HashMap<String, TextView[]>();


            List<ListList> aa = this.Attr_listListData.get(position).getList();


            String type = (String) this.Attr_listListData.get(position).getTitle();
            holder.tvPropName.setText(type);//规格名称
            holder.tvPropName.setTextColor(Color.BLACK);


//            Log.e("aaaaaaaaaa1111", String.valueOf(aa.size()));
            //动态加载标签
            //判断布局中的子控件是否为0,如果为0,就添加,防止ListView滚动时重复添加
        Log.e("bbbbbbbb", String.valueOf(holder.vgPropContents.getChildCount()));
            if (holder.vgPropContents.getChildCount() == 0) {
                TextView[] textViews = new TextView[aa.size()];
                Log.e("aaaaaaaaaa", String.valueOf(aa.size()));


                //设置每个标签的文本和布局
                //TableRow tr=new TableRow(mContext);


                for (int i = 0; i < aa.size(); i++) {
                    Log.e("aaaaaaaaaa2222", String.valueOf(i));
                    TextView textView = new TextView(mContext);
//                    textView.setGravity(17);
                    textView.setPadding(25, 15, 25, 15);




                    textViews[i] = textView;
                    textViews[i].setBackgroundResource(R.drawable.textshape12);
                    textViews[i].setText(aa.get(i).getName());
                    textViews[0].setBackgroundResource(R.drawable.textshape11);
                    textViews[0].setTextColor(Color.RED);






                    String path = aa.get(i).getPath();
                    boolean isClick = false;
                    for (int size = 0; size < Detail_listListData.size(); size++) {
                        String attr_id = Detail_listListData.get(size).getAttr_id();
                        if (attr_id.indexOf(path) != -1) {
                            isClick = true;
                        }
                    }
                    textViews[i].setEnabled(isClick);


                    textViews[i].setBackgroundResource(R.drawable.textshape12);
                    textViews[i].setTextColor(Color.BLACK);
                    textViews[i].setTag(i);
                    holder.vgPropContents.addView(textViews[i]);
                }
                //holder.tlPropContents.addView(tr);
                //绑定标签的Click事件
                for (int j = 0; j < textViews.length; j++) {
                    textViews[j].setTag(textViews);
                    textViews[j].setOnClickListener(new LableClickListener(type,aa.get(j).getPath(),textViews.length));
                }


//                把控件存起来
               mapView.put(type, textViews);
               mViewList.add(mapView);
            }
            /**判断之前是否已选中标签*/
            if (selectProMap.get(type) != null) {
                for (int h = 0; h < holder.vgPropContents.getChildCount(); h++) {
                    TextView v = (TextView) holder.vgPropContents.getChildAt(h);
                    if (selectProMap.get(type).equals(v.getText().toString())) {
                        v.setBackgroundResource(R.drawable.textshape11);
                        v.setTextColor(Color.RED);
                        selectProMap.put(type, v.getText().toString());
                    }
                }
            }
//        }
        return convertView;
    }


    public class ViewHolder {


        ImageView ivImg;
        TextView tvPropName;
        LinearLayout llPropContents;
        MyViewGroup vgPropContents;
        TableLayout tlPropContents;
    }


    public interface OnClickMyTextView {//创建一个接口类


        void myTextViewClick(int id);//创建一个回调函数,实例化接口的时候就要具体化这个回调函数,即要有函数体
    }


    //注册函数
    public void setOnClickMyTextView(OnClickMyTextView onClickMyTextView) {
        this.mOnClickMyTextView = onClickMyTextView;
    }




    class LableClickListener implements View.OnClickListener {
        private String type;
        private String path;
        private int le;


        public LableClickListener(String type, String path,int le) {


            this.type = type;
            this.path=path;
            this.le=le;
        }




        @Override
        public void onClick(View v) {


            TextView[] textViews = (TextView[]) v.getTag();
            TextView tv = (TextView) v;
            for (int i = 0; i < textViews.length; i++) {


//                String path = Attr_listListData.get(i).getList().get(i).getPath();
                //让点击的标签背景变成橙色,字体颜色变为红色
                if (tv.equals(textViews[i])) {


                    mOnClickMyTextView.myTextViewClick(i);




                        textViews[i].setBackgroundResource(R.drawable.textshape11);
                        textViews[i].setTextColor(Color.RED);


                  if (TextUtils.isEmpty(selectTvMap.get(type))){
                      selectTvMap.put(type,path);
                  }else {
                      selectTvMap.remove(type);
                  }


                    selectProMap.put(type, textViews[i].getText().toString());




                } else {
                    //其他标签背景变成灰色,字体颜色为黑色
                    //textViews[i].setBackgroundDrawable(drawableNormal);
                    textViews[i].setBackgroundResource(R.drawable.textshape12);
                    textViews[i].setTextColor(Color.BLACK);


                }


                if (selectTvMap!=null&&selectTvMap.size()==le){
                    String str ="";
                    for (Map.Entry<String, String> maps:selectTvMap.entrySet() ) {
                      str+=  maps.getValue();


                    }


                    Log.e("Tag",str);


                }




            }


        }




    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值