【问题标题】:Creating a Multi-pane layout using android actionbar and navigation drawer使用 android 操作栏和导航抽屉创建多窗格布局
【发布时间】:2014-06-17 10:02:00
【问题描述】:

我目前正在创建一个可以使用片段处理不同屏幕支持和多窗格视图的应用程序。现在我使用 android studio 使用默认的导航抽屉和操作栏,并创建了两个片段来测试这个和纵向布局:fragment_1 和 fragment_2,它们只是替换了 MainActivity 上的容器。但现在的问题是如何在 1 布局中实现 fragment_1 和 fragment_2?我试图研究此link 上的多窗格开发,但我认为要正确实施它,我需要使用 ActivityFragment 但在这种情况下,我只能使用 Fragments,这就是为什么我不知道该怎么做.到目前为止的代码如下:

@Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        switch (position) {
            case 0:
                        fragmentManager.beginTransaction()
                        .replace(R.id.container, FragmentMain.newInstance(position + 1))
                        .commit();
                break;
            case 1:
                fragmentManager.beginTransaction()
                        .replace(R.id.container, FragmentAbout.newInstance(position + 1))
                        .commit();
                break;
            /*create the other cases here for navigation*/
        }
    }

fragment_main.xml(第一个片段)

<RelativeLayout 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"
    tools:context="com.kahel.main.MainActivity$PlaceholderFragment">
    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/list_news"
            android:choiceMode="singleChoice" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

fragment_about.xml(第二个片段)

<RelativeLayout 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"
    tools:context="com.kahel.main.MainActivity$PlaceholderFragment">
    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/list_news"
            android:choiceMode="singleChoice" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

fragment_main.xml(横向)

<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"
    tools:context="com.kahel.main.MainActivity$PlaceholderFragment"
    android:orientation="horizontal"
    android:weightSum="1">
    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container"
        android:layout_width="@dimen/land_scoop_feed"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:id="@+id/list_news"
            android:choiceMode="singleChoice" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

当我尝试更改布局时,它只是强制关闭我的应用程序。 :/

请告诉我要做什么或要搜索什么。谢谢! :)

【问题讨论】:

    标签: android android-fragments android-actionbar android-navigation


    【解决方案1】:

    好吧,我遇到了完全相同的问题,经过大量研究后,我对缺乏这方面的文档感到惊讶,在我看来,这是常见的情况。但是,在尝试了一些事情之后,我想出了一个解决方案。

    我的第一种方法是用“根片段”替换 Navigation Drawer 容器,该容器同时具有两个可用的布局,其中包含我想要的片段。问题是布局上声明的嵌套片段是不允许的。

    为了克服这个其他问题,在我的“最终布局”中,我替换了 FrameLayouts 的片段定义,然后以编程方式将片段加载到“根片段”上。

    总结一下:你有你的导航抽屉,点击你想要的,你用一个“根片段”替换你的片段容器。这个“根片段”有两种布局:一种是默认布局,另一种是横向布局。这些布局包含按照我们想要的方式排列的 FrameLayout 元素,在初始化时被我们的“最终片段”替换。

    我怀疑这是实现带有导航抽屉的多窗格布局目标的最佳方式,但这是我能够做到的最适合我需要的方式。我欢迎任何建议!。

    我用一些代码来说明解决方案。

    MainActivity.java(带有导航抽屉的那个) ...

    void drawerOptionSelected(String option) {
    
        if(option.equals(XXXX)) {
    
            Fragment fragment = new RootFragment();
            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.content_frame, fragment);
            transaction.commit();
    
            drawerList.setItemChecked(menuOptions.indexOf(option), true);
            drawerLayout.closeDrawer(drawerList);
        }
    }
    
    ...
    

    root_fragment_layout.xml(默认)

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/entries_list_single"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    

    root_fragment_layout.xml(横向)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout android:id="@+id/entries_list"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    
        <FrameLayout android:id="@+id/entries_post"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

    RootFragment.java

    public class RootFragment extends Fragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.root_fragment_layout);
    
            if(savedInstanceState != null) { //Fragments already loaded, avoid replacing them again
                return;
            }
    
            ListFragment listFragment = new ListFragment();
            final FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); //IMPORTANT! Use child fragment manager
    
            if (findViewById(R.id.entries_list_single) == null) { //We are loading the landscape layout
                PostFragment postFragment = new PostFragment();
    
                transaction.replace(R.id.entries_list, listFragment);
                transaction.replace(R.id.entries_post, postFragment);
                transaction.commit();
    
            } else { //We are loading the default layout
    
                transaction.replace(R.id.entries_list_single, listFragment);
                transaction.commit();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多