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);
}
}
Related
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
I'm trying to do an activity with tabs and pagerview
I found this code in a blog but it does not come like making the tabs have icons instead of text ... I put the code:
ViewPagerAdapter.java
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);
}
}
Tablayout_in_Android.java
public class Tablayout_in_Android extends Fragment {
public Tablayout_in_Android() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tablayout_in__android, container, false);
}
}
Method onCreate main class
viewPager = (ViewPager) findViewById(R.id.viewpager);
addTabs(viewPager);
tabLayout = (TabLayout) findViewById(R.id.pestaƱas);
tabLayout.setupWithViewPager(viewPager);
}
private void addTabs(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Tablayout_in_Android(), "ONE");
adapter.addFrag(new Tablayout_in_Android(), "TWO");
adapter.addFrag(new Tablayout_in_Android(), "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);
}
}
how can i change text for icons? Thanks
Try this:
tabLayout.setupWithViewPager(mViewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.your_icon);
}
To expand on Jonathan's answer, you can also have a list of icons to show in the tabs. Ensure the icons match the number of tabs you have.
tabLayout.setupWithViewPager(mViewPager);
int[] tabIcons = {
R.drawable.ic_icon_1,
R.drawable.ic_icon_2,
R.drawable.ic_icon_3
};
for(int i=0; i<tabLayout.getTabCount(); i++){
if(tabLayout.getTabAt(i) != null){
tabLayout.getTabAt(i).setIcon(tabIcons[i]);
}
}
Also, you may want to have the icon and tab label to be in a horizontal orientation, the use app:tabInlineLabel="true" in the TabLayout's xml layout
I have implemented a ViewPager in order to swipe between two Fragments like such:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.statistics_general_vp, container, false);
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mStatisticsPagerAdapter = new StatisticsPagerAdapter(getActivity().getSupportFragmentManager());
mViewPager.setAdapter(mStatisticsPagerAdapter);
return view;
}
public class StatisticsPagerAdapter extends FragmentStatePagerAdapter {
public StatisticsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return new GeneralStatistics();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
But for now my two slides display the same Fragment.
How may I have slide 1 display Fragment 1 and slide 2 Fragment 2?
have u created fragment1 and fragment 2 seperately?? if yes then do this - in activity holding these two fragments.
private void createViewPager(ViewPager viewPager) {
Log.d("activity", "viewpager");
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Fragment1(), "");
adapter.addFrag(new Fragment2(), "");
viewPager.setAdapter(adapter);
}
create fragment like this -
public class Fragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.transaction_layout, container, false);
return v;
}
}
create viewpager like this -
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);
}
}
or if you want to make tab with icons use this method -
private void createTabIcons() {
Log.d("activity", "createtabicons");
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item2, null);
tabOne.setText("QR Scan");
tabOne.setTypeface(MRR);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image1, 0, 0);
tabOne.setCompoundDrawablePadding(5);
tabLayout.getTabAt(0).setCustomView(tabOne);
tabLayout.getTabAt(0).getCustomView().setSelected(true);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item2, null);
tabTwo.setText("Mobile No.");
tabTwo.setTypeface(MRR);
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image3, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
tabTwo.setCompoundDrawablePadding(5);
}
Hello i show you what i doing, i got 2 fragments in mainActivity
private void addFragments() {
List<Fragment> fragments = new ArrayList<>();
chatFragment = new ChatFragment();
remoteFragment = new RemoteFragment();
fragments.add(remoteFragment);
fragments.add(chatFragment);
MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(TAB_REMOTE_POSITION)
.setIcon(getDrawable(R.drawable.ic_settings_remote_white_24dp));
tabLayout.getTabAt(TAB_SOCIAL_POSITION)
.setIcon(getDrawable(R.drawable.ic_group_white_24dp));
}
Here is my adapter:
class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
and on OnCreate u should
addFragments();
also i bind the views with ButterKnife
#Bind(R.id.view_pager)
ViewPager viewPager;
#Bind(R.id.tab_layout)
TabLayout tabLayout;
but u can static implement this with android
And remember that u have listeners:
private void pageChangeListener() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
View view = getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService
(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
});
}
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());