我想用view pager实现SlidingTabs。我按照指定的步骤in this link进行了操作。我的情况只有一个区别。我使用片段而不是活动。所以在这种情况下,我想在用户按下标签时给孩子片段充气。
这是我尝试这样做的方法:
//I am using navigation drawer in my activity and when user press one item it create a fragment which I want to show many child fragments using tabs
@Override
public void onItemClick(int position) {
switch (position){
case 3 :
mFragmentManager = getSupportFragmentManager();
mFragment = new MainFragment().newInstance(mHelpLiveo.get(position).getName());
if (mFragment != null){
mFragmentManager.beginTransaction().replace(R.id.container, mFragment).commit();
}
break;
}
}
我的MainFragment.java中的onCreateView函数:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.group_tabs, container, false);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
//EDİT 3 :
adapter = new ViewPagerAdapter(this.getChildFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) rootView.findViewById(R.id.private_public_pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.blue_grey_900);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
return rootView;
}
在我的ViewPagerAdapter类中:
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
Tab1 tab1 = new Tab1();
return tab1;
}
else // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
{
Tab2 tab2 = new Tab2();
return tab2;
}
}
其他任何内容与the link I provided above相同。
编辑1:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.melomg.example.ui.MainFragment">
<com.melomg.example.ui.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/myColor"/>
<android.support.v4.view.ViewPager
android:id="@+id/private_public_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
</LinearLayout>
在mainfragment.xml文件中只出现SlidingTabLayout我无法看到Vİew寻呼机。
编辑2:
一些屏幕来解释我的申请。
答案 0 :(得分:1)
在mainfragment.xml布局中,LinearLayout方向设置为水平,因此项目水平位于同一行。 SlidingTabLayout的宽度是match_parent,因此ViewPager被压缩到右边,宽度为0.只需将方向更改为“vertical”
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.melomg.example.ui.MainFragment">
...
</LinearLayout>