user3422177 user3422177 -4 years ago 134
Android Question

Recycler View in Android not Clicking

I am working on an Android app. I am facing some problem with opening a popup menu when I click on an Item in the Recycler View.

Show.java (the Activity containing the Recycler View). mRecycler is the object associated with the RecyclerView.

mRecycler.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRecycler, new RecyclerViewClickListener() {
@Override
public void onClick(View view, final int position) {
PopupMenu menu = new PopupMenu(Show.this,mRecycler);
MenuItem itemView = (MenuItem) findViewById(R.id.three);
if(method.equals("Completed"))
itemView.setTitle("Mark as imcomplete");
menu.getMenuInflater().inflate(R.menu.menu_popup,menu.getMenu());

menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
public boolean onMenuItemClick(MenuItem item){
int id=item.getItemId();
switch (id){
case R.id.one:
Intent intent = new Intent(Show.this,Add.class);
intent.putExtra("Task",tasks.get(position));
startActivity(intent);
finish();
startActivity(getIntent());
break;
case R.id.two:
deleteRecord(position);
break;
case R.id.three:
toggleComplete(position);
}
return true;
}
});

}

@Override
public void onLongClick(View view, int position) {
onClick(view,position);
}
}));


Code snippet for xml of RecyclerView

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:scrollbars="vertical"
android:clickable="true"
android:contextClickable="true"
android:longClickable="true" />


RecyclerViewClickListener.java

public interface RecyclerViewClickListener {
void onClick(View view,int position);
void onLongClick(View view,int position);
}


RecyclerTouchListener.java - class that implements the OnItemTouchListener

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
private GestureDetector gestureDetector;
private RecyclerViewClickListener clickListener;

public RecyclerTouchListener (Context context,final RecyclerView recyclerView,
final RecyclerViewClickListener clickListener){
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e){
return true;
}

@Override
public void onLongPress(MotionEvent e){
View child = recyclerView.findChildViewUnder(e.getX(),e.getY());
if (child != null && clickListener!=null)
clickListener.onLongClick(child,recyclerView.getChildAdapterPosition(child));
}

});
}

@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.getChildAdapterPosition(child));
return false;
}

@Override
public void onTouchEvent(RecyclerView rv,MotionEvent e){}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntecept){}
}


Nothing happens when I click an item on the RecyclerView. The android system does not even acknowledge that it was clicked. Please check what is the problem with my code.

Answer Source

Would You please try this

mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRecyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                menuCardIndex = position;
                PopupMenu popupMenu = new PopupMenu(Show.this, view);
                popupMenu.setOnMenuItemClickListener(Show.this);
                popupMenu.inflate(R.menu.menu_popup);
                popupMenu.show();
            }

            @Override
            public void onLongClick(View view, int position) {
            }
        }));

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.one:
            Your_First_Operation;
            return true;
        case R.id.two:
            Your_Second_Operation;
            return true
        case R.id.three:
            Your_Third_Operation;
            return true
    }
    return false;
}

finally put this one your activity

public class Show extends AppCompactActivity implements PopupMenu.OnMenuItemClickListener 

hopefully by this way, You can listen your click listener. if it's working properly, You can set your logic.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download