我正在試圖在單擊按鈕時更改片段。但沒有任何事情發生,我不知道是否問題是在按鈕單擊或當我嘗試更改片段.. 我有一個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);
}
}
謝謝!現在工作正常! – Guixe94 2015-03-13 09:54:18
我很高興我可以幫助你 – 2015-03-13 09:54:40