getitem()没有在我的ViewPager中返回被调用的Fragment

时间:2016-01-15 11:33:36

标签: android android-fragments arraylist android-viewpager android-tablayout

我使用 MainActivity 类在ArrayList中为我的适配器 ViewPagerAdapter 添加了8个片段。这是我的代码:

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager fm, List<Fragment> mFragmentList) {
    super(fm);
    this.mFragmentList =mFragmentList ;
}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
  }

@Override
public int getCount() {
    return mFragmentList.size();
}

//Method for adding Fragment
public void addFragment(Fragment fragment, String title) {
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
    return mFragmentTitleList.get(position);
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private Toolbar toolBar;
private TabLayout tabLayout;
private ViewPager viewPager;
public static final String MYTAG = "mytag";
private int[] tabIcons = {R.drawable.parkingicon, R.drawable.childrenparkicon, R.drawable.cafeteriaicon,
        R.drawable.teaicon, R.drawable.stationaryicon, R.drawable.securityicons, R.drawable.tumtumicon,
        R.drawable.wastebinicon};

static final LatLng ZOOM = new LatLng(19.129, 72.915);
public static GoogleMap googleMap;
private static Context mContext;
//Context context= getApplicationContext();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = getApplicationContext();

    //ToolBar Initialisation
    toolBar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolBar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //ViewPager Intialisation
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    //TabLAyout Initialisation
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    Log.d("Tag", "i am before iconing");
    setupTabIcons();
    Log.d("Tag", "i am after iconing");

    try {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().
                    findFragmentById(R.id.map)).getMap();
        }
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        googleMap.setMyLocationEnabled(true);
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ZOOM, 15));
        Marker marker = googleMap.addMarker(new MarkerOptions().position(ZOOM).title("MYCheck Marker"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.d("Tag", "i am after google map calling");
}

private void setupTabIcons() {
    for (int i = 0; i < 8; i++) {
        Log.d("Tag", "i called tab at "+i);
        tabLayout.getTabAt(i).setIcon(tabIcons[i]);
    }
}

private void setupViewPager(ViewPager viewPager) {
    Log.d("Tag", "i am  goring to call ViewpagerAdapter");
    //Calling the Adapter Here
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new Parking(), "Parkings");
    adapter.addFragment(new ChildrenPark(), "Children Parks");
    adapter.addFragment(new Cafeteria(), "Cafeteria");
    adapter.addFragment(new Canteens(), "Canteens");
    adapter.addFragment(new Stationary(), "Stationery");
    adapter.addFragment(new SecurityCheck(), "Security");
    adapter.addFragment(new TumTum(), "Tum Tum");
    adapter.addFragment(new WasteBins(), "WasteBins");
    viewPager.setAdapter(adapter);
    Log.d("Tag", "i had set the Adapter for you and now moving out after adding all adapters. END");
}

public Context getContext(){
    return mContext;
}

}

所以,我想点击选项卡并在Viewpager.But中添加相应的片段,每当我点击位置0的第一个标签时,它会在位置1加载片段。我无法知道为什么我的 getitem( )适配器的方法无法正常工作。请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

将您的getItem()更改为以下,将position增加一个。

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
  }

并将您的setupTabIcons'更改为tabIcons.length

 private void setupTabIcons() {
        for (int i = 0; i > tabIcons.length; i++) {
            Log.d("Tag", "i called tab at "+i);
            tabLayout.getTabAt(i).setIcon(tabIcons[i]);
        }
    }

我已经测试了它应该适合您的代码。