If I ave an activity with a viewPAger on it and two sets of data that I want to display, is it best to have one fragment and load information into textViews based on position passed from activity or better to create more than one fragment and load the one needed?
Any help is greatly appreciated.
public class testFragment extends Fragment {
private static final String ARG_PAGE = "pageNumber";
private static final int DEFAULT_INT = 0;
TextView header, sd1;
int h1, page = 0;
public testFragment (){
}
public testFragment newInstance(int pageNumber) {
testFragment fragment = new testFragment ();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page = getArguments().getInt(ARG_PAGE);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_high_scores_page, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
header = (TextView) getActivity().findViewById(R.id.modes_header);
sd1 = (TextView) getActivity().findViewById(R.id.stat_det_1);
switch (page) {
case 1:
header.setText(R.string.easy_mode);
h1 = DEFAULT_INT;
sh1 = DEFAULT_INT;
sd1.setText(String.valueOf(h1));
break;
case 2:
header.setText(R.string.medium_mode);
h1 = DEFAULT_INT + 1;
sh1 = DEFAULT_INT + 1;
sd1.setText(String.valueOf(h1));
break;
default:
header.setText(R.string.action_restart);
h1 = DEFAULT_INT;
sh1 = DEFAULT_INT;
sd1.setText(String.valueOf(h1));
break;
}
}
public void setTextViewInfo(){
sd1.setText(String.valueOf(h1));
}
}
Or would it be better to send the information from my activity and load one or multiple fragments instead?
public class testActivity extends FragmentActivity {
private static final int NUM_PAGES = 8;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testActivity);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.high_scores_pager);
mPagerAdapter = new testActivity (getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class testActivityAdapter extends FragmentStatePagerAdapter {
public testActivityAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
testFragment tf1 = new testFragment ();
notifyDataSetChanged();
return easy.newInstance(position);
case 1:
testFragment2 tf2 = new testFragment ();
notifyDataSetChanged();
return medium.newInstance(position);
default:
testFragment d = new testFragment ();
return d.newInstance();
}
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
edit:
In my adapter, I just tried
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return HighScoresEasyFragment.newInstance(position);
case 1:
return HighScoresMediumFragment.newInstance(position);
case 2:
return HighScoresExpertFragment.newInstance(position);
case 3:
return HighScoresBrainFartFragment.newInstance(position);
case 4:
return HighScoresClassicFragment.newInstance(position);
case 5:
return HighScoresPuzzleFragment.newInstance(position);
case 6:
return HighScoresPerfectFragment.newInstance(position);
case 7:
return HighScoresPowerFragment.newInstance(position);
default:
return HighScoresEasyFragment.newInstance(position);
}
ugh, but it didn't load anything... my constructor is now...:
public static HighScoresEasyFragment newInstance(int pageNumber) {
HighScoresEasyFragment fragment = new HighScoresEasyFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
It loaded the first page's header but no data and it didn't load anything for second to 8th fragments...
You should call notifyDataChanged() from your activity using your adapter reference. Basically notifyDataChanged just tells all your fragments in your adapter that data has changed and they should refresh themselves. Remove notifyDataChanged from your getItem() method and put it when you change data in your activity.
Related
my recyclerView Adapter - Activity-1
here i'm sending arguments to ViewPager Fragmnet using setArguments()
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString("projectName", jobCardLists.get(position).getProject());
// set MyFragment Arguments
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.setArguments(bundle);
Intent intent = new Intent(context, JobCardViewActivity.class);
context.startActivity(intent);
});
}
Activity-2 which contains ViewPager - Tab layout
public class JobCardViewActivity extends AppCompatActivity {
private ViewPager mViewPager;
private Toolbar mToolbar;
private JobCardViewVPAdapter mViewPagerAdapter;
private TabLayout mTabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_card_view);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//set TabLayout
setViewPager();
}
private void setViewPager() {
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPagerAdapter = new JobCardViewVPAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
mTabLayout = (TabLayout) findViewById(R.id.tab);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.getTabAt(0).setIcon(R.drawable.ic_home_white_24dp);
}
}
My viewPager Adapter
public class JobCardViewVPAdapter extends FragmentStatePagerAdapter {
private static int TAB_COUNT = 6;
public JobCardViewVPAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return DetailsFragment.newInstance();
case 1:
return WorkActivitiesFragment.newInstance();
case 2:
return DocumentsFragment.newInstance();
case 3:
return TasksFragment.newInstance();
case 4:
return WorkImagesFragment.newInstance();
case 5:
return NotesFragment.newInstance();
}
return null;
}
#Override
public int getCount() {
return TAB_COUNT;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 1:
return WorkActivitiesFragment.TITLE;
case 2:
return DocumentsFragment.TITLE;
case 3:
return TasksFragment.TITLE;
case 4:
return WorkImagesFragment.TITLE;
case 5:
return NotesFragment.TITLE;
}
return super.getPageTitle(position);
}
}
My ViewPager fragment class
here i'm getting NULL from getArguments()
public class DetailsFragment extends Fragment {
private View view;
private Activity activity;
public static final String TITLE = "Details";
private String projectName;
public DetailsFragment() {
// Required empty public constructor
}
public static DetailsFragment newInstance() {
return new DetailsFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
if (getArguments() != null) {
Bundle bundle = getArguments();
projectName = bundle.getString("projectName");
} else {
Toast.makeText(activity, "NULL", Toast.LENGTH_SHORT).show();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_details, container, false);
TextView tvProjectName, startDate, endDate, assignedBy, workCategory, status;
tvProjectName = view.findViewById(R.id.tv_project_name);
tvProjectName.setText(projectName);
return view;
}
1.You can use Single Object (singleton) for your fragment.
In your fragment use,
private static DetailsFragment ourInstance = new DetailsFragment();
public static DetailsFragment newInstance() {
return ourInstance;
}
And in your onBindViewHolder(), use
DetailsFragment detailsFragment = DetailsFragment.newInstance();
detailsFragment.setArguments(bundle);
2.Or else, pass data between the activities
In your onBindViewHolder(), use
Intent intent = new Intent(context, JobCardViewActivity.class);
intent.putString("project", jobCardLists.get(position).getProject());
context.startActivity(intent);
and do,
1.in JobCardViewActivity,get the string and save it
myString=getIntent().getString("project"); //public String myString;
2.in fragment, use that saved string
tvProjectName.setText(((JobCardViewActivity)getActivity()).myString);
I have faced this issue, maybe there is already the same issue, but I cannot find in this page. So my problem is that I have my activity, which initialize ViewPager and I have 2 fragments in this viewpager.
My Activity:
public class ShoppingDetailsActivity extends BaseActivity {
ViewPagerAdapter adapter;
#Bind(R.id.view_pager)
ViewPager viewPager;
#Bind(R.id.pager_tabs)
PagerSlidingTabStrip pagerSlidingTabStrip;
int shop_id;
int id;
int shop_sub_id;
int subId;
int shop_sub_sub_id;
int subSubId;
int shop_detail_id;
int detailId;
public static final String SHOP_ID = "shop_id";
public static final String SHOP_SUB_ID = "shop_sub_id";
public static final String SHOP_SUB_SUB_ID = "shop_sub_sub_id";
public static final String SHOP_DETAIL_ID = "shop_detail_id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityComponent().inject(this);
setContentView(R.layout.activity_shopping_details);
ButterKnife.bind(this);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
pagerSlidingTabStrip.setViewPager(viewPager);
shop_id = getIntent().getIntExtra(SHOP_ID, id);
shop_sub_id = getIntent().getIntExtra(SHOP_SUB_ID, subId);
shop_sub_sub_id = getIntent().getIntExtra(SHOP_SUB_SUB_ID, subSubId);
shop_detail_id = getIntent().getIntExtra(SHOP_DETAIL_ID, detailId);
}
}
My Adapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new InfoAdminFragment();
break;
case 1:
fragment = new ViewAdminFragment();
break;
default:
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "InfoAdminFragment";
} else if (position == 1) {
return "ViewAdminFragment";
} else {
return super.getPageTitle(position);
}
}
}
As you can see in My Activity I have a lot of IDs using getIntent() and get them successfully, but I also need somehow to pass to my both fragments in ViewPager, how can I make solve this issue?
Pass the ID's as bundle to ViewPager adapter from your activity
adapter = new ViewPagerAdapter(getSupportFragmentManager(), getIntent().getExtras());
Modify your ViewPager adapter as below
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Bundle bundle;
public ViewPagerAdapter(FragmentManager fm, Bundle bundle) {
super(fm);
this.bundle = bundle;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new InfoAdminFragment();
break;
case 1:
fragment = new ViewAdminFragment();
break;
default:
fragment = new InfoAdminFragment();
break;
}
fragment.setArguments(bundle);
return fragment;
}
//other code
}
And retrieve the bundle in your fragments
Bundle bundle = getArguments();
shopId = bundle.getInt(ShoppingDetailsActivity.SHOP_ID);
Check your code with this one:
private int shop_id;
private int shop_sub_id;
private int shop_sub_sub_id;
private int shop_detail_id;
public void setExtraData(int shopId, int shopSubId, shopSubSubId, shopDetailId) {
this.shop_id = shopId;
this.shop_sub_id = shopSubId;
this.shop_sub_sub_id = shopSubSubId;
this.shop_detail_id = shopDetailId;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new InfoAdminFragment();
break;
case 1:
fragment = new ViewAdminFragment();
break;
default:
break;
}
setBundleData(fragment);
return fragment;
}
public void setBundleData(Fragment fragment){
Bundle bundle = new Bundle();
bundle.putInt("shop_id", shop_id);
bundle.putInt("shop_sub_id", shop_sub_id);
bundle.putInt("shop_sub_sub_id", shop_sub_sub_id);
bundle.putInt("shop_detail_id", shop_detail_id);
fragment.setArguments(bundle);
}
And in your activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityComponent().inject(this);
setContentView(R.layout.activity_shopping_details);
ButterKnife.bind(this);
shop_id = getIntent().getIntExtra(SHOP_ID, id);
shop_sub_id = getIntent().getIntExtra(SHOP_SUB_ID, subId);
shop_sub_sub_id = getIntent().getIntExtra(SHOP_SUB_SUB_ID, subSubId);
shop_detail_id = getIntent().getIntExtra(SHOP_DETAIL_ID, detailId);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.setExtraData(shop_id, shop_sub_id, shop_sub_sub_id, shop_detail_id);
viewPager.setAdapter(adapter);
pagerSlidingTabStrip.setViewPager(viewPager);
}
In your fragment you can get:
getArguments().getInt(/*Key*/, /*Default value*/);
like:
getArguments().getInt("shop_id", -1);
You can achieve the same above scenario by passing intent rather than single-single values as I am doing here in setExtraData method of adapter.
I am using a Viewpager inside a fragment and the viewpager is having a listview within its fragment.So when a list item is clicked it is redirected to another fragment in the same activity. So when i come back to my previous fragment(viewpager) the viewpager is blank! why is it so
I am desperate for the solution. Any suggestion would be appreciated.
//tabs inside the fragment
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
/* #Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PageFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return "TAB " + (position + 1);
}*/
#Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
frag = new Full_Roaster_Fragment();
break;
case 1:
frag = new By_Sport_Fragment();
break;
case 2:
frag = new By_Team_Fragment();
break;
case 3:
frag = new Video_Critique_Fragment();
break;
}
return frag;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
String title=" ";
switch (position){
case 0:
title="Full Roaster";
break;
case 1:
title="By Sport";
break;
case 2:
title="By Team";
break;
case 3:
title="For Video Critique";
break;
}
return title;
}
}
//Child Fragment of Viewpager
public class Full_Roaster_Fragment extends Fragment implements AdapterView.OnItemClickListener {
private View rootView;
public static ListView Analystslist;
public static FullRoasterAdapter full_Roaster_adapter;
public static ArrayList<ics.com.ics_app.Beans.AnalystSearchBean.Teams> analysts_search_arraylist1 = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_full_roaster, container, false);
initialization();
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Full Roaster on Activity Created", Toast.LENGTH_SHORT).show();
// Populates list with our static array
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
Log.e("sizeee",String.valueOf(ApplicationGlobalClass.analysts_search_arraylist.size()));
}
private void initialization() {
Analystslist = (ListView)rootView.findViewById(R.id.Analysts_listview);
Analystslist.setOnItemClickListener(this);
}
#Override
public void onResume() {
super.onResume();
// if (ApplicationGlobalClass.flag) {
Toast.makeText(getActivity(), "full Roaster on Resume", Toast.LENGTH_SHORT).show();
full_Roaster_adapter = new
..
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ApplicationGlobalClass.user_id=ApplicationGlobalClass.analysts_search_arraylist.get(i).get(i).getId();
Log.e("userid",String.valueOf(ApplicationGlobalClass.user_id));
Log.e("userid2",String.valueOf(ApplicationGlobalClass.analysts_search_arraylist.get(i).get(i).getId()));
//here new fragment is calling from this fragment
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FlexibleSpaceWithImageWithViewPagerTab2Activity()).addToBackStack("profile1").commit();
}
You need to override onBackPressed() in the activity that's hosting the fragments like this:
public void onBackPressed() {
int = count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}
I have a weird problem.
In the main activity I use this code:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = {"Laatste Nieuws", "Uitrukken", "Voertuigen", "Contact", "Top Grossing"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = NaviContentFragment.newInstance(position);
break;
case 1:
fragment = NaviContentFragmentTwo.newInstance(position);
break;
case 2:
fragment = NaviContentFragment.newInstance(position);
break;
case 3:
fragment = NaviContentFragment.newInstance(position);
break;
case 4:
fragment = NaviContentFragment.newInstance(position);
break;
default:
break;
}
return fragment;
}
}
So when I open a tab it will open a new fragment. And when I open tab 1 labeled "Uitrukken" it should open a other fragment than the other ones. This fragment:
public class NaviContentFragmentTwo extends Fragment {
private static final String ARG_POSITION = "position";
public static NaviContentFragment newInstance(int position) {
NaviContentFragment fragment = new NaviContentFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = rootView = inflater.inflate(R.layout.navi_content_two, container, false);
TextView textViewNaviConTwo = (TextView) rootView.findViewById(R.id.textViewNaviConTwo);
textViewNaviConTwo.setText("HALLLOOOTJES :)");
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
With the Text "HALLLOOOTJES :)". And only in that fragment. But the other way around happens. This text is show on all the tabs except for tab number 1.
I'm certainly missing something.
Screenshots:
It should say "HALLLOOOTJES :)"
In your NaviContentFragmentTwo class, you are returning a NaviContentFragment from your newInstance method.
My guess is you should be returning a NaviContentFragmentTwo instance.
So it should be something like..
public class NaviContentFragmentTwo extends Fragment {
private static final String ARG_POSITION = "position";
public static NaviContentFragmentTwo newInstance(int position) {
NaviContentFragmentTwo fragment = new NaviContentFragmentTwo ();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
This code could be the possible cause:
public static NaviContentFragment newInstance(int position) {
NaviContentFragment fragment = new NaviContentFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
Try replacing NaviContentFragment to NaviContentFragmentTwo.
I have activity, that contains viewpager, with some fragment on the each tab. I want to call some method from fragment. I'm using:
android.support.v4.app.Fragment;
My code:
Main Activity:
public class ProfileActivity extends BaseTabsActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
initTabs();
// call method here
}
}
public class BaseTabsActivity extends BaseActivity {
protected void initTabs() {
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(4);
mViewPager.setAdapter(new ProfileTabsAdapter(getSupportFragmentManager(), this));
}
}
ProfileTabsAdapter:
public class ProfileTabsAdapter extends FragmentPagerAdapter {
public ProfileTabsAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Fragment0();
case 1:
return new Fragment1();
case 2:
return new Fragment2();
case 3:
return new Fragment3();
case 4:
return new Fragment4();
}
return null;
}
#Override
public int getCount() {
return 5;
}
#Override
public CharSequence getPageTitle(int index) {
switch (index) {
case 0:
return context.getResources().getString(R.string.string0);
case 1:
return context.getResources().getString(R.string.string1);
case 2:
return context.getResources().getString(R.string.string2);
case 3:
return context.getResources().getString(R.string.string3);
case 4:
return context.getResources().getString(R.string.string4);
}
return null;
}
}
Fragment:
public class Fragment0 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
return rootView;
}
public String doSomething() {
return "hi";
}
}
Have any idea?
It's well described in the official documentation: https://developer.android.com/training/basics/fragments/communicating.html
For your convinience, here is relevant part of the code:
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
}
But please make sure and go read the docs I linked at the begining.