懒人ListView适配器

懒人ListView适配器

适配器

public abstract class CommonAdaper<T> extends BaseAdapter {
    private Context context;
    private List<T> list;


    public CommonAdaper(Context context, List<T> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = ViewHolder.get(view, viewGroup, 列表子项布局的id, i);
        convert(holder, getItem(i));
        return holder.getConvertView();
    }

    public abstract void convert(ViewHolder holder, T item);

}

ViewHolder【这个是核心:不变的在CommonAdaper,变得在ViewHolder】

public class ViewHolder {
    //现在对于int作为键的官方推荐用SparseArray替代HashMap
    private final SparseArray<View> views;
    private int position;
    private View convertView;
    private Context context;

    private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.context = context;
        this.views = new SparseArray<>();
        this.convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        convertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     */
    public static ViewHolder get(View convertView, ViewGroup parent, int layoutId, int position) {
        if (convertView == null) {
            return new ViewHolder(parent.getContext(), parent, layoutId, position);
        }
        return (ViewHolder) convertView.getTag();
    }

    /**
     * 通过控件的Id获取对于的控件,如果没有则加入views
     */
    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = convertView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return convertView;
    }

    /**
     * 设置字符串
     */
    public ViewHolder setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    /**
     * 设置图片
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView iv = getView(viewId);
        iv.setImageResource(drawableId);
        return this;
    }

    /**
     * 设置图片
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView iv = getView(viewId);
        iv.setImageBitmap(bitmap);
        return this;
    }

    public int getPosition() {
        return position;
    }
}

Activity中调用【结合你的实际需求写】

public class XXActivity extends Activity {
    //声明控件
    private ListView listView;
    //声明数据
    private List<实体类> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_school);

        listView = (ListView) findViewById(列表对应的id);
        initList();

        listView.setAdapter(new CommonAdaper<实体类>(this, list) {
            @Override
            public void convert(ViewHolder holder, SchoolData item) {
                holder.setText(列表子项的TextView对应的的id, item.getName());
                holder.setImageResource(列表子项ImageView对应的id, item.getImg_id());
            }
        });
    }

    private void initList() {
        list = new ArrayList<实体类>();
        for (int i = 0; i < 10; i++) {
            list.add(new SchoolData());
        }
    }
}

布局就不写了,你能收到说明你的前面的工作应该没问题(相信你)!!


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风御浪云帆之上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值