【问题标题】:Change between fragment navigation drawer viewpagerindicator片段导航抽屉视图页面指示器之间的更改
【发布时间】:2013-11-22 10:00:12
【问题描述】:

我在 content_frame 中有一个导航抽屉和一个 ViewPagerIndicator。我想在导航抽屉中的片段之间进行更改,但问题是标签之间的切换 ViewPager 有效,但是当我切换到另一个选项时,您必须删除 ViewPager并放置一个新片段将这个片段放在 ViewPager 下。

附上照片,以便您查看。

1) 这里已经可以看到ViewPager下面的新片段了,可以通过显示的文字来区分是Prueba

附上代码

Activity_main.xml

<android.support.v4.widget.DrawerLayout   
 xmlns:android="http://schemas.android.com/apk/res/android"    
 android:id="@+id/drawer_layout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

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

    <LinearLayout 
        android:id="@+id/content_linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <com.viewpagerindicator.TabPageIndicator
            android:id="@+id/indicator"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>

</FrameLayout>

<ListView 
 android:id="@+id/left_drawer"
 android:layout_width="240dp"
 android:layout_height="match_parent"
 android:layout_gravity="start"
 android:choiceMode="singleChoice"
 android:divider="@android:color/transparent"
 android:dividerHeight="0dp"
 android:background="#111"/>

片段之间的切换方法。

   /** Main content by replacing fragments
 * @param position
 */
private void selectItem(int position) {
    //Code
    Fragment fragment = new TryFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    switch (position) {
    case 0:
        mPager.setCurrentItem(0);
        break;

    case 1:
        mPager.setCurrentItem(1);
        break;

    case 2:

        ft.replace(R.id.content_frame, fragment);

        break;

    }        
    ft.commit();
    mAdapter.notifyDataSetChanged();
    //Close drawer
    mDrawerLayout.closeDrawer(mDrawerList);
}

类 TryFragment

    class TryFragment extends Fragment
{
    public static final String  ARG_PLANET_NUMBER   = "planet_number";

    public TryFragment()
    {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
           TextView text = new TextView(getActivity());
            text.setGravity(Gravity.CENTER);
            text.setText("Prueba");
            text.setTextSize(20 * getResources().getDisplayMetrics().density);
            text.setPadding(20, 20, 20, 20);

            LinearLayout layout = new LinearLayout(getActivity());
            layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            layout.setGravity(Gravity.CENTER);
            layout.addView(text);

            Log.d("TestFragment", "Estoy en el fragment");

            return layout;
    }
}

不知道是不是必须销毁viewpager才能显示新的fragment或者xml中的层级问题。

【问题讨论】:

    标签: android android-fragments android-viewpager navigation-drawer viewpagerindicator


    【解决方案1】:

    你的ViewPager必须是Fragment,而且应该放在layout.content_frame中,所以当你调用ft.replace(R.id.content_frame, fragment);您的 ViewPager 片段将被替换。

    【讨论】:

    • 我不懂你。
    • 据我了解,从导航抽屉中选择另一个项目时,您需要替换 ViewPager。所以需要把ViewPager放到单独的Fragment中,需要的时候放到contant_frame中。
    • 我有 3 个选项,第一个和第二个选项卡是 ViewPager,第三个是我必须删除 ViewPager 显示该片段。
    • 那么你需要在选择选项3时隐藏content_linear
    • findViewById(R.id.content_linear).SetVisibitity(View.GONE); ft.replace(R.id.content_frame, 片段);
    【解决方案2】:

    您不应同时使用 ViewPager 和 NavigationDrawer。您可以在这篇 G+ 帖子中看到 Roman Nurik 的回答: https://plus.google.com/u/1/+DavidTaSmoochibi/posts/8dWEkFcbTFX

    “您不应该将导航抽屉与操作栏选项卡一起使用。如果您的目标是类似于 Google Play 音乐的 UI,您应该手动实现选项卡(并注意它在平板电脑上的外观 - 你不要'不想要平板电脑上的全宽标签栏)。还请务必查看今年 Google I/O 的 Android 应用程序设计中的结构,详细了解可用于公开应用程序层次结构的各种界面模式。”

    【讨论】:

    • 问题是,如果我使用操作栏选项卡,它会显示在选项卡上,这会显示在选项卡下方。唯一没有发生在我身上的是 ViewPager。
    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多