android片段视图未显示

时间:2016-07-18 20:08:45

标签: android fragment android-fragmentactivity

我正在尝试通过从导航视图中选择来加载带有webview的片段类。主要活动布局有一个FrameLayout

这是我的片段类

public class HomeFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

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

        View v = inflater.inflate(R.layout.fragment_home, container, true);
        WebView lView = (WebView) v.findViewById(R.id.webPage);

        String helpFileName = "homepage.html";
        lView.clearHistory();
        lView.getSettings().setDomStorageEnabled(true);
        lView.getSettings().setJavaScriptEnabled(true);
        lView.loadDataWithBaseURL("file:///android_asset/", helpFileName, "text/html", "utf-8", null);
        return v;
    }
}

这是我的MainActivity布局

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <android.support.v7.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textSize="22dp" />

                <ImageView
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:background="@drawable/white_logo"
                    android:layout_margin="5dp"/>

            </android.support.v7.widget.Toolbar>

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

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

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

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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_navigation"/>

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

这是我的MainActivity

public class MainActivity extends FragmentActivity {
    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    private Menu menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);

        //setSupportActionBar(toolbar);
        initNavigationDrawer();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        this.menu = menu;

        getMenuInflater().inflate(R.menu.menu_main, menu);

        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        MenuItem item = menu.findItem(R.id.action_search);
        item.setVisible(true);
        item.setEnabled(true);

        return true;
    }

    public void initNavigationDrawer() {

        NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                selectedDrawerMenu(menuItem);
                return true;
            }
        });

        View header = navigationView.getHeaderView(0);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawer);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View v){
                super.onDrawerClosed(v);
            }
            @Override
            public void onDrawerOpened(View v) {
                super.onDrawerOpened(v);
            }
        };

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
    }

    public void selectedDrawerMenu(MenuItem menuItem) {
        // Create a new fragment and specify the fragment to show based on nav item clicked
        Fragment fragment = null;
        Class fragmentClass = null;
        int id = menuItem.getItemId();
        switch (id){
            case R.id.home:
                invalidateOptionsMenu();

                fragmentClass = HomeFragment.class;
                Toast.makeText(getApplicationContext(),"Home",Toast.LENGTH_SHORT).show();

                break;
            case R.id.u_guide:
                invalidateOptionsMenu();

                 Toast.makeText(getApplicationContext(),"User Guide",Toast.LENGTH_SHORT).show();

                break;
            case R.id.logout:
                Intent in = new Intent(MainActivity.this, PinVerification.class);
                startActivity(in);
                finish();
        }

        if(fragmentClass!=null) {
            try {
                fragment = (Fragment) fragmentClass.newInstance();

                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        drawerLayout.closeDrawers();
    }
}

如何在活动中显示片段视图

0 个答案:

没有答案