zoukankan      html  css  js  c++  java
  • MyArrayAdapter 比较标准的写法

    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;
    }
    }

  • 相关阅读:
    .Net Core 自动化部署:使用jenkins部署到linux docker容器运行
    .Net Core 自动化部署:使用docker版jenkins部署dotnetcore应用
    React 使用browserHistory项目访问404问题
    使用Visual Studio Code和typescript 开发调试React Native项目
    java 基础
    javascript设计模式(1)——面向对象基础
    如何形成高度自律
    数据可视化-gojs插件使用技巧总结
    Git命令总结
    开源Git代码托管平台
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/6170282.html
Copyright © 2011-2022 走看看