导航抽屉点击中未显示的片段

时间:2016-01-26 23:10:19

标签: java android android-fragments

我是Android开发人员的新手,几年来一直在为ios开发,但在我的第一个Android应用程序中遇到了问题。

我正在开发一个应用程序,在开始时你已经登录,所以我创建了UI和登录按钮,到目前为止工作得很好,该应用程序能够连接到数据库下载必要的信息到它通过xml工作。

下载的Xml为导航栏中的项目命名, 基本上所有导航抽屉项目都应该引导您到同一个片段,内容在将下载的xml的内容加载到它时会发生变化,但窗口保持不变。

为了做到这一点,我以编程方式创建了导航抽屉项目,在新的xml中为片段添加了ui。

当运行应用程序时,一切都运行良好,一旦您登录,您可以通过向右滑动打开NavigationDrawer,显示正确的项目和名称,但是当单击项目时......没有任何反应。 我使用LOGE来了解代码是否正常工作,并且巫婆菜单项试图打开,它确实返回正确的菜单项等。

如果片段中有代码,它会执行代码并返回预期的代码,但它根本不可见。

这是我在MainActivity中的onNavigationItemSelected。

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    String title = item.getTitle().toString();
    int order = (int) getMenuItemTag(item);
    if (title != "Modos Personales" && title != "Home") {
        Log.e("onNavigationItem: ", Integer.toString(order));

        Fragment fragment = null;

        Class fragmentClass;

        fragmentClass = MainFragment.class;

        try {
            fragment = (Fragment) fragmentClass.newInstance();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.flContent, fragment);
            fragmentTransaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //FragmentManager fragmentManager = getFragmentManager();
        //android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        //fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

    }

以下是我的主要活动“activity_main.xml”

的XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<FrameLayout
    android:id="@+id/flContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

这是app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true"
    tools:context="com.paranoidinteractive.heimer.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:foreground="@mipmap/logo_menu"
    android:foregroundGravity="center">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:theme="@style/Base.Theme.AppCompat.Light"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />


</android.support.design.widget.CoordinatorLayout>

这是我的片段“room_view.xml”

的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/room_view"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorScreen"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_gravity="center_vertical" />
</LinearLayout>

这是我的片段“MainFragment.java”

public class MainFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        int number = 0;

        if (inflater != null){
            Log.e("INFLATER NO NULL: ", Integer.toString(number));
        } else {
            Log.e("INFLATER NULL: ", Integer.toString(number));
        }

        if (container != null){
            Log.e("CONTAINTER NO NULL: ", Integer.toString(number));
        } else {
            Log.e("CONTAINER NULL: ", Integer.toString(number));
        }



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

如果您查看LOGCAT,它总是返回:

  

01-26 16:27:36.771 9259-9259 / com.paranoidinteractive.heimer E / INFLATER NO NULL :: 0

     

01-26 16:27:36.771 9259-9259 / com.paranoidinteractive.heimer E / CONTAINER NO NULL :: 0

所以我知道它的工作和到达那里,但它根本没有显示它。

我已经按照所有可用的教程阅读了大量的问题和答案,但到目前为止,没有一个答案对我有帮助,所以我决定发布这个问题。

提前感谢您的帮助,希望您能帮助我。 对不起,发布日志!

0 个答案:

没有答案
相关问题