【问题标题】:How to swap ViewPager with fragment如何用片段交换 ViewPager
【发布时间】:2015-05-30 11:08:00
【问题描述】:

我有带有 ViewPager 和一些列表的标签栏,我可以单击它们并将我的 ViewPager 放在 Fragmant 后面。我在使用替换更改带有片段的 android.support.v4.view.ViewPager 时遇到了一些问题。 这是我包含它们的布局:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="6">
            <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/orders_fragment"
                android:name="fragments.OrderFragment1"
                tools:layout="@layout/fragment_2" />


            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/primaryColor"/>



        </RelativeLayout>

我正在尝试这样做:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction=manager.beginTransaction();
            Fragment f1 = manager.findFragmentById(R.id.pager);
            transaction.remove(f1);
            transaction.commit();
        }
    });

我错了吗?我能以我的方式做到这一点吗?还是有一些特殊的方法来做这种事情?我是新来的,所以如果有人有一个例子或其他东西,那就太棒了。

【问题讨论】:

  • transaction.remove(f1);是 transaction.replace(R.id.orders_fragment,f1);以及。好久没睡了。

标签: java android android-fragments android-studio


【解决方案1】:

ViewPager 应该是布局的第一个元素。现在它可能是片段不可点击和聚焦。试试这个:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="6">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/primaryColor"/>

             <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/orders_fragment"
                android:name="fragments.OrderFragment1"
                tools:layout="@layout/fragment_2" />


        </RelativeLayout>

另外,这里有一个viewPager 的例子

【讨论】:

  • 如果ViewPage之后会有片段我们会在它上面看到,我希望它只有在点击List后才会看到
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多