RvItemAdapter.java
public class RvItemAdapter extends RecyclerView.Adapter<RvItemAdapter.Viewholder> implements View.OnClickListener{
private List<Integer> mdata;
Context context;
private OnItemClickListener mOnItemClickListener;
public RvItemAdapter(Context context, List<Integer> mdata) {
this.context=context;
this.mdata = mdata;
}
@Override
public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_rv, parent, false);
view.setOnClickListener(this);
Viewholder viewholder = new Viewholder(view);
return viewholder;
}
@Override
public void onBindViewHolder(Viewholder holder, int position) {
holder.itemView.setTag(position);
holder.textView.setText(String.valueOf(mdata.get(position)));
}
@Override
public int getItemCount() {
return mdata.size();
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, (int) v.getTag());
}
}
class Viewholder extends RecyclerView.ViewHolder {
TextView textView;
public Viewholder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.list_item);
}
}
public void setClickListener(OnItemClickListener clickListener) {
this.mOnItemClickListener = clickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}}
这是我的MainActivity
RecyclerView recyclerView;
RvItemAdapter mRvItemAdapter;
private ArrayList<Integer> mDdat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.main_rv);
mDdat = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mDdat.add(i);
}
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
mRvItemAdapter = new RvItemAdapter(this, mDdat);
recyclerView.setAdapter(mRvItemAdapter);
recyclerView.addOnItemTouchListener(new ItemClickListener(recyclerView, new ItemClickListener.onItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "ssssssssssssss", Toast.LENGTH_SHORT).show();
}
}));
recyclerView.addOnItemTouchListener(new ItemClickListener(recyclerView, new ItemClickListener.onItemLongClickListener() {
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(MainActivity.this, "ssssssssssssss", Toast.LENGTH_SHORT).show();
}
}));
mRvItemAdapter.setClickListener(new RvItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.e("sssss","sssssssss");
Toast.makeText(MainActivity.this, "什么鬼", Toast.LENGTH_SHORT).show();
}
});
ItemClickListener.java
public class ItemClickListener extends RecyclerView.SimpleOnItemTouchListener {
private onItemClickListener clickListener;
private onItemLongClickListener clickLongListener;
private GestureDetectorCompat gestureDetector;
public ItemClickListener(final RecyclerView recyclerView, onItemClickListener listener) {
this.clickListener = listener;
gestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onItemClick(childView, recyclerView.getChildAdapterPosition(childView));
}
return true;
}
});
}
public ItemClickListener(final RecyclerView recyclerView, onItemLongClickListener listener) {
this.clickLongListener = listener;
gestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && clickListener != null) {
clickLongListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
super.onTouchEvent(rv, e);
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
super.onRequestDisallowInterceptTouchEvent(disallowIntercept);
}
/**
* 监听接口
*/
public interface onItemClickListener {
void onItemClick(View view, int position);
}
public interface onItemLongClickListener {
void onItemLongClick(View view, int position);
}
}
我想知道为什么?