I am trying to replace the fragment views in my tabbed swipe view with another fragment but it does not replace or add the fragment, instead it only adds the menu of the fragment in all the tabbed activity.
I want when in a particular tab activity in my swipeview, I can replace the content(fragmentA) with another fragment(FragmentBoy) which can override that particular menu of that particular tabbed activity. Also could also work when replacing content(fragmentB) with another fragment(FragmentGirl).
Please I stucked, I need a help on this-
public class Main2Activity extends AppCompatActivity {
private ViewPager mViewPager;
private int[] tabIcons = {
R.drawable.ic_grand,
R.drawable.ic_event,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
setupTabIcons();
}
private void setupTabIcons() {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}
private void setupViewPager(ViewPager mViewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentA(), "ONE");
adapter.addFrag(new FragmentB(), "TWO");
mViewPager.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 null;
}
}
public static class FragmentA extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.grand,container,false);
Button tb=(Button)view.findViewById(R.id.btn_c);
tb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction().add(R.id.swapfrag, new BOYFragment());
transaction.commit();
}
});
return view;
}
}
Related
How do i add icons to the side of my page title in tab layout? This is a new created tab activity, so the code is already generated by android studio. I just don't know how to add icons beside my title. Thanks
Here is the code of my main activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Schedule";
case 1:
return "News";
case 2:
return "Games";
case 3:
return "Standings";
}
return null;
You can add icons to tab layout by:
tabLayout.addTab(tabLayout.newTab().setText("Tab1").setIcon(R.mipmap.ic_launcher));
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
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);
}
}
}
I have main_activity with fragments. Main Activity contains swiperefreshlayout overlaying viewerpager. After swiping new data is downloaded, now I want to refresh it on every fragment. How can I achieve that?
My code:
public class MainActivity extends AppCompatActivity {
object simple_object;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
SwipeRefreshLayout mSwipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout1);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Refresh items
refreshItems(); //refreshing simple object
}
});
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new main_tab1(simple_object), "Tab1");
adapter.addFragment(new main_tab2(simple_object), "Tab2");
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 addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
I cant create fragment with vierpager. I'll use this on my sliding menu to replace activity. I don't wnat to use extends FragmentActivity because it will not work when I use in
getFragmentManager().beginTransaction().replace(R.id.container, home).commit();
public class Home extends Fragment{
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home, container, false);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new Recent(), "RECENT");
adapter.addFragment(new Shake(), "SHAKE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<android.support.v4.app.Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
My error is the getChildFragmentManager()
I have one activity named rootActivity , in which i have a rootContainer in which i replace the fragments.
Now, the first fragment that i replace is DashboardFragment.
I now click on the cross to open a new fragment like this...
Now when i press the back button the content of dashboard fragment is lost.
Can someone help me
Here's a video to show what's really happening.
https://youtu.be/NobqYUCEZKY
Code for the DashboardFragment(contains the fragments inside tabs)
public class DashboardFragment extends Fragment {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
/**
*
* #param config : For setting up stuff by the given config
* #return
*/
public static DashboardFragment newInstance(Bundle config) {
DashboardFragment fragment = new DashboardFragment();
fragment.setArguments(config);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_dashboard, container, false);
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((RootActivity)getActivity()).setSupportActionBar(toolbar);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
return rootView;
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(android.R.drawable.btn_star_big_off);
tabLayout.getTabAt(1).setIcon(android.R.drawable.btn_star_big_on);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new TrackeesFragment(), "Trackee");
adapter.addFragment(new TrackersFragment(), "Trackers");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
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 addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
try by changing the following line in
setupViewPager(ViewPager viewPager) {}
Change
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
To
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
I have follow this link to implement tablayout Android Material Design working with Tabs
Now what I did I use fragment instead of Activity for tablayout.
Below is code :-
public class HopitalTabLayoutFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
public HopitalTabLayoutFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_hopital_tab_layout, container, false);
viewPager = (ViewPager)view. findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout)view. findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
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 addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
and add this fragment from Activity :-
public class HomeActivityDrawer extends AppCompatActivity {
private Toolbar toolbar;
private NavigationView navigation_view_left, navigation_view_right;
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
header = (RelativeLayout) findViewById(R.id.header);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fragment = new HopitalTabLayoutFragment();
}
}
These are my three fragments which are used in tab layout:-
public class TwoFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText("DONE");
return view;
}
}
Similarly One and Three.
Now the problem is that at first time all the three fragments are loaded and onCreateView method runs but when I open tablayout again only fragment One initiate not the second one. What's going wrong in this code? Please help!!
Import FragmentStatePagerAdapter instead FragmentPagerAdapter.
import android.support.v4.app.FragmentPagerAdapter;
to
import android.support.v4.app.FragmentStatePagerAdapter;
and then extend from FragmentStatePagerAdapter:
class ViewPagerAdapter extends FragmentPagerAdapter
to
class ViewPagerAdapter extends FragmentStatePagerAdapter