我正在开发包含五个标签的Android应用程序。每个选项卡需要多个页面并在选项卡上重新选择,它应该在每个选项卡的第一个片段上。以下是我的代码;
public class PagerAdaptor extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdaptor(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
CuddllFragment tab1 = new CuddllFragment ();
return tab1;
case 1:
MyCuddllFragment tab2 = new MyCuddllFragment();
return tab2;
case 2:
// CuddllConversationFragment tab3 = new CuddllConversationFragment();
ConversationFragment conversationFragment = new ConversationFragment();
return conversationFragment;
case 3:
// CuddllNotificationsFragment tab4 = new CuddllNotificationsFragment();
Tab4ContainerFragment notificationPageFragment = new Tab4ContainerFragment();
return notificationPageFragment;
case 4:
MyProfileFragment tab5 = new MyProfileFragment();
return tab5;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
我的寻呼机适配器类如下:
type Instance() => this;
答案 0 :(得分:0)
<强> Tab.xml 强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TabFragment"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"/>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/orange" />
</LinearLayout>
此处 TabFragment.Java
package com.example.sachin.omcommunication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
private int[] tabIcons = {
R.drawable.tab_home,
R.drawable.arrow,
R.drawable.tab_home
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.activity_home_page,null);
tabLayout = (TabLayout) x.findViewById(R.id.sliding_tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
//You tab icons
int[] icons = {
R.drawable.tab_home,
R.drawable.arrow,
R.drawable.tab_home
};
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(icons[i]);
}
}
});
tabLayout.setupWithViewPager(viewPager);
return x;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public android.support.v4.app.Fragment getItem(int position)
{
switch (position){
case 0 : return new HomePage();
case 1 : return new Attendence();
case 2 : return new Delivery();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
}
return null;
}
}
}
试试这个..可能适合你。