我已经在 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>
如何在此处添加滑动手势?
提前感谢您的帮助。