Can write common adapter for different views? I am interested in common adapter for recyclerView
and spinner
. In my project i have RecyclerView
and Spinner and i want to write base generic adapter for both (RecyclerView
and Spinner
)
Can you help me?
Common adapter for spinner and recyclerView
Yes, You can implement 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 ListView
/RecyclerView
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
.
Its great! Thank you –
Complex
Here is my sample with cursor example for androidx. The idea is to have a common text view id for both. You can check it in xml file provided by android.R.layout.simple_spinner_item and android.R.layout.simple_list_item_1.
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
/**
* Common adapter for cursor.
*/
public class CommonAdapter extends RecyclerView.Adapter<CommonAdapter.ViewHolder> {
private Cursor mCursor;
private boolean isSpinner;
public CommonAdapter(boolean isSpinner){
this.isSpinner = isSpinner;
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(parent, isSpinner);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (mCursor.moveToPosition(position)) {
holder.mText.setText(mCursor.getString(
mCursor.getColumnIndexOrThrow(Data.COLUMN_ID)));
}
}
@Override
public int getItemCount() {
return mCursor == null ? 0 : mCursor.getCount();
}
void setCursor(Cursor cursor) {
mCursor = cursor;
notifyDataSetChanged();
}
static class ViewHolder extends RecyclerView.ViewHolder {
final TextView mText;
ViewHolder(ViewGroup parent, boolean isSpinner) {
super(LayoutInflater.from(parent.getContext()).inflate(
isSpinner?android.R.layout.simple_spinner_item:android.R.layout.simple_list_item_1
, parent, false));
mText = itemView.findViewById(android.R.id.text1);
}
}
}
© 2022 - 2024 — McMap. All rights reserved.