如何在ViewPager中使用ViewPager

时间:2017-05-14 11:37:57

标签: android tabs android-viewpager

我有分类&子类别。我必须在viewpager中创建viewpager但我不能显示Frag2内容... tnx 4帮助 enter image description here

activity_tab.xml& TabActivity



public class TabActivity extends AppCompatActivity {
    MaterialTabs tabHost;
    ViewPager pager;
    ViewPagerAdapter1 pagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);
        ViewPagerAdapter1 adapter = new                          ViewPagerAdapter1(getSupportFragmentManager());
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".testttttttttt.TabActivity">

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"/>

</android.support.v4.view.ViewPager>
&#13;
&#13;
&#13; ViewPagerAdapter1.java

&#13;
&#13;
public class ViewPagerAdapter1 extends FragmentStatePagerAdapter {
    public ViewPagerAdapter1(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new Frag1();
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "TAB " + (position + 1);
    }

}
&#13;
&#13;
&#13; Frag1.java

&#13;
&#13;
public class Frag1 extends Fragment {


    View view;
    MaterialTabs tabHost;
    ViewPager pager;
    ViewPagerAdapter2 pagerAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_tab1, container, false);

        //Init View Pager
        tabHost = (MaterialTabs) view.findViewById(R.id.tabHost);
        pager = (ViewPager) view.findViewById(R.id.pager);
        pagerAdapter = new ViewPagerAdapter2(getFragmentManager());
        pager.setAdapter(pagerAdapter);
        tabHost.setViewPager(pager);

        // Set Custom Font
        Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/BYekan.ttf");
        tabHost.setTypeface(typeface, Typeface.NORMAL);

        pager.setOffscreenPageLimit(5);
        pager.setCurrentItem(4);
        return view;
    }
}
&#13;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="right"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffea00">


    <ir.negaryab.negaryab.Util.MaterialTab.MaterialTabs
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/ColorPrimary"
        android:textColor="@color/Gray"
        app:mtIndicatorColor="@color/ScrollColor"
        app:mtTextColorSelected="@color/White"
        app:mtMrlRippleColor="@color/ScrollColor"
        app:mtTextSelectedStyle="bold"
        app:mtTextUnselectedStyle="normal"
        app:mtSameWeightTabs="false"
        app:mtPaddingMiddle="false" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabHost" />

</LinearLayout>
&#13;
&#13;
&#13; ViewPagerAdapter2.java

&#13;
&#13;
public class ViewPagerAdapter2 extends FragmentStatePagerAdapter {

    public ViewPagerAdapter2(FragmentManager fm) {
        super(fm);
    }

    public Fragment getItem(int num) {
        switch (num) {
            case 0:
                return new Frag2();
            case 1:
                return new Frag2();
            case 2:
                return new Frag2();
            case 3:
                return new Frag2();
            default:
                break;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 4;
    }
String[] title={"فکت","ثبت طلا","ثبت آر","آرمن"};
    @Override
    public CharSequence getPageTitle(int position) {
        return title[position];
    }

}
&#13;
&#13;
&#13; Frag2.java

&#13;
&#13;
public class Frag2 extends Fragment {
String ss="";

    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_tab2, container, false);
        TextView txt=(TextView)view.findViewById(R.id.txt);
        txt.setText("khghjghj");
        return view;
    }
}
&#13;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000">
<TextView
    android:text="fgfffgg"
    android:textSize="24dp"
    android:textColor="@color/Black"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/txt"
    android:gravity="center_vertical|center_horizontal"
    android:layout_gravity="center_horizontal" />
</LinearLayout>
&#13;
&#13;
&#13;

0 个答案:

没有答案