Recyclerview On Click Listener Not Working
When i want to click recycler view then it should open another Activity Data is fetching but the problem is when i click on recycleview it displays nothing. I tried RecyclerView.O
Solution 1:
Using Interface you can solve your problem:
publicinterfaceOnItemClickListener {
publicvoidonItemClick(View view, int position, List<AllRestaurant> AllRestaurant);
}
Implement interface in your adapter class like this.
publicclassMyViewHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener {
//public TextView long1, medium, small;
TextView t1,t2,t3,t4,t5;
ImageView logo;
publicMyViewHolder(View view) {
super(view);
t1=(TextView)view.findViewById(R.id.t1);
t2=(TextView)view.findViewById(R.id.t2);
t3=(TextView)view.findViewById(R.id.t3);
t4=(TextView)view.findViewById(R.id.t4);
t5=(TextView)view.findViewById(R.id.t5);
logo=(ImageView) view.findViewById(R.id.image);
// final AllRestaurant items = getItem(position);//long1.setText(items.getRestaurent_id());// final NotifyUsers notifyUsers=new NotifyUsers(context);// username.setText(items.getResponse_by());// little.setText(items.g);
}
@OverridepublicvoidonClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition(), getData());
}
}
}
Then call like that:
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(newOnItemClickListener() {
@OverridepublicvoidonItemClick(View view, int position, List<UserDetails> userDetailsList) {
Toast.makeText(getActivity(), "index --> " + position, Toast.LENGTH_SHORT).show();
}
});
Solution 2:
if you want on item click for recycle view First you need to create the following class :
publicclassRecyclerItemClickListenerimplementsRecyclerView.OnItemTouchListener {
protected OnItemClickListener listener;
private GestureDetector gestureDetector;
@Nullableprivate View childView;
privateint childViewPosition;
publicRecyclerItemClickListener(Context context, OnItemClickListener listener) {
this.gestureDetector = newGestureDetector(context, newGestureListener());
this.listener = listener;
}
@OverridepublicbooleanonInterceptTouchEvent(RecyclerView view, MotionEvent event) {
childView = view.findChildViewUnder(event.getX(), event.getY());
childViewPosition = view.getChildPosition(childView);
return childView != null && gestureDetector.onTouchEvent(event);
}
@OverridepublicvoidonTouchEvent(RecyclerView view, MotionEvent event) {
// Not needed.
}
@OverridepublicvoidonRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
/**
* A click listener for items.
*/publicinterfaceOnItemClickListener {
/**
* Called when an item is clicked.
*
* @param childView View of the item that was clicked.
* @param position Position of the item that was clicked.
*/voidonItemClick(View childView, int position);
/**
* Called when an item is long pressed.
*
* @param childView View of the item that was long pressed.
* @param position Position of the item that was long pressed.
*/voidonItemLongPress(View childView, int position);
}
/**
* A simple click listener whose methods can be overridden one by one.
*/publicstaticabstractclassSimpleOnItemClickListenerimplementsOnItemClickListener {
/**
* Called when an item is clicked. The default implementation is a no-op.
*
* @param childView View of the item that was clicked.
* @param position Position of the item that was clicked.
*/publicvoidonItemClick(View childView, int position) {
// Do nothing.
}
/**
* Called when an item is long pressed. The default implementation is a no-op.
*
* @param childView View of the item that was long pressed.
* @param position Position of the item that was long pressed.
*/publicvoidonItemLongPress(View childView, int position) {
// Do nothing.
}
}
protectedclassGestureListenerextendsGestureDetector.SimpleOnGestureListener {
@OverridepublicbooleanonSingleTapUp(MotionEvent event) {
if (childView != null) {
listener.onItemClick(childView, childViewPosition);
}
returntrue;
}
@OverridepublicvoidonLongPress(MotionEvent event) {
if (childView != null) {
listener.onItemLongPress(childView, childViewPosition);
}
}
@OverridepublicbooleanonDown(MotionEvent event) {
// Best practice to always return true here.// http://developer.android.com/training/gestures/detector.html#detectreturntrue;
}
}
}
The in you fragment class:
publicclassRestaurantFragmentextendsFragmentimplementsRecyclerItemClickListener.OnItemClickListener{
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Viewv= inflater.inflate(R.layout.layout, container, false);
list.addOnItemTouchListener(newRecyclerItemClickListener(getActivity().getApplicationContext(), this));
.....
}
@OverridepublicvoidonItemClick(View childView, int position) {
// add the code for the click here
}
@OverridepublicvoidonItemLongPress(View childView, finalint position) {
}
}
Let me know if you need any help
Solution 3:
Here is the solution i got with the help of recyclerview.addonItemTouchListener and creating Interface.
publicclassRestaurantFragmentextendsFragment {
RecyclerView recyclerView;
LinkedList<AllRestaurant> allRestaurants;
RestaurantAdapter allRestaurantAdapter;
MethodModel methodModel;
@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.restaurantlist, container, false);
methodModel=newMethodModel(getActivity());
allRestaurants=methodModel.getAllRestaurant();
allRestaurantAdapter=newRestaurantAdapter(allRestaurants);
recyclerView=(RecyclerView)view.findViewById(R.id.recycler_view);
RecyclerView.LayoutManagermLayoutManager=newLinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(newDefaultItemAnimator());
recyclerView.addItemDecoration(newDividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
// set the adapter
recyclerView.setAdapter(allRestaurantAdapter);
recyclerView.addOnItemTouchListener(newRecyclerTouchListener(getActivity(), recyclerView, newClickListener() {
@OverridepublicvoidonClick(View view, int position) {
}
@OverridepublicvoidonLongClick(View view, int position) {
}
}));
return view;
}
classRecyclerTouchListenerimplementsRecyclerView.OnItemTouchListener{
publicRecyclerTouchListener(Context context, RecyclerView recyclerView, ClickListener clicklistener){
}
@OverridepublicbooleanonInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
Log.d("Performing Better", "onInterceptTouchEvent: "+e);
startActivity(newIntent(getActivity(), Details.class));
returnfalse;
}
@OverridepublicvoidonTouchEvent(RecyclerView rv, MotionEvent e) {
Log.d("On Touch Event Better", "onTouchEvent: "+e);
}
@OverridepublicvoidonRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
publicstaticinterfaceClickListener {
publicvoidonClick(View view, int position);
publicvoidonLongClick(View view, int position);
}
}
Post a Comment for "Recyclerview On Click Listener Not Working"