Android studio Fragment ViewPager for sliding drawable - android

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()

Related

ViewPager not showing content onResume

I'm using FragmentStatePagerAdapter for TabLayout with a ViewPager going to show a RecyclerView and when I click on a item of my list and i click back to go to previous page It's showing me a blank page with TabLayout and no RecyclerView.
Here is my Adapter code:
public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public MyViewPagerAdapter(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 here is my fragment handling ViewPager and TabLayout:
public class InternetFragment extends Fragment {
private View rootView;
private TabLayout tabLayout;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_internet_package, container, false);
initUI();
return rootView;
}
private void initUI() {
ViewPager viewPager = rootView.findViewById(R.id.viewpager);
tabLayout = rootView.findViewById(R.id.tabs);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TextView txtTabTitle = (TextView) LayoutInflater.from(GC.getInstance().getAppContext()).inflate(R.layout.custom_tablayout_cell, null);
tabLayout.getTabAt(i).setCustomView(txtTabTitle);
txtTabTitle.setTypeface(GC.font);
}
}
private void setupViewPager(ViewPager viewPager) {
String IMSI = GC.getInstance().getSimNumber();
MyViewPagerAdapter pageAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager());
if (IMSI.startsWith(ClientConstant.IMSI_RIGHTEL) || IMSI.startsWith(ClientConstant.IMSI_TALIA)) {
tabLayout.setVisibility(View.GONE);
pageAdapter.addFragment(new InternetOnlinePackageFragment(), "");
} else {
pageAdapter.addFragment(new InternetUSSDPackageFragment(), getString(R.string.package_ussd));
pageAdapter.addFragment(new InternetOnlinePackageFragment(), getString(R.string.package_online));
}
viewPager.setAdapter(pageAdapter);
if (!IMSI.startsWith(ClientConstant.IMSI_RIGHTEL) || !IMSI.startsWith(ClientConstant.IMSI_TALIA))
viewPager.setCurrentItem(1);
}
}

Set Up Tab Icons Error

public class AllFragments extends Fragment {
private int[] tabIcons = {
R.mipmap.ic_home,
R.mipmap.search_icon,
// R.mipmap.ic_msg,
};
private TabLayout tabLayout;
public static int int_items = 2 ;
private ViewPager viewPager;
public AllFragments() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragment_all_fragments, container, false);
tabLayout = (TabLayout) x.findViewById(R.id.tabLayout);
viewPager = (ViewPager) x.findViewById(R.id.viewPG);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
return x;
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}
private void setupViewPager(ViewPager viewPager) {
AllFragments.ViewPagerAdapter adapter = new AllFragments.ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new HomeF(), "");
adapter.addFragment(new SearchFragment(), "");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(android.support.v4.app.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);
}}}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.support.design.widget.TabLayout$Tab
android.support.design.widget.TabLayout$Tab.setIcon(int)' on a null
object reference at
com.example.dell.AllFragments.setupTabIcons(AllFragments.java:47)
I am trying to create a simple tabLayout with icons but the application keeps crushing and Idk why,there are two errors one is this and the second one is in my main class
onStart() {
super.onStart();
auth.addAuthStateListener(authListener);}
Can sb tell me where my error is?Thanks in Advance!
You have forgotten to call setupViewPager(viewPager); on onCreateView

handling fragment after manually phone lock in View pager

Actually, i am implementing a TabLayout, so in main MainActivity, I load the TabFragment and in the tabFragment(contain two fragments) I used the ViewPager.
So the problem is when I am on fragment two in TabLayout and the after the screen goes off it will move to the Fragment one(Default one). So how can I tackle this one
Here is TabFragment Code:
public class TabFragment extends Fragment {
public TabFragment() {
// Required empty public constructor
}
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2 ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View x=inflater.inflate(R.layout.content_main_menu,null);
tabLayout=(TabLayout) x.findViewById(R.id.tabs);
viewPager =(ViewPager) x.findViewById(R.id.viewpager);
setupViewPager(viewPager);
//viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
viewPager.setOffscreenPageLimit(1);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
private void setupViewPager(ViewPager viewPager) {
MyAdapter adapter = new MyAdapter(getChildFragmentManager());
adapter.addFragment(new HomeFragment(), "Fragment One");
adapter.addFragment(new Offers(), "fragment Two");
viewPager.setAdapter(adapter);
}
class MyAdapter extends FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public MyAdapter(FragmentManager fm) {
super(fm);
}
#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);
}
}
}
Use FragmentStatePagerAdapter instead of FragmentPagerAdapter. read documentation here. FragmentStatePagerAdapter only stores the savedInstanceState of fragments, and destroys all the fragments when they lose focus.

Tab Layout in a Fragment

i am trying to create a tab layout in a fragment but i am facing an error in creating the ViewPagerAdapter as it cannot resolve method getFragmentManger()
i tried using getActivity but the problem still exist
that is the code for the fragment
public class HomeFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
public static final String TAG = "HF";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
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<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);
}
}
}

View is not inflated on returning to the tab fragment

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());

Categories

Resources