4

I tried as below at onClick() method of recyelerview.viewholder class.

SampleDialogFragment used in the sample extends DialogFragment.

@Override
public void onClick(View v)
{
SampleDialogFragment df= new SampleDialogFragment();
df.show(v.getContext().getSupportFragmentManager(), "Dialog");
}

I'm facing problem at v.getContext().getSupportFragmentManager(). I can't call getSupportFragmentManager().

I also tried as below .

@Override
public void onClick(View v)
{
SampleDialogFragment df= new SampleDialogFragment();
SampleActivity activity = new SampleActivity();
df.show(activity.getSupportFragmentManager(), "Dialog");
}

SampleActivity is the activity the recycler view is attached . It shows no error. When I run the app and crash.

The log shows that the activity has destoryed.

Any solution ?

Lin Lin
  • 259
  • 2
  • 14
  • Pass in the `Context` through the adapter's constructor and use it to get FragmentManger instance. Or use call back method to listen for the click event in the SampleActivity. Don't create `new SampleActivity()` – Emil Nov 24 '15 at 09:13
  • `getActivity().getSupportFragmentManager()` if you have fragment – Kishore Jethava Nov 24 '15 at 09:18
  • @Boss I tried . I can't call getSupportFragmentManager with context in the recyclerview.viewholder. – Lin Lin Nov 24 '15 at 09:20
  • @kishorejethava I tried , not working – Lin Lin Nov 24 '15 at 09:22
  • I can make toast using the context i passed into this class . But , getsupportFragmentManager is not working – Lin Lin Nov 24 '15 at 09:23
  • `getSupportFragmentManager()` only available in `FragmentActivity` – Emil Nov 24 '15 at 09:23
  • @Boss Can you support me any example – Lin Lin Nov 24 '15 at 09:28
  • Possible duplicate of [Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?](http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif) – Vladimir Markeev Nov 24 '15 at 09:48

2 Answers2

6

The proper way is to use an interface.

public interface OnItemClickListener {
    void onItemClicked(View v);
}

And call the interface method when the onClick method is fired.

public class YourListAdapter extends RecyclerView.Adapter<...>

//your code
private OnItemClickListener listener;

public YourListAdapter(OnItemClickListener listener /*your additional parameters*/) {
    this.listener = listener;
    //...
}

@Override
public void onClick(View v){    
    listener.onItemClicked(View v);
}
}

You have to pass the OnItemClickListener Interface instance from SampleActivity

And have it implement it in your SampleActivity

public class SampleActivity extends FragmentActivity implements OnItemClickListener {

    @Override
    public void onItemClicked(View v) {
        SampleDialogFragment df= new SampleDialogFragment();
        df.show(getSupportFragmentManager(), "Dialog");
    }
}
Emil
  • 2,786
  • 20
  • 24
  • @Boss can you please explain more, or link to an example? where is the interface OnItemClickListener suppose to be? how does all this code come together? – Yuval Levy Nov 24 '15 at 13:41
  • @YuvalLevy You can create a new file for interface or put it in the adapter class. What do you meant by _all this code come together_ ? They are separate files. – Emil Nov 24 '15 at 13:49
  • Is it right that in the end, when clicked in RecyclerView you want that the method that will be invoke is the method inside the fragment class? @Boss – Yuval Levy Nov 24 '15 at 13:57
  • so if that is more comfortable to me I can make it without the interface, or there is some reason not to do that? – Yuval Levy Nov 24 '15 at 14:00
  • @YuvalLevy OP wants to get the instance of `SupportFragmentManager` in the adapter to show his `DialogFragment`. There are two ways. He could pass the `SupportFragmentManager` instance from his `FragmentActivity` to the adapter and call the `DialogFragment` from the adapter or He could use an interface and handle the click event in the `Activity/Fragment` itself. Using the interface is the proper way, I think. – Emil Nov 24 '15 at 14:03
1

the easiest way i use it

public class AdapterProduct extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

FragmentManager FragManager ;
    // in constructor
    public AdapterProduct(Context context, RecyclerView view ,
 ArrayList<Product> items , FragmentManager getSupportFragmentManager) {
        this.items = items;
        ctx = context;
        FragManager = getSupportFragmentManager;
        lastItemViewDetector(view);
    }
}

in onClick

@Override
public void onClick(View v)
{
SampleDialogFragment df= new SampleDialogFragment();
SampleActivity activity = new SampleActivity();
df.show(FragManager , "Dialog");
}

in your MainActivity or where u set your recycleview

AdapterProduct mAdapter = new AdapterProduct(MainActivity.this, rv_Daily_Deals,
BeanProduct,getSupportFragmentManager());
Hassan Badawi
  • 302
  • 1
  • 10