Android-封装BaseAdapter

封装BaseAdapter

///java版本写法
public abstract class BaseListViewAdapter extends BaseAdapter {

    public final void notifyItemChanged(ListView listView, int position) {
        int visiblePosition = listView.getFirstVisiblePosition();
        if (position - visiblePosition >= 0) {
            View view = listView.getChildAt(position - visiblePosition);
            if (view != null) {
                VH viewHolder = (VH) view.getTag();
                onBindViewHolder(viewHolder, position);
            }
        }
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH holder;
        if (convertView == null) {
            int itemType = getItemViewType(position);
            holder = onCreateViewHolder(parent, itemType);
            holder.mItemViewType = itemType;
            convertView = holder.itemView;
            convertView.setTag(holder);
        } else {
            holder = (VH) convertView.getTag();
        }
        onBindViewHolder(holder, position);
        return convertView;
    }

    protected abstract VH onCreateViewHolder(ViewGroup parent, int viewType);

    protected abstract void onBindViewHolder(VH holder, int position);

    public static class BaseListViewHolder {

        public final View itemView;
        int mItemViewType;

        public BaseListViewHolder(View itemView) {
            this.itemView = itemView;
        }
    }
}
///kotlin版本写法
abstract class BaseListViewAdapter : BaseAdapter() {

    fun notifyItemChanged(listView: ListView, position: Int) {
        val visiblePosition = listView.firstVisiblePosition
        if (position - visiblePosition >= 0) {
            val view = listView.getChildAt(position - visiblePosition)
            if (view != null) {
                val viewHolder = view.tag as VH
                onBindViewHolder(viewHolder, position)
            }
        }
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var convertView = convertView
        val holder: VH
        if (convertView == null) {
            val itemType = getItemViewType(position)
            holder = onCreateViewHolder(parent, itemType)
            holder.mItemViewType = itemType
            convertView = holder.itemView
            convertView.tag = holder
        } else {
            holder = convertView.tag as VH
        }
        onBindViewHolder(holder, position)
        return convertView
    }

    protected abstract fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): VH

    protected abstract fun onBindViewHolder(holder: VH, position: Int)

    open class BaseListViewHolder(val itemView: View) {
        var mItemViewType = 0
    }
}

创建bean

public class Bean {

    public static final int TYPE_RECEIVE = 0;
    public static final int TYPE_SEND = 1;

    public int itemType;
    public String content;

    public Bean(int itemType, String content) {
        this.itemType = itemType;
        this.content = content;
    }
}

如何使用

public class MainActivity extends AppCompatActivity {

    private List items = new ArrayList();
    private ListView listView;
    private ListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listView);

        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                Bean bean = new Bean(Bean.TYPE_RECEIVE, "RECEIVE");
                items.add(bean);
            } else {
                Bean bean = new Bean(Bean.TYPE_SEND, "SEND");
                items.add(bean);
            }
        }

        adapter = new ListViewAdapter();
        listView.setAdapter(adapter);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bean bean = items.get(5);
                bean.content = "更新";
                adapter.notifyItemChanged(listView, 5);
            }
        });
    }

    class ListViewAdapter extends BaseListViewAdapter {

        @Override
        protected BaseListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == Bean.TYPE_RECEIVE) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_receive, parent, false);
                ReceiveViewHolder holder = new ReceiveViewHolder(view);
                return holder;
            } else {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_send, parent, false);
                SendViewHolder holder = new SendViewHolder(view);
                return holder;
            }
        }

        @Override
        protected void onBindViewHolder(BaseListViewHolder holder, int position) {
            Bean bean = items.get(position);
            if (holder instanceof ReceiveViewHolder) {
                ReceiveViewHolder receiveViewHolder = (ReceiveViewHolder) holder;
                receiveViewHolder.tv.setText(bean.content);
            } else {
                SendViewHolder sendViewHolder = (SendViewHolder) holder;
                sendViewHolder.tv.setText(bean.content);
            }
        }

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

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            return items.get(position).itemType;
        }
    }

    class SendViewHolder extends BaseListViewAdapter.BaseListViewHolder {

        public TextView tv;

        public SendViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }

    class ReceiveViewHolder extends BaseListViewAdapter.BaseListViewHolder {

        public TextView tv;

        public ReceiveViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}

你可能感兴趣的:(Android-封装BaseAdapter)