单击按钮更改片段

Change Fragment on button click

我正在尝试在单击按钮时更改片段。但是什么也没发生,我不知道问题是在按钮点击上还是在我尝试更改片段时.. 我有一个 navigationDrawer,当我点击第 3 个部分时,我将 ActivityMain 的片段更改为另一个代码(情况 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;
        }
    }

它工作正常,然后在 Conexion() 片段中我有两个带有 id 的按钮:btconectar 和 btRegistro。当我单击 "btconectar" 按钮时,我想将实际片段更改为 LogIn() 片段,但它不起作用。我的代码:

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(片段):

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);
    }



}

你必须returnmyInflatedView

return myInflatedView

代替

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

inflate return一个新的 View 实例,并且您将点击侦听器附加到 myInflatedView 的组件。

您的 onCreateView 方法有问题。您设置的 return 不同视图。在这个方法的最后你 return

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

改为:

return myInflatedView;