我正在开发一个项目,其中我使用了开源代码,但有一次我被卡住了,在这段代码中我使用的是RecyclerView,其中onRecycleItemClickListener已经实现但是现在我需要在现有代码中实现onItemLongClickListener < / p>
public abstract class BaseRecyclerViewAdapter<T, VH extends BaseClickListenerViewHolder> extends RecyclerView.Adapter<VH> {
private List<T> objectsList;
protected final BaseActivity baseActivity;
protected final LayoutInflater layoutInflater;
protected final Resources resources;
protected QBUser currentQbUser;
// Package private because we need access in BaseViewHolder but not in child classes
OnRecycleItemClickListener<T> onRecycleItemClickListener;
AdapterView.OnItemLongClickListener onItemLongClickListener;
public BaseRecyclerViewAdapter(BaseActivity baseActivity) {
this.baseActivity = baseActivity;
this.layoutInflater = LayoutInflater.from(baseActivity);
resources = baseActivity.getResources();
objectsList = new ArrayList<>();
}
public BaseRecyclerViewAdapter(BaseActivity baseActivity, List<T> objectsList) {
this(baseActivity);
this.objectsList = objectsList;
currentQbUser = AppSession.getSession().getUser();
}
public void setList(List<T> items) {
objectsList = items;
notifyDataSetChanged();
}
public void addItem(T item) {
objectsList.add(item);
notifyItemInserted(objectsList.size() - 1);
}
public void addItem(int position, T item) {
objectsList.add(position, item);
notifyItemInserted(position);
}
public void addAll(Collection<T> collection) {
objectsList.addAll(collection);
notifyItemRangeChanged(objectsList.size() - collection.size(), collection.size());
}
public void removeItem(int position) {
objectsList.remove(position);
notifyItemRemoved(position);
}
public void removeItem(T item) {
int position = objectsList.indexOf(item);
if (position != -1) {
objectsList.remove(item);
notifyItemRemoved(position);
}
}
public void clear() {
objectsList.clear();
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return objectsList.size();
}
public T getItem(int position) {
return objectsList.get(position);
}
public List<T> getAllItems() {
return objectsList;
}
public boolean isEmpty() {
return objectsList.size() == 0;
}
public void setOnRecycleItemClickListener(OnRecycleItemClickListener<T> onRecycleItemClickListener) {
this.onRecycleItemClickListener = onRecycleItemClickListener;
}
protected void displayAvatarImage(String uri, ImageView imageView) {
ImageLoader.getInstance().displayImage(uri, imageView, ImageLoaderUtils.UIL_USER_AVATAR_DISPLAY_OPTIONS);
}
}
答案 0 :(得分:0)
您可以在代码中使用此课程ItemTouchListenerAdapter
轻松添加对项目点击和长按次数的支持。
用法:
recyclerView.addOnItemTouchListener(new ItemTouchListenerAdapter(recyclerView, this));
代码:
public class ItemTouchListenerAdapter extends GestureDetector.SimpleOnGestureListener implements RecyclerView.OnItemTouchListener {
public interface RecyclerViewOnItemClickListener {
void onItemClick(RecyclerView parent, View clickedView, int position);
void onItemLongClick(RecyclerView parent, View clickedView, int position);
}
private RecyclerViewOnItemClickListener listener;
private RecyclerView recyclerView;
private GestureDetector gestureDetector;
public ItemTouchListenerAdapter(RecyclerView recyclerView, RecyclerViewOnItemClickListener listener) {
if (recyclerView == null || listener == null) {
throw new IllegalArgumentException("RecyclerView and Listener arguments can not be null");
}
this.recyclerView = recyclerView;
this.listener = listener;
this.gestureDetector = new GestureDetector(recyclerView.getContext(), this);
}
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
return false;
}
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
@Override
public void onShowPress(MotionEvent e) {
View view = getChildViewUnder(e);
if (view != null) {
view.setPressed(true);
}
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
View view = getChildViewUnder(e);
if (view == null) return false;
view.setPressed(false);
int position = recyclerView.getChildPosition(view);
listener.onItemClick(recyclerView, view, position);
return true;
}
public void onLongPress(MotionEvent e) {
View view = getChildViewUnder(e);
if (view == null) return;
int position = recyclerView.getChildPosition(view);
listener.onItemLongClick(recyclerView, view, position);
view.setPressed(false);
}
@Nullable
private View getChildViewUnder(MotionEvent e) {
return recyclerView.findChildViewUnder(e.getX(), e.getY());
}
}