My view pager's Adapter is like:
public Pager(FragmentManager fm, int tabCount) {
super(fm);
this.tabCount = tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new frag1();
case 1:
return new frag2();
case 2:
return new frag1();
case 3:
return new frag2();
case 4:
return new frag1();
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
}
how i set the tablayout and viewpager in java class:
for (int i = 0; i < 5; i++) {
tabLayout.addTab(tabLayout.newTab());
}
Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.beginFakeDrag();
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
I am getting weird error look like this:
W/FragmentManager: moveToState: Fragment state for frag2{2428405 #1 id=0x7f0c006a} not updated inline; expected state 3 found 2
so how can i fix this issue?
I am getting answers from stack but it doesn't solve my issue,
so anyone have any knowledge about this, kindly help me!
Replace this line viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); with tabLayout.setTabsFromPagerAdapter(adapter);
Try connecting the tabLayout with the viewPager with tabLayout.setupWithViewPager(viewPager) instead of adding the tabs manually in the loop.
available_bus_fragment.xml
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:background="#d4d4d4"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e8e8e8"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
ViewPagerAdapter.java is adapter for ViewPager.
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return String.valueOf(position);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment tab1 = new YourFragment1();
return tab1;
case 1:
Fragment tab2 = new YourFragment1();
return tab2;
case 2:
Fragment tab3 = new YourFragment1();
return tab3;
}
return null;
}}
This is your class where you set adapter of Viewpager.
public class MyFragment extends Fragment {
View rootView;
ViewPagerAdapter adapter;
TabLayout tabLayout;
ViewPager viewPager;
private int[] tabIcons = {
R.drawable.tabclock,
R.drawable.clock,
R.drawable.tab_rupee
};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.available_bus_fragment, null);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
adapter = new ViewPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabsFromPagerAdapter(adapter);
setupTabIcons();
return rootView;
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]).setTag("Departure").setText("Departure");
tabLayout.getTabAt(1).setIcon(tabIcons[1]).setTag("Duration").setText("Duration");
tabLayout.getTabAt(2).setIcon(tabIcons[2]).setTag("Price").setText("Price");
} }
Related
this is my pager adapter, i want to load 2 fragment by viewpager and tablayout but it displaying white screen only.
public class HomeFragmentAdapter extends FragmentPagerAdapter {
RelativeLayout headerLogo;
public HomeFragmentAdapter(FragmentManager fm, RelativeLayout headerLogo) {
super(fm);
this.headerLogo = headerLogo;
}
#Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
return new DoubleColumnMintsList();
case 1:
return new MyCircleMain();
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
}
this is inside activity:
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
HomeFragmentAdapter adapter = new HomeFragmentAdapter(getFragmentManager(), headerLogo);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
viewPager.setOffscreenPageLimit(2);
viewPager.setAdapter(adapter);
setupTabs(tabLayout);
viewPager.setCurrentItem(0)
I was also facing same issue, it solved try to use this line:
HomeFragmentAdapter adapter = new HomeFragmentAdapter(getChildFragmentManager(), headerLogo);
I am getting this error .Although I have gone through various link but nothing helped.
Already using getChildFragmentManager() as mentioned in various links.
This is the code:
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(this);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setOffscreenPageLimit(2);
class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0: return new Fragment1();
case 1: return new UserFragment();
case 2: return new Fragment2();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
i have fragment which have three tabs in, after i came back to HomeFragment from another activity it's same i have duplicate layout
please inform me if I'm doing something wrong ?
HomeFragment.class
public class HomeFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home,container,false);
// [Start Tabs]
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar_fragment);
//toolbar.setNavigationIcon(android.R.drawable.ic_menu_preferences);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) rootView.findViewById(R.id.fragmetn_viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs_fragment);
tabLayout.setupWithViewPager(viewPager);
//[*End Tab ]
return rootView;
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.notifyDataSetChanged();
adapter.addFragment(new Tab1Fragment() , "Tab1");
adapter.addFragment(new Tab2Fragment() , "Tab2");
adapter.addFragment(new Tab3Fragment() , "Tab3");
viewPager.setAdapter(adapter);
}
}
Try something as:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabGravity="fill"
app:tabMode="scrollable"
android:background="#color/material_blue_grey_800"
app:tabIndicatorColor="#color/orange"
app:tabSelectedTextColor="#color/orange"
app:tabTextColor="#color/white"
android:layout_width="match_parent"
android:singleLine="true"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
And:
public class TabFragment extends Fragment {
public TabLayout tabLayout;
public ViewPager viewPager;
private static int currentItem = 0;
MyAdapter adapter;
public static TabFragment newInstance(int someInt) {
TabFragment frag = new TabFragment();
Bundle args = new Bundle();
args.putInt("tab", someInt);
frag.setArguments(args);
return frag;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentItem", viewPager.getCurrentItem());
}
public static TabFragment newInstance() {
TabFragment frag = new TabFragment();
return frag;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter=new MyAdapter(getChildFragmentManager());
currentItem=getArguments() != null ? getArguments().getInt("tab") : 0;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Adapter for the View Pager
*/
if (adapter==null) adapter=new MyAdapter(getChildFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem( currentItem);
/**
* Now , this is a workaround ,
* The setupWithViewPager doesn't work without the Runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new Tab1Fragment();
case 1 : return new Tab2Fragment();
case 2 : return new Tab3Fragment();
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Tab1";
case 1 :
return "Tab2";
case 2 :
return "Tab3";
}
return null;
}
}
hey guys i implemented a tab layout(an action bar with tabs) , basically it is made of using pagerAdapter and FragmentStatePagerAdapter below is my code :
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
case 3:
TabFragment4 tab4 = new TabFragment4();
return tab4;
case 4:
TabFragment5 tab5 = new TabFragment5();
return tab5;
default:
return null;
}
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
super.destroyItem(container, position, object);
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
#Override
public int getCount() {
return mNumOfTabs;
}}
my mainActivty:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab4"));
tabLayout.addTab(tabLayout.newTab().setText("Tab5"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final 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) {
}
});
now i want to remove the tabs(fragments) and for that am just removing them from here
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab4"));
if (condition == true){ // do nothing which means don't add the fragment }
else {
tabLayout.addTab(tabLayout.newTab().setText("Tab5")); }
and is working but not as it supposed to , even after removing the the tab from here tabLayout.addTab(tabLayout.newTab().setText("Tab5")); the fragments are still there and actively performing their task , i want to shut them completely , am new in android so have no idea about this that its a good approach or not guys is it enough or not please let me know any guidance will be so helpful for me
i use this tutorial for creating tab.
and now i have a recyclerview in each fragment and i want to notify them from mainactivity. how to access the adapter in fragment?
i use this for calling refreshData() method in fragment:
TabFragment1 tabFragment = (TabFragment1) getSupportFragmentManager().findFragmentByTag(TabFragment1.class.getSimpleName());
if (tabFragment != null) {
tabFragment.refreshData();
}
but that return null.
mainActivity:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
final 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) {
}
});
PagerAdapter:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
case 3:
TabFragment4 tab4 = new TabFragment4();
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
One of myTabFragment:
public CardAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView v = (RecyclerView) inflater.inflate(R.layout.tab_fragment_1, container, false);
SetupRecycleView(v);
return v;
}
public void refreshData() {
adapter.notifyDataSetChanged();
}
void SetupRecycleView(RecyclerView recList) {
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(G.context);
llm.setOrientation(LinearLayoutManager.VERTICAL);
//recList.setLayoutManager(new GridLayoutManager(G.context, 2));
recList.setLayoutManager(llm);
List<StructGhaza> ghazas = StructGhaza.getAllGhaza("2");
adapter = new CardAdapter(ghazas);
recList.setAdapter(adapter);
}
The CardAdapter:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.GhazaViewHolder> {
private List<StructGhaza> ghazas;
public CardAdapter(List<StructGhaza> ghazaList) {
this.ghazas = ghazaList;
}
#Override
public int getItemCount() {
return ghazas.size();
}
#Override
public void onBindViewHolder(GhazaViewHolder ghazaViewHolder, int position) {
StructGhaza ghaza = ghazas.get(position);
ghazaViewHolder.vTitle.setText(ghaza.Name);
//----Load Image
Glide.with(G.context)
.load(ghaza.Aks)
.placeholder(R.drawable.loading_spinner)
.crossFade()
.into(ghazaViewHolder.itemImg);
//---------------
ghazaViewHolder.Rate.setRating(ghaza.Star);
ghazaViewHolder.sampleLayout.setHoverView(ghazaViewHolder.hover);
ghazaViewHolder.sampleLayout.addChildAppearAnimator(ghazaViewHolder.hover, R.id.tvHover, Techniques.FlipInX);
ghazaViewHolder.sampleLayout.addChildDisappearAnimator(ghazaViewHolder.hover, R.id.tvHover, Techniques.FlipOutX);
}
#Override
public GhazaViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.fragment_carditem, viewGroup, false);
return new GhazaViewHolder(itemView);
}
public static class GhazaViewHolder extends RecyclerView.ViewHolder {
protected TextView vTitle;
protected BlurLayout sampleLayout;
protected View hover;
protected ImageView itemImg;
protected RatingBar Rate;
public GhazaViewHolder(View v) {
super(v);
sampleLayout = (BlurLayout) v.findViewById(R.id.sample);
itemImg = (ImageView) v.findViewById(R.id.item_img);
hover = LayoutInflater.from(G.context).inflate(R.layout.fragment_hover, null);
vTitle = (TextView) v.findViewById(R.id.titleTv);
Rate = (RatingBar) v.findViewById(R.id.rating);
}
}
}
EDIT:
After many test, the previous answer work with Android < 21, but for Android > 21 it's little bit weird, it doesn't seems to work in the same way.
Finally, I solved it like that, and it works everywhere:
In your adapter:
HashMap<Integer, Fragment> mPageReferenceMap = new HashMap<>();
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
mPageReferenceMap.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
mPageReferenceMap.remove(position);
}
public Fragment getFragment(int key) {
return mPageReferenceMap.get(key);
}
And you call getFragment where you want in your activity like that:
int index = viewPager.getCurrentItem();
ViewPagerAdapter adapter = ((ViewPagerAdapter)viewPager.getAdapter());
final YourFragment tabFragment = (YourFragment) adapter.getFragment(index);
PREVIOUS ANSWER :
Warning : it doesn't really work with Android > 21
I had the same problem. Finally, i did'nt use id or tag.
Instead of using:
CustomFragment customFragment = (CustomFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_custo_tag));
use this:
CustomFragment customFragment = (CustomFragment) getSupportFragmentManager().getFragments().get(0);
to get instance of CustomFragment.
I have put get(0) as your position of CustomFragment instance is at zero.
I found this issue here:
Set and get using tag a fragment in android
This is late but I hope this will be helpful for someone. To find fragment by tag we can do this:
String tag=getSupportFragmentManager().getFragments().get(index).getTag();
SecondFragment f = (SecondFragment) getSupportFragmentManager().findFragmentByTag(tag);