我正在尝试在longClickListener
中使用Fragment
但由于某种原因它只是无效。如果我进行长时间点击,则onItemClickListener
这是我的代码
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<Info> Infos;
private ClickListener clickListener;
private MyLongClickListener myLongClickListener;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
// each data item is just a string in this case
public ImageView image;
public/* Roboto */ TextView name;
public/* Roboto */ TextView companyName;
public/* Roboto */ TextView city;
public/* Roboto */ TextView time;
public/* Material */ TextView icon;
public ViewHolder(View convertView) {
super(convertView);
companyName = (TextView) convertView
.findViewById(R.id.t1);
city = (TextView) convertView
.findViewById(R.id.t2);
convertView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
clickListener.onItemClick(getPosition(), v);
}
@Override
public boolean onLongClick(View v) {
myLongClickListener.onItemLongClick(getPosition(), v);
return true;
}
}
public void add(int position, Info item) {
Infos.add(position, item);
notifyItemInserted(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public RecyclerViewAdapter(ArrayList<Info> dummyModelList) {
Infos = dummyModelList;
}
// Create new views (invoked by the layout manager)
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.genre_list_item, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Info currInfo = Infos.get(position);
holder.city.setText(currInfo.getTitle());
holder.companyName.setText(currInfo.getArtist());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return Infos.size();
}
public void setOnItemClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public void setOnItemLongClickListener(MyLongClickListener clickListener) {
this.myLongClickListener = clickListener;
}
public interface ClickListener {
void onItemClick(int position, View v);
}
public interface MyLongClickListener {
boolean onItemLongClick(int position, View v);
}
}
这就是我在Fragment
中使用它的方法。此onLongClick
和onItemClickListener
都放在onResume
的{{1}}函数中。
Fragment
我无法在代码中看到任何错误,但不知道它为什么不起作用。触摸事件始终由recyclerViewAdapter.setOnItemLongClickListener(new
RecyclerViewAdapter.MyLongClickListener() {
@Override
public boolean onItemLongClick(int position, View v) {
// Task
return true;
}
});
接收。任何帮助将不胜感激,谢谢!
答案 0 :(得分:5)
在构造函数中设置click和onclicklisteners。
convertView.setOnClickListener(this);
convertView.setOnLongClickListener(this);
如果没有setOnLongClickListener
,您就无法听到长时间点击。