Александр Александр -4 years ago 136
Android Question

Common adapter for spinner and recyclerView

Can write common adapter for different views? I am interested in common adapter for

recyclerView
and
spinner
. In my project i has
RecyclerView
and Spinner and i want to write base generic adapter for both (
RecyclerView
and
Spinner
)
Can you help me?

Answer Source

Yes, You can immplement a common adapter for listview/spinner and Recyclerview

public static class CommonAdapter extends RecyclerView.Adapter<CommonAdapter.RecycleViewHolder>
            implements ListAdapter {


        private Context mContext;
        private List<String> data;

        public CommonAdapter(Context context, List<String> data) {
            mContext = context;
            this.data = data;
        }

        // Start of recycleview adapter
        @Override
        public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new RecycleViewHolder(new TextView(mContext));
        }

        @Override
        public void onBindViewHolder(RecycleViewHolder holder, int position) {
            holder.textView.setText(data.get(position));
        }

        @Override
        public int getItemCount() {
            return getCount();
        }

        public static class RecycleViewHolder extends RecyclerView.ViewHolder {

            public TextView textView;

            public RecycleViewHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView;
            }
        }

        // Implementation of list adapter(Spinner/ListView)
        @Override
        public boolean areAllItemsEnabled() {
            return true;
        }

        @Override
        public boolean isEnabled(int position) {
            return true;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {

        }

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

        @Override
        public String getItem(int position) {
            return data.get(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SpinnerViewHolder holder;
            if (convertView == null) {
                convertView = new TextView(mContext);
                holder = new SpinnerViewHolder();
                holder.textView = (TextView) convertView;
                convertView.setTag(holder);
            } else {
                holder = (SpinnerViewHolder) convertView.getTag();
            }
            holder.textView.setText(getItem(position));
            return convertView;
        }

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

        @Override
        public boolean isEmpty() {
            return false;
        }

        public static class SpinnerViewHolder {
            public TextView textView;
        }

    }

This is the basic idea for doing that, You can genirify even more to use the same adapter for all the different list/recyclerviews with different datatypes too. But as of now I am giving the basic idea of combined adapters.

The Adapter is extending the RecyclerView.ViewHolder which is required by RecyclerView and I am implementing the same class to ListAdapter which is required by ListView/Spinner.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download