android onClick Button 内 fragment 不工作和没有错误

发布时间: 2016/6/25 15:55:54
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我创建了 fragment 有一些按钮和我试图拨打其他电话 fragment 通过点击 button 但没有任何反应,没有错误,没有行动。

这是我的实现 ︰

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:

你错过 .commit() 在你 fragmentTransaction 语句。

 fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
赞助商