在这里,我将创建这种类型的布局
我已使用自定义NavigationDrawer
完成ActionBar
。现在,我需要在Tabs
的第一个ViewPager
中添加Fragment
NavigationDrawer
。我使用FragmentActivity
完成了此操作,但当我使用Fragment
尝试相同的操作时,我在NullpointerException
中收到navigationDrawerActivity
错误。
NavigationDrawer
的第一个片段,我需要Tabs
和ViewPager
public class HomeFragment extends Fragment
implements OnTabChangeListener, OnPageChangeListener
{
HorizontalScrollView horizontalSV;
HomeFragmentAdapter uAdapter;
private ViewPager mViewPager;
private FragmentTabHost mTabHost;
private FragmentActivity myContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewPager = (ViewPager) getView().findViewById(R.id.viewpager);
horizontalSV = (HorizontalScrollView) getView().findViewById(R.id.horizontal_scrollview);
// Tab Initialization
initialiseTabHost();
uAdapter = new HomeFragmentAdapter(myContext.getSupportFragmentManager());
Fragment first = new First();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.tabcontent, first).commit();
// Fragments and ViewPager Initialization
mViewPager.setAdapter(uAdapter);
mViewPager.setOnPageChangeListener(this);
}
// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
final TabWidget tabWidget = mTabHost.getTabWidget();
final int screenWidth = getActivity().getWindowManager()
.getDefaultDisplay()
.getWidth();
final int leftX = tabWidget.getChildAt(pos).getLeft();
int newX = 0;
newX = leftX + (tabWidget.getChildAt(pos).getWidth() / 2)
- (screenWidth / 2);
if (newX < 0) {
newX = 0;
}
horizontalSV.scrollTo(newX, 0);
}
// Manages the Page changes, synchronizing it with Tabs
// @Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int pos = this.mViewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
}
// @Override
public void onPageSelected(int arg0) {
}
// @Override
public void onPageScrollStateChanged(int arg0) {
}
public void setCurrentItem(int item, boolean smoothScroll) {
mViewPager.setCurrentItem(item, smoothScroll);
}
// Tabs Creation
private void initialiseTabHost(){} {
mTabHost = (FragmentTabHost) getView().findViewById(android.R.id.tabhost);
mTabHost.setup();
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
rllp.addRule(RelativeLayout.CENTER_IN_PARENT);
Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
First.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
Second.class, arg2);
mTabHost.setOnTabChangedListener(this);
}
}
它的Xml类如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<HorizontalScrollView
android:id="@+id/horizontal_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:tabStripEnabled="true" />
</HorizontalScrollView>
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
</TabHost>
</LinearLayout>
片段类是:
public class HomeFragmentAdapter extends FragmentPagerAdapter {
public HomeFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
/* case 0:
return new First();
case 1:
return new Second();*/
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
感谢。