package com.ali.fridge.supermarket.module;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView; import com.ali.fridge.R;
import com.ali.fridge.util.LogUtils; import java.util.List; import static com.ali.fridge.util.LogUtils.makeLogTag; /**
* Created by xiaomin.wxm on 2016/4/7.
*/
public class MyArrayAdapter extends ArrayAdapter<String> { private static final String TAG = makeLogTag(MyArrayAdapter.class);
private Context mContext;
private List<String> mString;
private LayoutInflater mInflater;
private boolean mFirst = true;
private int mNowItemSelected;
private MyViewHolder holder; public MyArrayAdapter(Context context, int textViewResourceId, List<String> aStrings) {
super(context, textViewResourceId, aStrings);
mString = aStrings;
mContext = context;
/*if (mString == null) {
mString = new ArrayList<>();
mString.add("推荐");
}*/
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new MyViewHolder();
mInflater = LayoutInflater.from(mContext);
convertView = mInflater.inflate(R.layout.goods_item_onecategory, null);
holder.categoryName = (TextView) convertView.findViewById(R.id.goods_tv_category);
holder.ivVerticalBar = (ImageView) convertView.findViewById(R.id.goods_img_category_selector);
convertView.setTag(holder);
} else {
holder = (MyViewHolder) convertView.getTag();
}
holder.categoryName.setText(mString.get(position));
if (position == mNowItemSelected) {
holder.ivVerticalBar.setVisibility(View.VISIBLE);
} else {
holder.ivVerticalBar.setVisibility(View.GONE);
}
return convertView;
} public void setNewData(List<String> aStrings) {
LogUtils.LOGD(TAG, "setNewData begin");
clear();
mString = aStrings;
/*if (mString == null) {
mString = new ArrayList<>();
mString.add("推荐");
}*/
if (mString != null) {
for (int i = 0; i < mString.size(); i++) {
insert(mString.get(i), i);
}
}
notifyDataSetChanged();
LogUtils.LOGD(TAG, "setNewData end");
} /*
index : selectitem index <0 means not selected
*/
public void setSelected(int index) {
mNowItemSelected = index;
notifyDataSetChanged();
} private class MyViewHolder {
TextView categoryName;
ImageView ivVerticalBar;
}
}
05-01 05:15