I am unable to create dynamic layout for each tabs, i am able to create tabs dynamically by this constructor(public MyPagerAdapter(FragmentManager fm , int noOfTabs)), but coudn't inflate the view for each tabs creating for each fragment which should be dyanmic.
public class MyPagerAdapter extends FragmentPagerAdapter
{
private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
"Top New Free", "Trending" };
int noOfTabs;
**public MyPagerAdapter(FragmentManager fm , int noOfTabs)
{
super(fm);
this.noOfTabs = noOfTabs;
}**
/*public MyPagerAdapter(FragmentManager fm)
{
super(fm);
}*/
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return noOfTabs; //TITLES.length;
}
#Override
public Fragment getItem(int position) {
return SuperAwesomeCardFragment.newInstance(position);
}
}
I am facing problem here which should be dynamic and not static as three fragments(FragmentA, FragmentB , FragmentC), it can be any number of fragments depending on the int noOfTabs:
#Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 2:
fragment = new FragmentC();
break; }
return fragment;
}
Thanks in advance if any one could help me in this, as i am using this with help of library PagerSlidingTabStrip.
#nbokmans solution may be okay but i've used a container to accomplish that.
Creating a simple host fragment with FrameLayout inside and by replacing fragment was a good dynamic solution for me.
Using Otto EventBus just made it awesome because it can be changed from everywhere with just simple post
#Subscribe
fun changeFragment(type: FragmentTypeEvent){
val ft = activity.supportFragmentManager.beginTransaction()
val frag = getFragment(type)
ft.replace(R.id.container_frame,frag)
ft.commit()
}
Related
Like in the image below, I have an Activity with a ViewPager (with TabLayout) inside.
Now I want to refresh for example the text of an TextView in a ViewPager Fragment. How can I do this?
http://imgur.com/0kWQCfx
[Sorry for the image link, but I don't have enough reputations yet.]
I already tried something like this:
//In Activity
View root = getLayoutInflater().inflate(R.layout.fragment_home, null);
TextView textView = (TextView) root.findViewById(R.id.text_view);
textView.setText("test");
But I get a NullPointerException for the textView.
Edit 1:
I only need to access the components inside one Fragment, so I am not using an array inside the Adapter.
I changed my ViewPagerAdapter to this:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public HomeFragment fragment;
...
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragment = new HomeFragment();
case 1:
...
default:
return null;
}
}
But I still get a NullPointerException here:
final Fragment root = viewPagerAdapter.fragment;
Log.d("testtest", String.valueOf(root == null));
Write this code in your activity where you want to change fragment value. Your fragment id is e.g. my_fragment
Fragment frag = getFragmentManager().findFragmentById(R.id.my_fragment)
((TextView) frag.getView().findViewById(R.id.text_view)).setText(s);
You can create your own Adapter:
private class YourAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener
{
Fragment screens[];
public CheckinHistoryAdapter(FragmentManager fm) {
super(fm);
screens = new Fragment[3];
screens[0] = new CoachFragment();
screens[1] = new LogingFragment();
screens[2] = new HistoryFragment();
}
#Override
public Fragment getItem(int index) {
if(index <= screens.length)
{
return screens[index];
}
return null;
}
#Override
public int getCount() {
return screens.length;
}
}
Use it like this
mViewPager.setAdapter(this.mPagerAdapter);
You can access fragment object with getItem method.
If you only need a reference to one Fragment, then just use the position parameter passed into instantiateItem(), and only assign your Fragment reference for the desired position:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public HomeFragment fragment;
//...
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new HomeFragment(); //modified
case 1:
...
default:
return null;
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
if (position == 0) {
fragment = (HomeFragment) createdFragment;
}
return createdFragment;
}
}
I spent the last days searching for a way to communicate two fragments, there are tons of answers and tutorials about how to do it but none of them worked for me. I tried parcelable, boundle, interfaces but nothing worked (probably because I made a mistake implementing them).
The tricky thing is that most tutorials explain a basic case with two fragments with IDs in their layouts but my app has a more complex architecture. Check the image below:
The aim is to send a custom object from the ListView (lv1) to ListView (lv2). Each listview is in a fragment created using FragmentPagerAdapter and belong to a different Drawer Fragment as explained in the image.
Can anyone point me in the right direction here?
MainActivity code of how Navigation Drawer fragments are created:
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
String title = getString(R.string.app_name);
HomeFragment homeFragment = null;
QuotesFragment quotesFragment = null;
WatchlistFragment watchlistFragment = null;
switch (position) {
case 0:
homeFragment = new HomeFragment();
title = getString(R.string.title_inicio);
break;
case 1:
quotesFragment = new QuotesFragment();
title = getString(R.string.title_cotizaciones);
break;
case 2:
watchlistFragment = new WatchlistFragment();
title = getString(R.string.title_messages);
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (position) {
case 0:
fragmentTransaction.replace(R.id.container_body, homeFragment, "HomeFragment");
break;
case 1:
fragmentTransaction.replace(R.id.container_body, quotesFragment, "QuotesFragment");
break;
case 2:
fragmentTransaction.replace(R.id.container_body, watchlistFragment, "WatchlistFragment");
break;
}
Also, check below the code of how fragmentpageadapter creates the fragmenttabs:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new QuotesIndexespage(),getResources().getString(R.string.one));
adapter.addFragment(new QuotesCurrenciesPage(),getResources().getString(R.string.two));
adapter.addFragment(new QuotesCommoditiesPage(),getResources().getString(R.string.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);
}
}
I'm making a score keeping app using tabs, tablayout, fragments, and a FragmentPagerAdapter. The app runs fine, but I can't figure out how I can access the fragments. After doing some work in the fragment, I want to access the fragment to update its score. How can I access the current fragment?
Edit - my main problem is finding the position. I can use tabLayout.getSelectedTabPosition() to get the correct position if I clicked on the tab, but if I swipe between tabs, viewpager handles it differently and I don't know how to find the position from that.
public class PagerAdapter extends FragmentPagerAdapter {
private int numTabs;
public PagerAdapter(FragmentManager fm, int tabs) {
super(fm);
numTabs = tabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return makeScoreFrag(1, "Team 1");
case 1:
return makeScoreFrag(2, "Team 2");
case 2:
return makeScoreFrag(3, "Team 3");
case 3:
return makeScoreFrag(4, "Team 4");
default:
return null;
}
}
#Override
public int getCount() {
return numTabs;
}
private ScoreFragment makeScoreFrag(int num, String title){
Bundle bundle = new Bundle();
bundle.putInt("score", 0);
bundle.putInt("number", num);
bundle.putString("title", title);
ScoreFragment frag = new ScoreFragment();
frag.setArguments(bundle);
return frag;
}
}
You can Override instantiateItem method in FragmentPagerAdapter
ScoreFragment[] fragments = new ScoreFragment[numTabs];
#Override
public Object instantiateItem(ViewGroup container, final int position) {
fragments[position] = (ScoreFragment) super.instantiateItem(container, position);
return fragments[position];
}
Then current fragment is:
ScoreFragment curFragment = fragments[curTab];
I'm crating a tabHost to contain all the content of my Android application in 3 tabs, should i create the content of the tab inside its linear layout or create the tab content in a separate fragment and include it in the tab layout?
And how to include the fragment inside a layout also?
Excuse me i'm a bit beginner to this.
you just add viewpagger for calling fragment
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Fragment1", "Fragment2", "Fragment2" };
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) {
//return SuperAwesomeCardFragment.newInstance(position);
switch (position) {
case 0:
return new JavaFragment1(); //this call you fragment1
case 1:
return new JavaFragment2(); //this call you fragment2
case 2:
return new JavaFragment3(); //this call you fragment3
}
return null;
}
}
I am using a FragmentPagerAdapter to swipe three Fragments. It's not giving me any errors, but it always loads the next Fragment in advance. How can I stop this behavior?
Here is my FragmentPagerAdapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context _context;
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context = context;
}
#Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch (position) {
case 0:
f = ReservationTab.newInstance(_context);
break;
case 1:
f = GalleryTab.newInstance(_context);
break;
case 2:
f = VideoTab.newInstance(_context);
break;
}
return f;
}
#Override
public int getCount() {
return 3;
}
}
try to use this
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2 /*count of your fragments in viwePager*/);
Try using setOffscreenPageLimit(0) on your ViewPager