改变碎片。为什么它不起作用

时间:2017-04-23 20:21:46

标签: java android android-fragments

我在Android Java中使用导航抽屉更改了此代码。只需按照官方谷歌开发帖和其他页面,但它仍然无法正常工作。只是页面的页眉改变而不是布局。请告诉我为什么,我不会问我是不是经常尝试并阅读其他帖子。

所以这是我的代码: 在MainActivity Java文件

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        Fragment f = null;
        Class fClass = null;
        // Tasklist
        if (id == R.id.nav_drawer_main_tasklist) {
            fClass = TaskList.class;
        }
    try{
            f=(Fragment) fClass.newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_main,f).commit();

        item.setChecked(true);
        setTitle(item.getTitle());
        // Drawer
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlayout_main);
        drawer.closeDrawer(GravityCompat.START

);

在TaskList Java

public class TaskList extends Fragment {

View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
    return super.onCreateView(inflater, container, savedInstanceState);

}

在任务列表XML文件

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="***">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_main_createtask"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="bottom|end"
        android:onClick="fab_main_createtask_onClick"
        app:srcCompat="@drawable/ic_add_white_24dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">



    </LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
    return super.onCreateView(inflater, container, savedInstanceState);

}

以上是错误的,返回膨胀的布局并删除可空。

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
    return view;

}

答案 1 :(得分:0)

您必须return自己的fragment视图。更新您的TaskList片段,如下所示:

public class TaskList extends Fragment {

    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
       view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
       return view ;  
   }
}