Domanda

I'm new to Android and I'm playing with Fragments, I know that there exist many examples to learn how Fragments work, unfortunately there is not many examples using Nested-Fragments (which are in the same Activity). This is my code below, ive encountered 2 problems:

  1. The Root Fragment is visible after I add the secondary Fragment.

  2. both Fragment use same design, but when I add the FragmentIi can see the dimensions of the widgets and the position on the screen are not the same.

  3. I Need some feedback how to do it right.

Thank you!

public class LoginActivity extends Activity {

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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_login,
                    container, false);

            TextView tv = (TextView) rootView.findViewById(R.id.signUpText1);
            tv.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    FragmentTransaction ft = getFragmentManager()
                            .beginTransaction();
                    ft.replace(R.id.loginViewPlaceholder, new SignupFragment());
                    ft.commit();
                }
            });

            return rootView;
        }
    }

    public static class SignupFragment extends Fragment {

        public SignupFragment() {
            // TODO Auto-generated constructor stub
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View signupView = inflater.inflate(R.layout.fragment_signup,
                    container, false);
            return signupView;
        }
    }

}
È stato utile?

Soluzione

The replace method will remove a fragment from the specified container and add the new one its place. In your example, it looks like you are adding the PlaceholderFragment to the container android.R.content and the SignupFragment to the container R.id.loginViewPlaceholder. If you want the SignupFragment to replace the PlaceholderFragment you will have to either use the same container for both, or you will have to do separate remove() and add() calls for your transaction.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top