Tab Layout in a Fragment - android

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

Related

How to do tabs with icons

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

Android studio Fragment ViewPager for sliding drawable

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

How can i add different data to 3 fragment in 3 tabs?

i want to know that how can i add some different data like an array to each fragments that are in TabLayout?
i mean that fragment1 show array1, fragment2 show array2 and fragment3 show array3
the below is my code for Tablayout:
public class PoshtibaniActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poshtibani);
toolbar = (Toolbar) findViewById(R.id.app_bar_empty);
//setSupportActionBar(toolbar);
toolbar.setTitle("پشتیبانی");
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mPager = (ViewPager) findViewById(R.id.pager);
setupViewPager(mPager);
mPager.setCurrentItem(2);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "پیامهای دریافتی");
adapter.addFragment(new FragmentTwo(), "پیامهای ارسالی");
adapter.addFragment(new FragmentThree(), "ارسال پیام");
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);
}
}
you should use SetArguement for each of your fragments.
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "پیامهای دریافتی",MY_ARRAY_1,MY_SEC_ARRAY_1);
adapter.addFragment(new FragmentTwo(), "پیامهای ارسالی",MY_ARRAY_2,MY_SEC_ARRAY_2);
adapter.addFragment(new FragmentThree(), "ارسال پیام",MY_ARRAY_3,MY_SEC_ARRAY_3);
viewPager.setAdapter(adapter);
}
then in your adapter :
public void addFragment(Fragment fragment, String title,Object data,ArrayList<MY_DATA_MODEL> array2) {
Bundle mBundle=new Bundle();
mBundle.put("dataArray",data);
mBundle.put("dataArray2",array2);
fragment.setArgument(mBundle);
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
Now in each of your fragments, you should override setArguments and get your data from bundle there.
Don't forget to make your data model parcelable if you want to pass them in bundle and intents.
view pager file
public class SamplePagerAdapter extends FragmentPagerAdapter {
String[] title = {
"stringA",
"stringB",
"stringC",
}
public SamplePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */
if (position == 0) {
return FragmentA.newInstance(title);
}
else if(position==1)
{
return new FragmentB.newInstance(title);
}
else { return FragmentC.newInstance(title);}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
Fragment file
public class FragmentA extends Fragment {
public static FragmentA newInstance(String [] s) {
FragmentA A = new FragmentA();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
A.setArgumentas(args);
return A;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}
public class FragmentB extends Fragment {
public static FragmentB newInstance(String [] s) {
FragmentB B = new FragmentB();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
B.setArgumentas(args);
return B;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}
public class FragmentC extends Fragment {
public static FragmentC newInstance(String [] s) {
FragmentC C = new FragmentC();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
C.setArgumentas(args);
return C;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}

Issue in implementing Tabs with viewpager

Following the tutorial from here
I have implemented the same in activity It is working perfectly but when I tried adding tabs inside a fragment,swiping tabs is not showing fragment's view.
public class DetailFragment extends android.support.v4.app.Fragment implements LoaderManager.LoaderCallbacks<Cursor>,View.OnClickListener {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail_start, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
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);
}
}
} // Update - code formatting
Please suggest.
To implement the View pager within a fragment use getChildFragmentManager() instead of getFragmentManager().
So while Initializing the ViewPagerAdapter for ViewPager use "getChildFragmentManager()".
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

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