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");
}
Related
I have imported the tabbed activity from android studio.
Under static placeholder fragment i have oncreate view and i have switched cases for each tab and given a seperate layout for each. I want to work in separate java class for each page in tab view. Can i seperate each cases to be in seperate class?
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int tabNumber= getArguments().getInt(ARG_SECTION_NUMBER);
switch (tabNumber) {
case 1: {
View rootView = inflater.inflate(R.layout.tab1, container, false);
return rootView;
}
case 2: {
View rootView = inflater.inflate(R.layout.tab2, container, false);
return rootView;
}
case 3: {
View rootView = inflater.inflate(R.layout.tab3, container, false);
return rootView;
}
private class DesignDemoPagerAdapter extends FragmentStatePagerAdapter {
public DesignDemoPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
//
if (position == 0) {
BaseFragment f = new SalesBlankTab();
salesStack.get(AppConstant.SALES_REPORT_TAB).push(f);
return f;
} else if (position == 1) {
BaseFragment f = new MemberBlankTab();
salesStack.get(AppConstant.MEMBER_ORDER).push(f);
return f;
} else {
BaseFragment f = new StockBlankTab();//MemberBlankTab,StockBlankTab
salesStack.get(AppConstant.STOCK_MANAGEMENT).push(f);
return f;
}
}
#Override
public int getCount() {
return AppConstant.TAB_COUNT;
}
}
So rishav there is a geItem method which returns variables as super.( ..... )
just return fragment at this place.
#Override
public Fragment getItem(int i)
{ Fragment fragment;
switch(i){
case 0: fragment = new MyFragment1();
break;
case 1: fragment = new MyFragment2();
break;
case 2:
fragment = new MyFragment3(); break;
default:
throw new IllegalArgumentException("Invalid section number"); }
return fragment; }
You can do something like this (from Android Hive):
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#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);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(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);
}
}
}
for more detail, you can try on this link: http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
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 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);
}
}
}
I have two fragments (Same class- PlayerFragment) and I set the ViewPager with this method:
private void setupViewPager(ViewPager viewPager) {
FragmentManager fManager = getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fManager);
Bundle args1 = new Bundle();
args1.putInt("Id",1);
PlayerFragment pf1 = new PlayerFragment();
pf1.setArguments(args1);
adapter.addFrag(pf1, "Players 1");
Bundle args2 = new Bundle();
args2.putInt("Id",2);
PlayerFragment pf2 = new PlayerFragment();
pf2.setArguments(args2);
adapter.addFrag(pf2, "Players 2");
viewPager.setAdapter(adapter);
}
I have two tabs but only the first tab shows me content, the other tab is empty.
And the first tabs shows me the contentent of the second one.
static 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);
}
}
In the activity where I use the fragments I have this code:
final ViewPager viewPager = (ViewPager) findViewById(R.id.vpTeam);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabsTeam);
tabLayout.setupWithViewPager(viewPager);
Thanks!
Edit: This is the PlayerFragment
public class PlayerFragment extends Fragment {
private ProgressDialog progressDialog;
private List<Player> myPlayers = new ArrayList<>();
private int myTeamId = 14;
public PlayerFragment(){
}
public static PlayerFragment newInstance(int someInt) {
PlayerFragment myFragment = new PlayerFragment();
Bundle args = new Bundle();
args.putInt("TeamId", someInt);
myFragment.setArguments(args);
return myFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Bundle args = getArguments();
if(args!=null) {
myTeamId = args.getInt("TeamId");
}
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_player, container, false);
new BackgroundAsyncTask().execute();
return view;
}
}
Can you post the code of PlayerFragment? I'm pretty sure that the error is that you have the variables in that class as static. Also try to use the "new instance()" way for creating fragments --> Best practice for instantiating a new Android 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());