我想在我的Recycler View上实现OnItemClickListener和OnItemLongClickListener。我使用Android库BRVAH来设置适配器。
我期待OnItemLongClickListener的自定义声明,以便我可以把它放在我的(已经实现的)OnItemClickListener上。
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
public void onItemClick(BaseQuicAdapter adapter, final View view, int position) {
.....
}
});
答案 0 :(得分:1)
你必须创建两个接口来处理click事件和长按事件到适配器中以进行recyclview。
像下面的接口一样进入适配器类..
onItemClickListner onItemClickListner;
onItemLongClickListner onItemLongClickListner;
public void setOnItemLongClickListner(RecyclerViewAdpater.onItemLongClickListner onItemLongClickListner) {
this.onItemLongClickListner = onItemLongClickListner;
}
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(String str);//pass your object types.
}
public interface onItemLongClickListner{
void onLongClick(String str);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
String data = mStringList.get(position); // if you pass object of class then create that class object.
holder.textView.setText(data);
// below code handle click event on recycler view item.
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick(data);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
onItemLongClickListner.onLongClick("Hiii");
return false;
}
});
}
然后在活动结束后,在适配器定义并绑定到Recycler视图后定义recycleler视图,然后获取如下所示的click事件..
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
@Override
public void onClick(String str) {
Toast.makeText(getApplicationContext(),"Value of Click"+str,Toast.LENGTH_SHORT).show();
}
});
recyclerViewAdpater.setOnItemLongClickListner(new RecyclerViewAdpater.onItemLongClickListner() {
@Override
public void onLongClick(String str) {
Toast.makeText(getApplicationContext(),"Value of Long Click"+str,Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
您可以使用库的以下方法。
/**
* Register a callback to be invoked when an item in this RecyclerView has
* been long clicked and held
*
* @param listener The callback that will run
*/
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
}
所以要在你的代码中实现它
mAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
.....
}
答案 2 :(得分:0)
进入适配器并找到ViewHolder类并编写以下代码
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//call onClick handler here
}
});
itemView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
//call onLongClick handler here //Here you will return true
return true;
}
});
}
}
然后创建两个界面,如
public interface OnMyLongClickListener{
void onLongClick(int pos);
}
public interface OnMyClickClickListener{
void onClick(int pos);
}
在您的activity或fragment类中实现这两个接口,然后在接收事件中调用这些接口方法