Samuel Georgeszusz
Samuel Georgeszusz

Reputation: 131

How to use a FragmentDialog in a class that extends RecyclerView.ViewHolder?

I am trying to display a Fragment Dialog when a user clicks on an item on my recycler view.

The bit I am stuck on is how to use a FragmentDialog in a class that extends RecyclerView.ViewHolder?

This is what I have so far:

  public class FinalHolder extends RecyclerView.ViewHolder{
        public FinalHolder(View view){
            super(view);
            TextView username= (TextView)view.findViewById(R.id.username);
            TextView address = (TextView)view.findViewById(R.id.address);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewFragmentDialog viewFragmentDialog = new ViewFragmentDialog();
                    viewFragmentDialog.show() // THIS IS WHERE I AM HAVING TROUBLE. THE `show` METHOD EXPECTS A FRAGMENTACTIVITY
                }
            });
        }
    }

The viewFragmentDialog.show() method is where I am having some difficulty - because it is expecting Fragment Activity but I am using RecyclerView.ViewHolder

Upvotes: 0

Views: 193

Answers (1)

Blackbelt
Blackbelt

Reputation: 157447

I won't use a DialogFragment in this case, if you want explicitly call it from RecyclerView.ViewHolder. The main reason is that you need access to the FragmentManager to show() your DialogFragment's subclass, and you don't. I see two possible solution. The easiest one is to use a normal AlertDialog. You need a context to access it that you can easily retrieve with itemView.getContext(), or use a Delegate (aka Listener) to communicate with the Activity/Fragment, and show the DialogFragment from there

Upvotes: 1

Related Questions