package com.bjzc.bhzhgd.base;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;

/**
 * Create By HuangWenFei
 * 创建日期：2023-01-11 15:17
 * 描述：
 */

public abstract class BaseAdapterViewBinding<T extends ViewBinding> extends BaseAdapter {

    private final LayoutInflater inflater;
    public BaseAdapterViewBinding(Context context) {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            T binding = getBinding(inflater, parent);
            holder = new ViewHolder(binding);
            convertView = binding.getRoot();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        handleData(position, holder.binding);
        return convertView;
    }

    protected abstract void handleData(int position, @NonNull T binding);

    protected abstract T getBinding(@NonNull LayoutInflater inflater, ViewGroup parent);

    class ViewHolder {

        private final T binding;
        public ViewHolder(T binding) {
            this.binding = binding;
        }
    }
}