0

我已经在 Fragment Activity 中实现了水平滑动手势 Fragment TabHost,它工作正常。但是 Tab 触摸不起作用。

源代码在这里:https ://github.com/babumirdha/FragmentTabSwipeExample

源代码如下:

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>

MyPagerAdapter.Java

   public class MyPagerAdapter extends PagerAdapter {
private Context ctx;

public MyPagerAdapter(Context ctx) {
    this.ctx = ctx;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

    return container;
}

@Override
public int getCount() {
    return 3;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return (view == object);
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

@Override
public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}
 }

片段标签.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>

如何在此处添加滑动手势?

提前感谢您的帮助。

4

1 回答 1

2

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

因此,将 aViewPager与 normal 一起使用TabHost,有无数关于如何做到这一点的教程。

于 2014-02-04T19:30:27.410 回答