shreyansh
shreyansh

Reputation: 107

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.

Upvotes: 0

Views: 609

Answers (2)

Solaiman Hossain
Solaiman Hossain

Reputation: 485

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.

Upvotes: 1

Claff
Claff

Reputation: 36

If your RecyclerView items have "clickable" children in their XML file, try to declare the property:

android:clickable="false"

Leave the clickable true only on the father of your list item.

If this is not your case, or you absolutely need clickable elements in your list item, there is another solution.


If you are using a RecyclerView, you should have implementend a RecyclerView Adapter somewhere in your code.

In the OnBindViewHolder method, try the following code:

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

  //YOUR CODE ...

  holder.itemView.setOnTouchListener(yourOnTouchListener);

  //MORE CODE ...
}

This will set the OnTouchListener to the single ItemView at the moment the list is generated.

I hope my answer is helpful and understandable enough.

Upvotes: 0

Related Questions