I have added 8 Fragments to my adapter ViewPagerAdapter in a ArrayList using MainActivity class. Here is my Code:
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;
}
}
So, i want to click on tab and add the corresponding fragment in Viewpager.But whenever i click first tab at postion 0, it is loading fragment at position 1. I am not able to know why my getitem() method of Adapter is not working properly. Please help. thanks in advance.
change your getItem()
to below, increase position
by one.
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
and change your setupTabIcons' to 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]);
}
}
I have tested your code it should work for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.