可以为不同的视图编写通用适配器吗?我对spinner
和RecyclerView
的通用适配器感兴趣。在我的项目中,我有RecyclerView
和Spinner,我想为两者(Spinner
和function checkForRepeatingDigits(N){
var arr = (''+N).split(''),
result = arr.filter((elem, i) => arr.indexOf(elem) == i);
return result.length != (''+N).length;
}
// Or
function checkForRepeatingDigits(N){
return [...new Set((''+N).split(''))].length != (''+N).length;
}
console.log([1, 10, 11, 1010, 1981].map(checkForRepeatingDigits))
)编写基础通用适配器
你能救我吗?
答案 0 :(得分:9)
是的,您可以为ListView
/ Spinner
和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;
}
}
这是执行此操作的基本思路,您可以更多地使用相同的适配器来为具有不同数据类型的所有不同ListView
/ RecyclerView
使用相同的适配器。但到目前为止,我给出了组合适配器的基本概念。
适配器正在扩展RecyclerView.ViewHolder
所需的RecyclerView
,并且我正在ListAdapter
/ ListView
所需的Spinner
实施相同的课程
答案 1 :(得分:0)
这是我的 androidx 的带有光标的示例。想法是为这两个都有一个通用的文本视图ID。您可以在 android.R.layout.simple_spinner_item 和 android.R.layout.simple_list_item_1 提供的xml文件中进行检查。
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);
}
}
}