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;
}
}
ExpandableListAdapter
最新推荐文章于 2021-05-25 10:51:21 发布