按钮点击更改片段

时间:2015-03-13 09:36:35

标签: android android-fragments onclicklistener

当我点击按钮时,我正试图改变片段。但没有任何事情发生,我不知道问题是在点击按钮上还是当我尝试更改片段时... 我有一个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);
    }



}

2 个答案:

答案 0 :(得分:2)

你在onCreateView方法中遇到了问题。您在设置时返回不同的视图。在此方法结束时,您将返回

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

将其更改为:

return myInflatedView;

答案 1 :(得分:1)

你必须返回myInflatedView

return myInflatedView

取代

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

inflate返回一个新的View实例,并将点击监听器附加到myInflatedView的组件。