Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当水平滑动手势在FragmentTabHost中起作用时,Tab touch不起作用_Java_Android_Android Fragments_Swipe Gesture_Fragment Tab Host - Fatal编程技术网

Java 当水平滑动手势在FragmentTabHost中起作用时,Tab touch不起作用

Java 当水平滑动手势在FragmentTabHost中起作用时,Tab touch不起作用,java,android,android-fragments,swipe-gesture,fragment-tab-host,Java,Android,Android Fragments,Swipe Gesture,Fragment Tab Host,我已经在Fragment活动中实现了水平滑动手势Fragment TabHost,效果很好。但触控键不起作用 源代码如下: 源代码如下: MainFragmentActivity.Java public class MainFragmentActivity extends FragmentActivity implements OnPageChangeListener, OnTabChangeListener { private FragmentTabHost host;

我已经在Fragment活动中实现了水平滑动手势Fragment TabHost,效果很好。但触控键不起作用

源代码如下:

源代码如下:

MainFragmentActivity.Java

public class MainFragmentActivity extends FragmentActivity implements
     OnPageChangeListener, OnTabChangeListener  {
private FragmentTabHost host;

    private ViewPager pager;

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

            pager = (ViewPager) findViewById(R.id.pager);

    host = (FragmentTabHost) findViewById(android.R.id.tabhost);

    host.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);

    TabSpec spec = host.newTabSpec("tab1");

    spec.setIndicator("First tab");
    host.addTab(spec,FragmentTab.class, null);

    spec = host.newTabSpec("tab2");

    spec.setIndicator("Second tab");
    host.addTab(spec,FragmentTab.class, null);

    spec = host.newTabSpec("tab3");

    spec.setIndicator("Third tab");
    host.addTab(spec,FragmentTab.class, null);  

            host.setOnTabChangedListener(this);

    pager.setAdapter(new MyPagerAdapter(this));
    pager.setOnPageChangeListener(this);

}

@Override
public void onTabChanged(String tabId) {

    int pageNumber = 0;
    if (tabId.equals("tab1")) {
        pageNumber = 0;
    } else if (tabId.equals("tab2")) {
        pageNumber = 1;
    } else {
        pageNumber = 2;
    }
pager.setCurrentItem(pageNumber);

}

@Override
public void onPageSelected(int pageNumber) {

    host.setCurrentTab(pageNumber);

}


}
fragment_tab_host.xml

 <android.support.v4.app.FragmentTabHost 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" />

    <FrameLayout
        android:id="@+id/tabFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

 <android.support.v4.view.ViewPager  
   xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/pager"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"/>  

</LinearLayout>
Java

 public class FragmentTab extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.page_layout, container, false);
    TextView tv = (TextView) v.findViewById(R.id.text);
    tv.setText(this.getTag() + " Content");
    return v;
  }
 }
page_layout.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/hello_world"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

如何在此处添加滑动手势


提前感谢您的帮助。

您的代码将失败,因为
FragmentTabHost
是一个用于手动处理片段(以及相应选项卡)的小部件。这意味着您不能将其与
ViewPager
组合,因为
FragmentTabHost
将直接将片段添加到容器中,而
ViewPager
通过适配器管理片段(与您的适配器类似,但使用实际执行某些操作的实现)。现在,
FragmentTabHost
添加了自己的内部布局,
ViewPager
将位于实际内容的顶部。这就是您能够滑动和更改选项卡的原因(因为,尽管您滑动的是空的
ViewPager
,但您在其上设置了一个
OnPageChangeListener
来更改选项卡)。您无法单击选项卡,因为顶部的
ViewPager
会吃掉触摸事件(您可以在选项卡顶部滑动,查看它们是否通过相同的
ViewPager
进行更改)


因此,使用一个
ViewPager
和一个普通的
TabHost
,有无数的教程介绍如何做到这一点。

查看类似的文章,这可能会对你有所帮助。是的,我有很多关于普通TabHost的教程,但我需要带tab单击和滑动的FragmentTabHost。是否有任何替代ViewPager的方法可以与FragmentTabHost一起用于选项卡单击和滑动。@BabulMirdha我在回答中已经告诉您为什么不能将
FragmentTabHost
ViewPager
一起使用。您需要使用
FragmentTabHost
有什么原因吗?我得到了您的答案,但我正在搜索ViewPager的替代方案。是的,我有理由使用FragmentTabHost。在我的应用程序中,我使用了导航抽屉。当我单击抽屉项目中的项目时,它会显示一个FragmentTabHost,其中有2个或3个选项卡,需要单击和滑动。它几乎就像YouTube应用程序。这里是演示应用程序的url。@BabulMirdha Youtube应用程序(至少是最新版本)没有使用
FragmentTabHost
(也没有使用
TabHost
)。即使如此,你也不需要那个小部件。选中此示例代码将整个文件复制(并替换)到当前项目,您将看到所需内容。我成功地实现了该代码。谢谢@Luksprog。
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/hello_world"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>