作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 ItemClickSupport
类,我想使用他们的长按监听器,但我如何知道它是否被长按?遗憾的是我没有找到任何使用它的示例。
ItemClickSupport.addTo(recyclerView3).setOnItemLongClickListener { recyclerView, position, v -> true }
如何检查它是否为真?我如何获得它被点击的位置?
ItemClickSupport.java:gist
/**
* A class that adds item click support to a {@link RecyclerView}.
*
* @author Hugo Visser
* @see <a href="http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/">
* Getting your clicks on RecyclerView</a>
*/
public class ItemClickSupport {
private final RecyclerView recyclerView;
private OnItemClickListener itemClickListener;
private OnItemLongClickListener itemLongClickListener;
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null) {
final int position = recyclerView.getChildAdapterPosition(v);
final long id = recyclerView.getChildItemId(v);
itemClickListener.onItemClick(recyclerView, v, position, id);
}
}
};
private View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (itemLongClickListener != null) {
final int position = recyclerView.getChildAdapterPosition(v);
final long id = recyclerView.getChildItemId(v);
return itemLongClickListener.onItemLongClick(recyclerView, v, position, id);
}
return false;
}
};
private RecyclerView.OnChildAttachStateChangeListener attachListener
= new RecyclerView.OnChildAttachStateChangeListener() {
@Override
public void onChildViewAttachedToWindow(View view) {
if (itemClickListener != null) {
view.setOnClickListener(clickListener);
}
if (itemLongClickListener != null) {
view.setOnLongClickListener(longClickListener);
}
}
@Override
public void onChildViewDetachedFromWindow(View view) {
if (itemClickListener != null) {
view.setOnClickListener(null);
}
if (itemLongClickListener != null) {
view.setOnLongClickListener(null);
}
}
};
private ItemClickSupport(RecyclerView recyclerView) {
this.recyclerView = recyclerView;
}
public static ItemClickSupport addTo(@NonNull RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support == null) {
support = new ItemClickSupport(view);
support.attach(view);
} else {
Log.w("RecyclerView already has ItemClickSupport.");
}
return support;
}
private void attach(RecyclerView view) {
view.setTag(R.id.item_click_support, this);
view.addOnChildAttachStateChangeListener(attachListener);
}
public static ItemClickSupport removeFrom(@NonNull RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support != null) {
support.detach(view);
} else {
Log.w("RecyclerView does not have ItemClickSupport.");
}
return support;
}
private void detach(RecyclerView view) {
view.removeOnChildAttachStateChangeListener(attachListener);
view.setTag(R.id.item_click_support, null);
itemClickListener = null;
itemLongClickListener = null;
}
/**
* @return The callback to be invoked when an item in the {@link RecyclerView}
* has been clicked, or {@code null} if no callback has been set.
*/
public OnItemClickListener getOnItemClickListener() {
return itemClickListener;
}
/**
* Register a callback to be invoked when an item in the
* {@link RecyclerView} has been clicked.
*
* @param listener callback that will be invoked
*/
public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
itemClickListener = listener;
return this;
}
/**
* @return The callback to be invoked when an item in the {@link RecyclerView}
* has been clicked and held, or {@code null} if no callback has been set.
*/
public OnItemLongClickListener getOnItemLongClickListener() {
return itemLongClickListener;
}
/**
* Register a callback to be invoked when an item in the
* {@link RecyclerView} has been clicked and held.
*
* @param listener callback that will be invoked
*/
public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
itemLongClickListener = listener;
return this;
}
/**
* Interface definition for a callback to be invoked when an item in the
* {@link RecyclerView} has been clicked.
*/
public interface OnItemClickListener {
/**
* Callback method to be invoked when an item in the {@code RecyclerView}
* has been clicked.
*
* @param parent {@code RecyclerView} where the click happened
* @param view view within the {@code RecyclerView} that was clicked
* @param position position of the view in the adapter
* @param id row ID of the item that was clicked
*/
void onItemClick(RecyclerView parent, View view, int position, long id);
}
/**
* Interface definition for a callback to be invoked when an item in the
* {@link RecyclerView} has been clicked and held.
*/
public interface OnItemLongClickListener {
/**
* Callback method to be invoked when an item in the {@code RecyclerView}
* has been clicked and held.
*
* @param parent {@code RecyclerView} where the click happened
* @param view view within the {@code RecyclerView} that was clicked
* @param position position of the view in the adapter
* @param id row ID of the item that was clicked
* @return {@code true} if the callback consumed the long click; {@code false} otherwise.
*/
boolean onItemLongClick(RecyclerView parent, View view, int position, long id);
}
}
最佳答案
在 {recyclerView, position, v -> true}
中,recyclerView, position, v
是您的 onItemLongClick
回调的参数。你会像这样使用它:
ItemClickSupport.addTo(recyclerView3).setOnItemLongClickListener { recyclerView, view, position, id ->
// Use position here
true // False if not consumed
}
编辑:
如果 OnItemLongClickListener
是 Kotlin 接口(interface),您可以像这样使用它:
ItemClickSupport.addTo(recyclerView3).setOnItemLongClickListener(object: ItemClickSupport.OnItemLongClickListener {
override fun onItemLongClick(parent: RecyclerView, view: View, position: Int, id: Long): Boolean {
TODO("not implemented")
}
})
关于android - 如何在我的 Android Activity 中使用 onItemLongClickListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47420622/
我是一名优秀的程序员,十分优秀!