简体   繁体   中英

Change Fragment on button click

I'm triying to change the fragment when I click a button. But nothing happends, I don't know if the problem is on the button click or when I try to change the fragment.. I have a navigationDrawer and when I click on the 3 section I change the fragment of my ActivityMain to Another with this code (is case 3):

public void onSectionAttached(int number) {
        android.app.Fragment fragment;
        android.app.FragmentManager fragmentManager;
        switch (number) {
            case 1:
                mTitle = getString(R.string.inicio);
                break;
            case 2:
                mTitle = getString(R.string.crafteos);
                fragment = new crafteos();
                fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
                break;
            case 3:
                mTitle = getString(R.string.v_pro);
                fragment = new Conexion();
                fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
                break;
        }
    }

It Works fine, then inside the Conexion() Fragment I have two buttons with ids: btconectar and btRegistro. And when I click on the "btconectar" button I want to change the actual fragment to the LogIn() fragment but it doesn't work.. My codes:

fragment_conexion.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/Blanco"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.barwill94.wikicraft.Conexion">

    <!-- TODO: Update blank fragment layout -->
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
   <Button
       android:layout_width="200dp"
       android:layout_height="200dp"
       android:gravity="center"
       android:id="@+id/BTConectarse"
       android:layout_gravity="center_horizontal"
       android:layout_marginTop="10dp"
       android:text="@string/iniciar_sesion"/>

            <Button
                android:layout_width="200dp"
                android:id="@+id/BTRegistro"
                android:layout_height="200dp"
                android:gravity="center"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="@string/registrarme"/>


        </LinearLayout>
    </ScrollView>

</FrameLayout>

Conexion.java (fragment):

public class Conexion extends Fragment {
    Button btconectar, btregistrarse;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Conexion.
     */
    // TODO: Rename and change types and number of parameters
    public static Conexion newInstance(String param1, String param2) {
        Conexion fragment = new Conexion();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public Conexion() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View myInflatedView = inflater.inflate(R.layout.fragment_conexion, container, false);

         btconectar = (Button) myInflatedView.findViewById(R.id.BTConectarse);
        btregistrarse = (Button) myInflatedView.findViewById(R.id.BTRegistro);


        btconectar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Fragment newFragment = new LogIn();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.container, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
        });




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

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }



    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }



}

You have issue in onCreateView method. You return diffrent view as you set. On the end of this method you return

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

Change it to:

return myInflatedView;

you have to return myInflatedView

return myInflatedView

in place of

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

inflate returns a new View's instance, and you attached the click listener to the myInflatedView's components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM