2015-03-13 120 views
0

我正在试图在单击按钮时更改片段。但没有任何事情发生,我不知道是否问题是在按钮单击或当我尝试更改片段.. 我有一个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()片段内我有两个按钮与IDS:btconectar和btRegistro。当我点击“btconectar”按钮,我想实际片段切换到登录()片段,但它不工作..我的代码:

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

您在onCreateView方法中有问题。你设置的时候返回不同的视图。在这个方法的最后返回

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

将其更改为:

return myInflatedView; 
+1

谢谢!现在工作正常! – Guixe94 2015-03-13 09:54:18

+0

我很高兴我可以帮助你 – 2015-03-13 09:54:40

1

你必须回到myInflatedView

return myInflatedView 

到位

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

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

+0

谢谢!现在工作正常! – Guixe94 2015-03-13 09:54:23