ExpandableListAdapter

package com.hitheone.theone.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hitheone.theone.Bean.CollocationBean;
import com.hitheone.theone.R;
import com.hitheone.theone.utils.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class HistoryrecordAdapter extends BaseExpandableListAdapter {

    //单元类
    class ExpandableListHolder {
        ImageView image;
        TextView name,pinpai,price;
    }

    //父单元
    class ExpandableGroupHolder {
        ImageView image;
        TextView tag,detail,count,check;
    }

    private List<CollocationBean> data=new ArrayList<CollocationBean>();
    private Context mContext;
    private LayoutInflater mGroupInflater; //用于加载group的布局xml
    private LayoutInflater mChildInflater; //用于加载listitem的布局xml

    //自宝义构造
    public HistoryrecordAdapter(Context context, List<CollocationBean> mdata) {
        this.data=mdata;
        this.mContext=context;
        mChildInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mGroupInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    //必须实现 得到子数据
    @Override
    public Object getChild(int groupPosition, int j) {
        return data.get(groupPosition).getCollocation_goods().get(j);

    }

    @Override
    public long getChildId(int groupPosition, int j) {
        return groupPosition;
    }

    @Override
    public int getChildrenCount(int i) {
        return data.get(i).getCollocation_goods().size();
    }

    @Override
    public Object getGroup(int i) {
        return data.get(i);
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public boolean hasStableIds() {//行是否具有唯一id
        return false;
    }

    @Override
    public boolean isChildSelectable(int i, int j) {//行是否可选
        return true;
    }
    @SuppressLint("NewApi")
    public void updateBackground(int position, TextView it) {
        int backgroundId;
        if (!data.get(position).getIscheck()) {
            backgroundId = R.mipmap.checked1;
            data.get(position).setIscheck(true);
        } else {
            backgroundId = R.mipmap.checked2;
            data.get(position).setIscheck(false);
        }
        Drawable background = ContextCompat.getDrawable(mContext, backgroundId);
        it.setBackground(background);
    }
    @Override
    public View getGroupView(final int groupPosition, boolean flag, View convertView, ViewGroup viewgroup) {

        ExpandableGroupHolder holder = null; //清空临时变量holder
        if (convertView == null) { //判断view(即view是否已构建好)是否为空

            convertView = mGroupInflater.inflate(R.layout.favoriteslistitem1, null);
            holder = new ExpandableGroupHolder();
            holder.detail=(TextView) convertView.findViewById(R.id.detail);
            holder.tag=(TextView) convertView.findViewById(R.id.tag);
            holder.count=(TextView) convertView.findViewById(R.id.collect);
            holder.check=(TextView) convertView.findViewById(R.id.ischeck);
            holder.image=(ImageView) convertView.findViewById(R.id.image);

            convertView.setTag(holder);
        } else { //若view不为空,直接从view的tag属性中获得各子视图的引用
            holder = (ExpandableGroupHolder) convertView.getTag();
        }
        if(!data.get(groupPosition).getFlag()){
            holder.check.setVisibility(View.GONE);
        }else{
            holder.check.setVisibility(View.VISIBLE);
        }
        if(data.get(groupPosition).getIscheck()){
            Drawable background = ContextCompat.getDrawable(mContext, R.mipmap.checked1);
            holder.check.setBackground(background);
        }else{
            Drawable background = ContextCompat.getDrawable(mContext, R.mipmap.checked2);
            holder.check.setBackground(background);
        }
        holder.tag.setText(data.get(groupPosition).getCollocation_tag());
        holder.detail.setText(data.get(groupPosition).getCollocation_title());
        holder.count.setText(data.get(groupPosition).getCollocation_like_num() + "");
        Glide.with(mContext).load(data.get(groupPosition).getCollocation_image()).into(holder.image);
        final TextView it=holder.check;
        holder.check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateBackground(groupPosition, it);
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             ViewGroup viewgroup) {
        ExpandableListHolder holder = null;
        if (convertView == null) {
            convertView = mChildInflater.inflate(R.layout.favoriteslistitemdetail1, null);
            holder = new ExpandableListHolder();
            holder.name=(TextView)convertView.findViewById(R.id.name);
            holder.pinpai=(TextView)convertView.findViewById(R.id.pinpai);
            holder.price=(TextView)convertView.findViewById(R.id.price);
            holder.image=(ImageView)convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else {//若行已初始化,直接从tag属性获得子视图的引用
            holder = (ExpandableListHolder) convertView.getTag();
        }
        holder.name.setText(data.get(groupPosition).getCollocation_goods().get(childPosition).getGoods_name());
        holder.pinpai.setText(data.get(groupPosition).getCollocation_goods().get(childPosition).getBrand_name());
        holder.price.setText("¥" + TextUtils.setPrice_twodecimal(data.get(groupPosition).getCollocation_goods().get(childPosition).getGoods_price() + ""));
        Glide.with(mContext).load(data.get(groupPosition).getCollocation_goods().get(childPosition).getGoods_image()).into(holder.image);
        return convertView;
    }
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值