How to access the button of fragment A from fragment B

I have two Fragmentin mine Activity: a fragment Ausing a button Xand a fragment Busing a button Y.

How can I change the background image of a button Xwhen I press a button B? Is it possible?

+3
source share
3 answers

From the documentation

Since each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in several actions, so you should design for reuse and avoid directly manipulating one fragment from another fragment.

, , . - . , . Fragment s - , .

... . , , , , .


# 1:

, A, , B. , :

:

public interface OnButtonClickedListener {
    public void onButtonClicked(); 
}

:

public class SampleActivity extends Activity implements OnButtonClickedListener {

    /* Implementation goes here */     

    public void onButtonClicked() {
        // This method is called from fragment A, and when it is called,
        //   it will send information to fragment B. Remember to first
        //   check to see if fragment B is non-null.

        /* Make call to a method in fragment B that will update its display */
    }
} 

A:

public class FragmentA extends Fragment {
    OnButtonClickedListener mListener;

    /* Implementation goes here */

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnButtonClickedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnButtonClickedListener ");
        }
    }

    public void clickButton() {
        // When the button is clicked, notify the activity.
        //   The activity will then pass the information to fragment
        //   B (if it has been created).

        mListener.onButtonClicked();
    }
}

# 2:

: " - - ? , A, B?"

, , - , . , . , , . , , , . , , B . , B, , , B .

, ... : P. ... , , - .

+10

:

:

public class SampleActivity extends Activity{

    public interface OnButtonClickedListener {
           public void onButtonClicked(); 
    }

    private OnButtonClickedListener onButtonClickedListener = null;

    public OnButtonClickedListener getOnButtonClickedListener () {
           return onButtonClickedListener 
    }

    public void setOnButtonClickedListener (
        OnButtonClickedListener onButtonClickedListener {
           this.onButtonClickedListener  = onButtonClickedListener;
    }

} 

A:

public class FragmentA extends Fragment {

private OnButtonClickedListener onButtonClickedListener = null;

private OnClickListener actionBarClickListener = new OnClickListener() {

    @Override
    public void onClick(View view) {
        if (onButtonClickedListener == null){
            onButtonClickedListener = ((SampleActivity) getActivity()).onButtonClickedListener ();
        }
        if (onButtonClickedListener != null) {
            onButtonClickedListener
                    .onButtonClicked();
        }
    }
};

}

B:

    public class FragmentB extends Fragment {

private OnButtonClickedListener onButtonClickedListener = new OnButtonClickedListener() {

    @Override
    public void onButtonClicked() {

        Toast.makeText(getActivity(), "Button clicked", Toast.LENGTH_SHORT).show();
    }
};

@Override
public void onResume() {
    super.onResume();
    SampleActivity sampleActivity = (SampleActivity) getActivity();
    sampleActivity.setSearchBoxTextChangedListener(onButtonClickedListener);
}

}

-.

+1

onClick , , Activity.

Activity B.

0

All Articles