我要为此创建刷卡应用程序,我在Android中使用ViewPager。当我运行下面的代码时,它成功运行并且打开了蓝色的Fragment,但对此不起作用。你能告诉我为什么吗?

这是我的Activity:

package app.learn.com.learnapp;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import android.os.Bundle;

import java.util.List;
import java.util.Vector;

import menulistFrag.blueColor;
import menulistFrag.greenColor;
import menulistFrag.redColor;


public class MenuActivityList1 extends FragmentActivity {

    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_activity_list1);
        viewPager = (ViewPager)findViewById(R.id.pager);

        instantiateFrags();



    }
    public  void instantiateFrags(){
        List<Fragment> frags = new Vector<Fragment>();
        frags.add(Fragment.instantiate(this, redColor.class.getName()));
        frags.add(Fragment.instantiate(this, greenColor.class.getName()));
        frags.add(Fragment.instantiate(this, blueColor.class.getName()));
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(),frags);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);
    }
}

这是我的PagerAdapter:
package app.learn.com.learnapp;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

import menulistFrag.blueColor;
import menulistFrag.greenColor;
import menulistFrag.redColor;

/**
 * Created by root on 13/9/15.
 */

public class PagerAdapter extends FragmentPagerAdapter {
    List<Fragment> fragments;
    public PagerAdapter(FragmentManager fm,List<Fragment> fragLists){
        super(fm);
        fragments =fragLists;
    }
    @Override
    public Fragment getItem(int position) {
   return fragments.get(position);
    }

    @Override
    public int getCount() {
        return 3;

    }
}

我有三个 fragment :
  • redColor.java
  • greenColor.java
  • blueColor.java

  • 这是我的布局文件:
    <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"
        android:orientation="vertical">
    <android.support.v4.view.ViewPager 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"
        android:id="@+id/pager">
    
    </android.support.v4.view.ViewPager>
    </LinearLayout>
    

    最佳答案

    您的InstantiateFrags函数有点奇怪-通常,您应该在PagerAdapter中声明这些 fragment ,然后使用该 Activity 的onCreate方法实例化PagerAdapter本身。您的PagerAdapter代码可能类似于以下内容:

    public class PagerAdapter extends FragmentPagerAdapter {
        int mNumOfTabs;
    
        public PagerAdapter(FragmentManager fm, int NumOfTabs){
            super(fm);
            this.mNumOfTabs = NumOfTabs;
        }
    
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    blueColor tab1 = new blueColor();
                    return tab1;
                case 1:
                    greenColor tab2 = new greenColor();
                    return tab2;
                case 2:
                    redColor tab3 = new redColor();
                    return tab3;
                default:
                    return null;
            }
        }
    
        @Override
        public int getCount() {
            return mNumOfTabs;
        }
    }
    

    然后在主要 Activity 的onCreate方法中初始化PagerAdapter(我在这里使用了TabLayout):
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Blue"));
    tabLayout.addTab(tabLayout.newTab().setText("Green"));
    tabLayout.addTab(tabLayout.newTab().setText("Red"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    PagerAdapter adapter = new PagerAdapter(
            getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(
            new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    
    });
    

    标签布局和寻呼机的xml:
     <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:layout_alignParentTop="true"
            android:theme="@style/YOUR_THEME_HERE"/>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/tab_layout"/>
    

    10-08 01:33