我已经在 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);
}
}
FragmentTab.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
是一个旨在手动处理 fragment (以及相应选项卡)的小部件。这意味着您无法将其与 ViewPager
结合使用,因为 FragmentTabHost
会将 fragment 直接添加到容器中,而 ViewPager
则管理 fragment 通过适配器(像您的适配器一样,但具有实际执行某些操作的实现)。现在,FragmentTabHost
添加了自己的内部布局,ViewPager
最终将位于实际内容之上。这就是为什么您能够滑动和更改选项卡(因为,虽然您滑动了一个空的 ViewPager
,但您在其上设置了一个 OnPageChangeListener
来更改选项卡)。您无法单击选项卡,因为顶部的 ViewPager
会占用触摸事件(您可以在选项卡顶部滑动以查看它们确实通过相同的 ViewPager
进行了更改) )。
因此,将 ViewPager
与普通的 TabHost
一起使用,有无数关于如何执行此操作的教程。
关于java - 在 FragmentTabHost 中,水平滑动手势起作用时,选项卡触摸不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21451046/