recyclerView.addOnItemTouchListener(new MyListClickListener(getContext(), new MyListClickListener.ClickListener() { @Override public void onClick(View v, int position) {
TextView textView = (TextView) v.findViewById(R.id.title);
Fragment fragment = ((MyPagerAdapter) viewPager.getAdapter()).getFragment(1);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(fragment);
fragment.onDestroy();
ft.attach(fragment);
ft.commit();
comm.sendData(String.valueOf(textView.getText()));
viewPager.setCurrentItem(1);
}
}));
公共类 MyListClickListener 实现 RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
ClickListener clickListener;
public MyListClickListener(Context context, ClickListener clickListener){
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(),e.getY());
if(child!= null && clickListener!= null && gestureDetector.onTouchEvent(e)){
clickListener.onClick(child,rv.getChildPosition(child));
}
return true;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
public interface ClickListener {
void onClick(View v, int position);
}
}