Android force Fragment to rebuild View

后端 未结 2 1149
野性不改
野性不改 2021-01-04 04:12

I have a simple app that has two fragments and when in landscape mode, both fragments are shown side by side and in portrait I show Fragment A and then if they select an opt

相关标签:
2条回答
  • 2021-01-04 04:48

    You will want to perform a FragmentTransaction and use the replace() method

    This shouldn't be too hard to do, but the answer will depend on where your Menu is located (i.e. is your onOptionsItemSelected() call back inside your parent Activity or is it in either Fragment A/B?).

    Suppose for simplicity's sake, your menu implementation and onOptionsItemSelected() is in the parent activity, and you want to reshape the fragments in the event that menu_option1 is chosen. It would look something like this:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    //...
    switch (item.getItemId()) {
    case R.id.menu_option1:
        //do something
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment newFragment = new YourFragmentClass();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.your_fragment_id, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
        return true;
    case R.id.menu_option2:
        //do something else;
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
    }
    

    Alternatively, if your menu is a child of one of your Fragments (which it should be for the sake of more reusable code), then one method is to require that host Activity to implement an interface defined by the Fragment, that can be used as a call back. And in the onOptionsItemSelected() callback inside your fragment class, you simply make a call to this callback method.

    Although it sounds like a mouthful, you only really need to do a couple of things. Pretend that this is your Fragment class

    public static class FragmentA extends ListFragment {
    OnSelectedListener mListener;
    // Container Activity must implement this interface
    public interface OnSelectedListener {
        public void onSelected();
    }
    ...
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //This is to ensure that the Activity has implemented the interface we set up above
        try {
            mListener = (OnSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnSelectedListener");
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    //...
    switch (item.getItemId()) {
    case R.id.menu_option1:
        //do something
        getActivity().onSelected();
        return true;
    case R.id.menu_option2:
        //do something else;
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
    }
    ...
    }
    

    Then in the Activity, you would see something like:

    public class MainActivity extends Activity implements FragmentA.onSelectedListener{
    ...
    public void onSelected(){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment newFragment = new YourFragmentClass();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.your_fragment_id, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
    }
    
    0 讨论(0)
  • 2021-01-04 04:59

    if the user selects a menu option I want to refresh or redraw the View that is associated with Fragment B and can’t understand how to make this work

    In onOptionsItemSelected(), have the activity call a method on the fragment that causes it to update its widgets with the new content. Or, have the activity execute a FragmentTransaction to replace the fragment (if the fragment was originally set up via a FragmentTransaction).

    0 讨论(0)
提交回复
热议问题