我正在尝试构建一些我不确定是否可行的东西。我有 Fragment 选项卡(使用 FragmentActivity、TabHost 和 TabWidget 作为我的 Fragment 选项卡容器),并且在每个我想要的 Fragment 中,作为顶栏,还有另一个 Fragment。您可以将其视为 - 由项目类型构成的底部栏,以及将是过滤器的顶部栏。我想以常规手势在过滤器之间捕捉。
我的应用程序的行为如下:我可以看到第一个片段所在的主要活动,我可以看到顶部/底部栏,但我看不到第一个顶部栏类别的视图(片段)。当我试图捕捉到另一个类别时,什么也没有发生。
这是片段的代码:
Public class Tab1Fragment extends Fragment {
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
private View child;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
child = inflater.inflate(R.layout.tab_frag1_layout, container);
_mViewPager = (ViewPager) child.findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(GlobalContext.getAppContext(),getFragmentManager());
_mViewPager.setCurrentItem(0);
new setAdapterTask().execute();
return (LinearLayout) inflater.inflate(R.layout.tab_frag1_layout,
container, false);
}
private class setAdapterTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
}
}
public void onActivityCreated(Bundle bdl) {
super.onActivityCreated(bdl);
_mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int position) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
child.findViewById(R.id.first_tab).setVisibility(
View.VISIBLE);
child.findViewById(R.id.second_tab)
.setVisibility(View.GONE);
break;
case 1:
child.findViewById(R.id.first_tab).setVisibility(View.GONE);
child.findViewById(R.id.second_tab).setVisibility(
View.VISIBLE);
break;
}
}
});
}
}
这是外部片段的 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >
<TableLayout
style="@style/layout_f_w"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:background="#dddddd"
style="@style/layout_wrap">
<!-- First Tab -->
<LinearLayout
style="@style/layout_f_w"
android:id="@+id/first_text"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/text_title"
android:text="Tab1" />
</LinearLayout>
<!-- Second Tab -->
<LinearLayout
style="@style/layout_f_w"
android:id="@+id/second_text"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/text_title"
android:text="Tab2" />
</LinearLayout>
</TableRow>
</TableLayout>
<!-- Include Tab Indicator -->
<include layout="@layout/indicator" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/viewPager" />
</LinearLayout>
这是我的第一个类别片段(第一个片段内的片段类别)的示例:
public class LayoutOne extends Fragment {
public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
return root;
}
}
请帮我 :(
这是我的片段适配器:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private Context _context;
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context=context;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
case 0:
f=LayoutOne.newInstance(_context);
break;
case 1:
f=LayoutTwo.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return false;
}
}