如何使用 Android 注释在 RecyclerView ViewHolder 中获取 AdapterPosition
how to getAdapterPosition in RecyclerView ViewHolder with Android Annotations
我使用 android 注释并创建了 Recycler List ,问题是,如果我删除 holder 中的某些内容并使用 notifyItemRemoved(position);
通过监听器更新适配器。 holder 中的位置没有更新。
@EBean
public static class CheckListAdapter extends RecyclerViewAdapterBase<LinkedHashMap<String, String>, HolderItemView>
implements AdapterListener {
private final Context mContext;
public CheckListAdapter(Context context) {
mContext = context;
this.items = new ArrayList<>();
}
@Override
protected HolderItemView onCreateItemView(ViewGroup parent, int viewType) {
return CheckListFragment_.HolderItemView_.build(mContext);
}
public void updateAdapter(ArrayList<LinkedHashMap<String, String>> list) {
items.clear();
items.addAll(list);
notifyDataSetChanged();
}
public void addData(LinkedHashMap<String, String> map) {
items.add(map);
notifyItemChanged(items.size() - 1);
}
@Override
public void onBindViewHolder(ViewWrapper<HolderItemView> holder, int position) {
HolderItemView view = holder.getView();
view.bind(items, position, this);
}
@Override
public void itemRemoved(int position) {
notifyItemRemoved(position);
}
}
HolderItemView.java
@EViewGroup(R.layout.item_checklist)
public static class HolderItemView extends LinearLayout {
private Context mContext;
@ViewById
AppCompatCheckBox checkbox;
@ViewById
TextView checklist_tv;
@ViewById
EditText checklist_et;
@ViewById
ImageView deleteButton;
private AdapterListener mListener;
private List<LinkedHashMap<String, String>> mainMap;
private int position;
public HolderItemView(Context context) {
super(context);
mContext = context;
}
public void bind(List<LinkedHashMap<String, String>> mainMap, int position, AdapterListener mListener) {
this.mainMap = mainMap;
this.position = position;
this.mListener = mListener;
if (mainMap.get(position).get("title").isEmpty()) {
checklist_tv.setVisibility(View.GONE);
checklist_et.setVisibility(View.VISIBLE);
checklist_tv.setText("");
checklist_et.setText("");
} else {
checklist_tv.setText(mainMap.get(position).get("title"));
}
checklist_et.setOnEditorActionListener(editListener);
checkbox.setOnCheckedChangeListener(checkChangeListener);
}
@Click
public void checklist_tv() {
checklist_et.setText(checklist_tv.getText());
checklist_tv.setVisibility(View.GONE);
checklist_et.setVisibility(View.VISIBLE);
}
@Click
public void deleteButton() {
mainMap.remove(position);
mListener.itemRemoved(position);
}
private TextView.OnEditorActionListener editListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
checklist_et.setVisibility(View.GONE);
checklist_tv.setVisibility(View.VISIBLE);
checklist_tv.setText(checklist_et.getText());
InputMethodManager in = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(checklist_et.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
};
private CompoundButton.OnCheckedChangeListener checkChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checklist_tv.setPaintFlags(checklist_tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
checklist_tv.setPaintFlags(checklist_tv.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
}
};
}
public interface AdapterListener {
void itemRemoved(int position);
}
RecyclerViewAdapterBase.java
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public abstract class RecyclerViewAdapterBase<T, V extends View> extends RecyclerView.Adapter<ViewWrapper<V>> {
protected List<T> items;
@Override
public int getItemCount() {
return items.size();
}
@Override
public final ViewWrapper<V> onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewWrapper<>(onCreateItemView(parent, viewType));
}
protected abstract V onCreateItemView(ViewGroup parent, int viewType);
// additional methods to manipulate the items
}
ViewWrapper.java
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class ViewWrapper<V extends View> extends RecyclerView.ViewHolder {
private V view;
public ViewWrapper(V itemView) {
super(itemView);
view = itemView;
}
public V getView() {
return view;
}
}
从回收商视图中删除项目后,您需要通知项目范围更改。所以你需要做这样的事情
notifyItemRemoved(position);.
notifyItemRangeChanged(position, arrayList.size());
我使用 android 注释并创建了 Recycler List ,问题是,如果我删除 holder 中的某些内容并使用 notifyItemRemoved(position);
通过监听器更新适配器。 holder 中的位置没有更新。
@EBean
public static class CheckListAdapter extends RecyclerViewAdapterBase<LinkedHashMap<String, String>, HolderItemView>
implements AdapterListener {
private final Context mContext;
public CheckListAdapter(Context context) {
mContext = context;
this.items = new ArrayList<>();
}
@Override
protected HolderItemView onCreateItemView(ViewGroup parent, int viewType) {
return CheckListFragment_.HolderItemView_.build(mContext);
}
public void updateAdapter(ArrayList<LinkedHashMap<String, String>> list) {
items.clear();
items.addAll(list);
notifyDataSetChanged();
}
public void addData(LinkedHashMap<String, String> map) {
items.add(map);
notifyItemChanged(items.size() - 1);
}
@Override
public void onBindViewHolder(ViewWrapper<HolderItemView> holder, int position) {
HolderItemView view = holder.getView();
view.bind(items, position, this);
}
@Override
public void itemRemoved(int position) {
notifyItemRemoved(position);
}
}
HolderItemView.java
@EViewGroup(R.layout.item_checklist)
public static class HolderItemView extends LinearLayout {
private Context mContext;
@ViewById
AppCompatCheckBox checkbox;
@ViewById
TextView checklist_tv;
@ViewById
EditText checklist_et;
@ViewById
ImageView deleteButton;
private AdapterListener mListener;
private List<LinkedHashMap<String, String>> mainMap;
private int position;
public HolderItemView(Context context) {
super(context);
mContext = context;
}
public void bind(List<LinkedHashMap<String, String>> mainMap, int position, AdapterListener mListener) {
this.mainMap = mainMap;
this.position = position;
this.mListener = mListener;
if (mainMap.get(position).get("title").isEmpty()) {
checklist_tv.setVisibility(View.GONE);
checklist_et.setVisibility(View.VISIBLE);
checklist_tv.setText("");
checklist_et.setText("");
} else {
checklist_tv.setText(mainMap.get(position).get("title"));
}
checklist_et.setOnEditorActionListener(editListener);
checkbox.setOnCheckedChangeListener(checkChangeListener);
}
@Click
public void checklist_tv() {
checklist_et.setText(checklist_tv.getText());
checklist_tv.setVisibility(View.GONE);
checklist_et.setVisibility(View.VISIBLE);
}
@Click
public void deleteButton() {
mainMap.remove(position);
mListener.itemRemoved(position);
}
private TextView.OnEditorActionListener editListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
checklist_et.setVisibility(View.GONE);
checklist_tv.setVisibility(View.VISIBLE);
checklist_tv.setText(checklist_et.getText());
InputMethodManager in = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(checklist_et.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
};
private CompoundButton.OnCheckedChangeListener checkChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checklist_tv.setPaintFlags(checklist_tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
checklist_tv.setPaintFlags(checklist_tv.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
}
};
}
public interface AdapterListener {
void itemRemoved(int position);
}
RecyclerViewAdapterBase.java
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public abstract class RecyclerViewAdapterBase<T, V extends View> extends RecyclerView.Adapter<ViewWrapper<V>> {
protected List<T> items;
@Override
public int getItemCount() {
return items.size();
}
@Override
public final ViewWrapper<V> onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewWrapper<>(onCreateItemView(parent, viewType));
}
protected abstract V onCreateItemView(ViewGroup parent, int viewType);
// additional methods to manipulate the items
}
ViewWrapper.java
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class ViewWrapper<V extends View> extends RecyclerView.ViewHolder {
private V view;
public ViewWrapper(V itemView) {
super(itemView);
view = itemView;
}
public V getView() {
return view;
}
}
从回收商视图中删除项目后,您需要通知项目范围更改。所以你需要做这样的事情
notifyItemRemoved(position);.
notifyItemRangeChanged(position, arrayList.size());