问题描述
我创建了一个带有一些按钮的片段,然后尝试通过单击按钮来调用其他片段,但是没有任何反应,没有错误,没有任何动作。
这是我的实现:
main_activit.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/grey"
tools:context=".MainActivity"
android:weightSum="1"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="@+id/mainToobar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="@string/mainToobarTitle"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/homebuttons_fragment_container"
android:layout_width="match_parent"
android:layout_height="200dp" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
HomeButtonsFragment.java
public class HomeButtonsFragment extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
) {
View view = inflater.inflate(R.layout.fragment_home_buttons, container, false);
Button button = (Button) view.findViewById(R.id.schedule_home_button);
button.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ScheduleFragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment);
}
}
fragment_home_button.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/schedule_home_button"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:text="@string/label.home.button.schedule"
android:tag="home_button"
android:stateListAnimator="@null"
style="@style/ScheduleButton"
/>
</RelativeLayout>
1楼
您在fragmentTransaction
语句中错过了.commit()
。
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
2楼
你错过了commit(); ,我确定有时候会遗漏一些小东西,只需添加commit();即可。 ,使用此FragmentManagerfragmentManager = getFragmentManager();即可正常工作 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ScheduleFragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
3楼
@Override public void onClick(View v){FragmentManagerfragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ScheduleFragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
或 fragmentTransaction.commit(); }
4楼
您的onClick方法应为
@Override
public void onClick(View v) {
if(v.getId()==R.id.schedule_home_button){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ScheduleFragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
}
}