java Tab touch在FragmentTabHost中使用水平滑动手势时不起作用
我已经在Fragment活动中实现了水平滑动手势Fragment TabHost,效果很好。但触控键不起作用
这里的源代码:https://github.com/babumirdha/FragmentTabSwipeExample
源代码如下:
主要碎片活动。爪哇
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(安卓.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);
}
}
片段\选项卡\主机。xml
<安卓.support.v4.app.FragmentTabHost
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@安卓:id/tabhost"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" >
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >
<TabWidget
安卓:id="@安卓:id/tabs"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_weight="0"
安卓:orientation="horizontal" />
<FrameLayout
安卓:id="@+id/tabFrameLayout"
安卓:layout_width="match_parent"
安卓:layout_height="0dp"
安卓:layout_weight="1" />
<安卓.support.v4.view.ViewPager
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/pager"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"/>
</LinearLayout>
MyPagerAdapter。爪哇
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);
}
}
碎片标签。爪哇
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;
}
}
页面布局。xml
<LinearLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:gravity="center_horizontal"
安卓:orientation="vertical" >
<TextView
安卓:id="@+id/text"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:gravity="center_vertical|center_horizontal"
安卓:text="@string/hello_world"
安卓:textAppearance="?安卓:attr/textAppearanceMedium" />
</LinearLayout>
如何在此处添加滑动手势
提前谢谢你的帮助
# 1 楼答案
您的代码将失败,因为
FragmentTabHost
是一个设计用于手动处理片段(以及相应选项卡)的小部件。这意味着您不能将它与ViewPager
组合,因为FragmentTabHost
将直接将片段添加到容器中,而ViewPager
通过适配器管理片段(与您的适配器类似,但使用实际执行某些操作的实现)。现在FragmentTabHost
添加了自己的内部布局,ViewPager
最终将位于实际内容之上。这就是为什么您能够滑动和更改选项卡(因为,尽管您滑动了一个空的ViewPager
,但它上设置了一个OnPageChangeListener
来更改选项卡)。您无法单击选项卡,因为顶部的ViewPager
会吃掉触摸事件(您可以在选项卡顶部滑动,查看它们是否通过相同的ViewPager
进行更改)因此,使用
ViewPager
和普通的TabHost
,有无数的教程介绍如何做到这一点