Shures
Shures

Reputation: 25

how to make two fragments communicate each other inside viewpager in android

Suppose, I have two fragments , FragmentA and FragmentB inside viewpager .When i click the button in fragmentA then it should be able to add the textview in another fragmentB.so, how is it possible ....please help me out.

class Myadpter extends FragmentPagerAdapter {
    Fragment fragment =null;
    public Myadpter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if(position==0){
            fragment = new Post();
        }
        if(position==1){
            fragment = new ActiveChat();
        }
        if(position==2){
            fragment = new LastUsers();
        }
        if(position==3){
            fragment = new Noname();
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return 4;
    }
}

Upvotes: 0

Views: 187

Answers (3)

Gaetano Dati
Gaetano Dati

Reputation: 97

Do as follows:

Fragment A

public class FragmentA extends Fragment implements View.OnClickListener {

OnButtonPressed mCallback;
Button yourButton;
TextView textViewFragA;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    return inflater.inflate(R.layout.your_layout, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    yourButton = findViewById(R.id.yourBtn);
    textViewFragA = findViewById(R.id.textViewFragA);

    yourButton.setOnClickListener(this);

}

@Override
public void onClick(View view) {

switch(view.getId()){
        case R.id.yourBtn:
            mCallback.onButtonPressed(textViewFragA);
            break;
}

 @Override
public void onAttach(Context context) {
    super.onAttach(context);
    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnButtonPressed) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().toString()
                + " must implement OnButtonPressed");
    }
}

@Override
public void onDetach() {
    mCallback = null; // Avoid memory leaking
    super.onDetach();
}

/**
 * Interface called whenever the user has clicked on the Button
 * @param textView The TextView to add in FragmentB
 */
public interface OnButtonPressed{
    void onButtonPressed(TextView textView);
}
}

FragmentB

public class FragmentB extends Fragment{

TextView textViewFragB;

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    return inflater.inflate(R.layout.your_layout, container, false);
}

 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    textViewFragB= findViewById(R.id.textViewFragB);

}

public TextView getTextViewFragB(){

return textViewFragB;
}

Activity

public class TabControllerActivity extends AppCompatActivity implements FragmentA.OnButtonPressed{

MyAdapter adapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity_layout);

// Your Stuff

}

// Everytime the user clicks on the Button in FragmentA, this interface method gets triggered
@Override
public void onButtonPressed(TextView textViewFragA) {

FragmentB fragmentB = (FragmentB) adapter.getItem(1)/* Be careful here and get the right fragment, 

otherwise the App will crash*/

// Since you got the TextView and not only the text inside of it,
// you can do whatever you want. Here for example we set the text like the textViewFragA. 
//In a few words you turn the textViewFragB to the other one
fragmentB.getTextViewFragB().setText(textViewFragA.getText().toString());

}

}

Hope it will help

Upvotes: 0

Richard Ansell
Richard Ansell

Reputation: 926

As already stated by the other user, implementing an interface is the way to go. This link Communicating with Other Fragments will explain in more detail how to achieve what you are attempting to do. Hope this solves your problem.

Upvotes: 0

VK.N
VK.N

Reputation: 193

Implement a interface to communicate between two fragments, the class where the view pager is will be a middle man

Upvotes: 1

Related Questions