美文网首页
android Baseadapter 和 ViewHolder

android Baseadapter 和 ViewHolder

作者: 银弹星空 | 来源:发表于2021-07-17 11:03 被阅读0次
/**
 * 主页菜单适配器
 */
public class MainMenuAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private List<Authority> authorityList;

    public MainMenuAdapter(Context context, List<Authority> authorityList) {
        this.layoutInflater = LayoutInflater.from(context);
        this.authorityList = authorityList;
    }

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

    @Override
    public Authority getItem(int position) {
        return authorityList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.main_menu_item, parent, false);
            holder.imageView = convertView.findViewById(R.id.btn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Authority authority = authorityList.get(position);
        holder.imageView.setImageResource(authority.getImage());
        return convertView;
    }
    class ViewHolder {
        ImageView imageView;
    }

}

相关文章

网友评论

      本文标题:android Baseadapter 和 ViewHolder

      本文链接:https://www.haomeiwen.com/subject/thqwpltx.html