I'm trying to set up a tablayout with 3 tabs, each tab will have a recycler view in it. I found this question: How to implement RecyclerView with CardView rows in a Fragment with TabLayout
Which is similier to what I want, I'm trying to set this up, then I'll set up my data retrieval after I get this working.
The problem I'm getting now it setting up the PagerAdapter. Here is my code:
public class WorkoutDaysActivity extends BaseActivity{
ListView mListView = new ListView(this);
ArrayList<CustomObject> w29w1m;
CustomListViewAdapter mCustomListViewAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
mToolBar = activateToolbar();
setUpNavigationDrawer();
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
PagerAdapter pagerAdapter =
new PagerAdapter(getSupportFragmentManager(), WorkoutDaysActivity.this)
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
}
#Override
public void onResume() {
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class PagerAdapter extends FragmentPagerAdapter {
String tabTitles[] = new String[] { "Tab One", "Tab Two", "Tab Three" };
Context context;
public PagerAdapter(FragmentPagerAdapter fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new WorkoutDetailFragment();
case 1:
return new WorkoutDetailFragment();
case 2:
return new WorkoutDetailFragment();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public View getTabView(int position) {
View tab = LayoutInflater.from(WorkoutDaysActivity.this).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) tab.findViewById(R.id.custom_text);
tv.setText(tabTitles[position]);
return tab;
}
}}
The first spot where I'm getting an error is
PagerAdapter pagerAdapter =
new PagerAdapter(getSupportFragmentManager(), WorkoutDaysActivity.this)
viewPager.setAdapter(pagerAdapter);
The error is:
PagerAdapter (android.support.v4.app.FragmentPagerAdapter, Context) in PagerAdapter cannot be applied to: (android.support.v4.app.FragmentManager, WorkoutDaysActivity.this)
The android.support.v4.app.FragmentManager is the part underlined in red. The second error is in this part:
class PagerAdapter extends FragmentPagerAdapter {
String tabTitles[] = new String[] { "Tab One", "Tab Two", "Tab Three" };
Context context;
public PagerAdapter(FragmentPagerAdapter fm, Context context) {
super(fm);
this.context = context;
}
The error is in the super(fm); part, and it states:
FragmentPagerAdapter (android.support.v4.app.FragmentManager) in FragmentPagerAdapter cannot be applied to: android.support.v4.app.FragmentPagerAdapter
I feel like this would be a simple error with importing the wrong thing, but my attempts to fix it failed. Any help greatly appreciated thank you!
Your PagerAdapter class extends FragmentPagerAdapter. Then its constructor TAKES a FragmentPagerAdapter to initialize. So to initialize a FragmentPagerAdapter you need a FragmentPagerAdapter already?
I think that parameter is supposed to be a FragmentManager.
Related
I have a FragmentActivity with two Fragments in it and a sliding tab layout:
String titles[] = new String[] {"Tab One", "Tab Two"};
int numTabs = titles.length;
EventAdapter adapter = new EventAdapter(getSupportFragmentManager(), titles, numTabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
SlidingTabLayout sliding_tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
sliding_tabs.setDistributeEvenly(true);
sliding_tabs.setViewPager(pager);
With the FragmentPagerAdapter:
private class EventAdapter extends FragmentPagerAdapter {
private List<String> titles;
private int numTabs;
public void addTab (String title) {
this.titles.add(title);
this.numTabs++;
this.notifyDataSetChanged();
}
public EventAdapter(FragmentManager fm, List<String> mTitles, int mNumTabs) {
super(fm);
this.titles = mTitles;
this.numTabs = mNumTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 2:
return new FragThree();
case 1:
return new FragTwo();
default:
return new FragOne();
}
}
#Override
public String getPageTitle(int position) {
return titles.get(position);
}
#Override
public int getCount() {
return numTabs;
}
}
Some time down the line, I would like to dynamically add the third tab to this layout. Is there a way to do this in code? As you can see, I already have the PagerAdapter set up to catch the third tab. I just need to load it in...
You need to add this method to your adapter EventAdapter :
public void addTab (String title) {
this.titles.add(title);
// this is the variable returned from getCount method
this.numbTabs++;
this.notifyDataSetChanged();
}
Main purpose is to add the tab and call notifydatasetchanged.
Now call addTab on this adapter's object and tab will be added.
after addTab do sliding_tab.setViewPager(this.pager);
Please accept if this solves the purpose.
How can I add Tabs & Fragment dynamically ?
My code :
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for (int i = 0; i < 4; i++) {
adapter.addFrag(new TabFragment(), "Tab"+i);
}
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Tab Fragment
public class TabFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activty_listview,container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Suppose my tab names are "Tab1, Tab2, Tab3"
How can I add tabs dynamically according to the Array if I get above and how to tab fragment know which tab selected.
Any suggestions are welcomed.
Thanks in advance:)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="fill_parent"
app:tabMode="fixed"
android:background="#ff0000"
app:tabGravity="fill"
android:layout_height="48dp" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="#+id/sliding_tabs"
android:background="#android:color/white" />
</RelativeLayout>
Activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
int icons[] = {R.mipmap.createuser, R.mipmap.ico_password, R.mipmap.gplus, R.mipmap.fb};
int colors[] = {Color.YELLOW, Color.CYAN, Color.LTGRAY, Color.CYAN};
void initViews() {
TabLayout tabs = (TabLayout) findViewById(R.id.sliding_tabs);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ArrayList<Fragment> fragments = new ArrayList<>(Arrays.asList(new Fragment1(),new Fragment2(),new Fragment3(),new Fragment4()));
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(),fragments);
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);
for (int i = 0; i < 4; i++) {
View view = inflator.inflate(R.layout.tabs,null,false);
TextView title = (TextView)view.findViewById(R.id.title);
RelativeLayout layout = (RelativeLayout)view.findViewById(R.id.layout);
ImageView icon = (ImageView)view.findViewById(R.id.icon);
title.setText("Tab" + i);
layout.setBackgroundColor(colors[i]);
icon.setImageResource(icons[i]);
// tabs.getTabAt(i).setCustomView(view);
tabs.getTabAt(i).setIcon(icons[i]);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Adapter:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by rohit.h on 12/21/2015.
*/
public class ViewPagerAdapter extends FragmentPagerAdapter {
String titles[] = {"Tab1", "Tab2", "Tab3", "Tab4"};
private final List<Fragment> fragments;
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
mTabLayout = (TabLayout)rootView.findViewById(R.id.tab_layout);
mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.tab1)));
mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.tab2)));
mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.tab3)));
mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.tab4)));
mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.tab5)));
mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
mViewPager = (ViewPager)rootView.findViewById(R.id.pager);
mPagerAdapter = new PagerAdapter
(getActivity().getSupportFragmentManager(), mTabLayout.getTabCount());
mViewPager.setAdapter(mPagerAdapter);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
you can refer this link
A simple solution (but not the best/perfect one) would be to add the fragments to adapter and set the tablayout and adapter to viewpager again at runtime. Will not be clean / might be jerky but will definitely work.
EDIT::
There's a method in FragmentPagerAdapter which is set to viewpager and tablayout as well : notifyDataSetChanged(). Use this method after inserting and removing fragments from pager adapter. I tried this for inserting it works. Should definitely work for removing as well.
I am working in a new Android project.
The first activity is using a slider menu and fragments. On the first fragment there is a list view (PrimaryFragmentDormir.java). After selecting one of the rows, a new activity is launched. This last activity uses three tabs, to show different information about the selected row object.
The listview is loaded from remote JSON files.
This is the onItemClick method at PrimaryFragmentDormir.java:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Hotel hotelActual = (Hotel) adapter.getItem(position);
String msg = "Elegiste el hotel " + hotelActual.getNombre();
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), Detalle_Hotel.class);
intent.putExtra("id_hotel", hotelActual.getId_hotel());
intent.putExtra("nombre_hotel", hotelActual.getId_hotel());
intent.putExtra("descripcion_hotel", hotelActual.getId_hotel());
intent.putExtra("latitud_hotel", hotelActual.getId_hotel());
intent.putExtra("longitud_hotel", hotelActual.getId_hotel());
intent.putExtra("direccion_hotel", hotelActual.getId_hotel());
intent.putExtra("web_hotel", hotelActual.getId_hotel());
intent.putExtra("tel_hotel", hotelActual.getId_hotel());
intent.putExtra("tel_reservas", hotelActual.getId_hotel());
intent.putExtra("foto_hotel", hotelActual.getId_hotel());
intent.putExtra("calificacion_hotel", hotelActual.getId_hotel());
intent.putExtra("num_estrellas", hotelActual.getId_hotel());
intent.putExtra("zona_hotel", hotelActual.getId_hotel());
intent.putExtra("facebook_hotel", hotelActual.getFacebook());
intent.putExtra("twitter_hotel", hotelActual.getTwitter());
startActivity(intent);
}
The Toast is shown and the activity Detalle_Hotel is shown also.
Detalle_Hotel has three tabs.
What I need is to get the values from hotelActual in the three tabs, in order to work with them separately.
This is Detalle_Hotel activity:
public class Detalle_Hotel extends AppCompatActivity {
// Declaring Your View and Variables
private String nombre_hotel, foto_hotel, descripcion_hotel,direccion_hotel,web_hotel,tel_hotel,tel_reservas,zona_hotel,facebook_hotel,twitter_hotel;
private int num_estrellas_hotel, id_hotel;
private double calificacion_hotel,latitud_hotel,longitud_hotel;
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Info","Mapa","OpiniĆ³n"};
int Numboftabs =3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detalle__hotel);
nombre_hotel = getIntent().getStringExtra("nombre_hotel");
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.rojomodesto);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
}
Here I received the value from nombre_hotel (as test for the other values), and now how can I pass it to the tabs?
Here is tab1 code:
public class Tab1 extends Fragment {
private TextView hotel_nombre;
private String nombre_hotel;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
#Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
hotel_nombre = (TextView) getView().findViewById(R.id.nombre_hotel);
hotel_nombre.setText(getActivity().nombre_hotel));
}
}
The line hotel_nombre.setText(getActivity().nombre_hotel)); shows a warning at the second nombre_hotel: "Cannot resolve symbol 'nombre_hotel'.
Any help is welcome.
EDIT:
ViewPageAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
if (position == 0) // if the position is 0 we are returning the First tab
{
Tab1 tab1 = new Tab1();
return tab1;
}
if (position == 1) // if the position is 0 we are returning the First tab
{
Tab2 tab2 = new Tab2();
return tab2;
}
if (position == 2) // if the position is 0 we are returning the First tab
{
Tab3 tab3 = new Tab3();
return tab3;
}
return null;
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return NumbOfTabs;
}
}
In your adapter you need to initialize it properly now (with string as argument).
public class Tab1 extends Fragment {
private static final String HOTEL = "hotel";
private TextView hotel_nombre;
private String nombre_hotel;
public static Tab1 newInstance(String s) {
Tab1 result = new Tab1();
Bundle bundle = new Bundle();
bundle.putString(HOTEL, s);
result.setArguments(bundle);
return result;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
nombre_hotel = bundle.getString(HOTEL);
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
#Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
hotel_nombre = (TextView) getView().findViewById(R.id.nombre_hotel);
hotel_nombre.setText(nombre_hotel);
}
}
EDIT:
Change your in your Detalle_Hotel Activity, adapter to:
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs,nombre_hotel);
And then in adapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
private String hotelNumbre;
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb, String hotelNum) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
this.hotelNumbre = hotelNum;
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
if (position == 0) // if the position is 0 we are returning the First tab
{
return Tab1.newInstance(hotelNumbre);
}
if (position == 1) // if the position is 0 we are returning the First tab
{
Tab2 tab2 = new Tab2();
return tab2;
}
if (position == 2) // if the position is 0 we are returning the First tab
{
Tab3 tab3 = new Tab3();
return tab3;
}
return null;
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return NumbOfTabs;
}
}
You need to make nombre_hotel field public instead of private, then use:
hotel_nombre.setText((Detalle_Hotel)getActivity().nombre_hotel));
val intent = Intent(activity, VoiceCommandServiceActivity::class.java)
intent.putExtra(SELECT_SERVICES, mServiceName as Serializable)
startActivity(activity, intent)
intent?.let {
it.extras?.let { extras ->
extras?.let { bundle ->
if (bundle.containsKey(SELECT_SERVICES)) {
val service = extras.getSerializable(SELECT_SERVICES) as MutableList<AppServiceModel>
}
}
}
}
I have implemented ActionBar tabs following this guide: github. However I have a problem with the tab indicator getting stuck between the 2nd and 3rd tab. Like there is 4 tabs, but only 3 shown. Looks like this:
It's only when I slide from the 3rd tab to the 2nd. It just stays there, and if i slide left again, it goes to the 2nd tab. So it basically just feels like there is 4 tabs.
My FragmentPagerAdapter Class looks like this
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private static Context context;
private String tabTitles[];
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
Resources resources = context.getResources();
tabTitles = new String[] { resources.getString(R.string.recent),
resources.getString(R.string.popular),
resources.getString(R.string.my) };
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new RecentFragment();
case 1:
return new PopularFragment();
case 2:
return new MyFragment();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public static class RecentFragment extends Fragment implements OnItemClickListener {
ListView listView;
List<RowItem> rowItems;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.custom_list, container, false);
String[] titles = { "titleA", "titleB", "titleC" };
String[] descriptions = { "a", "b", "c" };
Integer[] images = { R.drawable.christoffer, R.drawable.frede, R.drawable.sofie };
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
listView = (ListView) rootView.findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(context,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return rootView;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
}
public static class PopularFragment extends Fragment {
}
public static class MyFragment extends Fragment {
}
}
My SlidingTabLayout and SlidingTabStrip classes looks excatly like the one in the guide. And in the MainActivity I have added this code to implement the tabs:
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(
getSupportFragmentManager(), MainActivity.this));
// Give the SlidingTabLayout the ViewPager
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
// Center the tabs in the layout
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
// Customize tab color
slidingTabLayout
.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return Color.RED;
}
});
Please load fragment XML into all tabs through java file and it will solve..
I have a navigation drawer in which there is a ViewPager that extends a Fragment. When i click the item of drawer i open the viewpager in which there are three fragments. It works perfectly. but if i click again the same drawer item to open the viewpager another one time, the viewpager is empty.. I can see the tabs but not the fragments in there. This is the Viewpager:
public class ViewPagerManager extends Fragment {
public static ViewPagerManager instance = null;
Toolbar toolbar;
public static PagerSlidingTabStrip tabs;
public MyPagerAdapter adapter;
public ViewPager pager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_tabbed, container, false);
instance = this;
adapter = new MyPagerAdapter(getFragmentManager());
pager = (ViewPager)view.findViewById(R.id.pager);
tabs = (PagerSlidingTabStrip)view.findViewById(R.id.tabs);
pager.setAdapter(adapter);
tabs.setViewPager(pager);
pager.setOffscreenPageLimit(3);
adapter.notifyDataSetChanged();
pager.invalidate();
return view;
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "One", "Two", "Three" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// Top Rated fragment activity
return new FragmentOne();
case 1:
// Games fragment activity
return new FragmentTwo();
case 2:
// Games fragment activity
return new FragmentThree();
}
return null;
}
}
}
Is it normal? How can i solve? If could help i'm using PagerSlidingTabStrip library.
Use this code
adapter = new MyPagerAdapter(getChildFragmentManager());
instead of
adapter = new MyPagerAdapter(getFragmentManager());
Try to redraw the last selected page when you return to the fragment. I think the viewpager is not cached and you need to reselect the last item. You can override the onresume method.