0

I have set Recyclerview in Fragment one.I want to send position of RecyclerView item from RecyclerView Adapter of Fragment one to RecyclerView Adapter of Fragment Two to set particular item of RecyclerView in Fragment two.
This is code for Fragment one Adapter

@Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
           final CurrentStatusEntry current=filterList.get(position);
          // holder.number.setText(current.getNo());
           holder.Name.setText(current.getName());
           holder.People.setText(current.getPeople());
           holder.Time.setText(current.getTime());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int position=holder.getAdapterPosition();

                    AppCompatActivity activity=(AppCompatActivity)v.getContext();
                    activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame,satusInfo).addToBackStack("info").commit();
                }
            });  

How to pass this position ?

Sam
  • 39
  • 7
  • 2
    U can pass data fragment with create Bundle and setArguments. Bundle args = new Bundle(); args.putInt("position", position); statusInfo.setArguments(args); – user3307005 Mar 05 '18 at 07:11
  • https://stackoverflow.com/questions/44960380/send-data-from-activity-to-fragment-already-created – denvercoder9 Mar 05 '18 at 07:12
  • But how to receive that data in AdapterClass of Second Fragment. – Sam Mar 05 '18 at 07:12
  • you mave have to look at this post. [pass data between fragments](https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – jake oliver Mar 05 '18 at 07:13
  • With this I am able to get the Value in Another Fragment.But the adapter position is set from Adapter Class so how to access the value in adapter class – Sam Mar 05 '18 at 07:15
  • Possible duplicate of [How to pass values between Fragments](https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) – Reaz Murshed Mar 05 '18 at 07:16

2 Answers2

0

You can use Bundle for this try like below example :

In Fragment one setArguments :

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

In second Fragment getArguments :

Bundle bundle = this.getArguments();
if (bundle != null) {
    int myInt = bundle.getInt(key, defaultValue);
}

Refer Android Doc.

Vishal G. Gohel
  • 1,008
  • 1
  • 16
  • 31
0

In fragment one post fragment instance to fragment two

getFragmentManager().beginTransaction()
                                .replace(R.id.ftHolder, fragmentTwo.newInstance(getAdapterPosition()))
                                .addToBackStack("info")
                                .commit();

in fragment two add this function

public static fragmentTwo newInstance(int position) {
    fragmentTwo fragment = new fragmentTwo();
    Bundle bdl = new Bundle();
    bdl.putInt("position", position);
    fragment.setArguments(bdl);
    return fragment;
}

And in onCreate() or onActivityCreated() get it and use it anywhere

position = getArguments().getInt("position");
Radesh
  • 13,084
  • 4
  • 51
  • 64