I am using two fragments inside viewpager. In the second fragment, I play a video from url using exoplayer. My problem is that when the activity is launch, the video is loaded automatically because all fragments are automatically created by viewpager. How to create one frament at a time so that the second fragment can play the video only if it is visible to the user (by swipping or tapping the tab).
The viewpager adapter
class ViewPagerAdapter extends FragmentStatePagerAdapter {
Bundle bundle;
public ViewPagerAdapter(FragmentManager manager, Bundle bundle) {
super(manager);
this.bundle = bundle;
}
#Override
public Fragment getItem(int position) {
Log.d("getItem", position + "");
switch (position) {
case 0:
IngredientListFragment ingredientListFragment = new IngredientListFragment();
ingredientListFragment.setArguments(bundle);
return ingredientListFragment;
case 1:
StepsListFragment stepsListFragment = new StepsListFragment();
stepsListFragment.setArguments(bundle);
return stepsListFragment;
default:
IngredientListFragment ing = new IngredientListFragment();
ing.setArguments(bundle);
return ing;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Ingredients";
case 1:
return "Steps";
default:
return "Ingredients";
}
}
}
Inside the activity
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), bundle);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
Please Help.
you do not need to create fragment one at a time:
use this code in second fragment to run exoplayer when user open that fragment:
private boolean _hasLoadedOnce = false; // your boolean field
#Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(true);
if (this.isVisible()) {
if (isFragmentVisible_ && !_hasLoadedOnce) {
// move exoplayer code here so this code run only when user select this fragment
_hasLoadedOnce = true;
}
}
}
One more thing, you have to put this line after creating viewpager:
in your case after
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(1);
Related
Since I implemented a BottomAppBar my App isn't responding when a certain Fragment should get displayed even though I haven't changed anything. There isn't an error message either and all other Fragments which are getting displayed when another item in the BottomNavigationBar is clicked work fine.
Fragment:
public class StatisticsFragment extends Fragment {
private TabLayout mTabLayout;
private ViewPager mViewPager;
private BankAccountsStatisticsFragment bankAccountsStatisticsFragment = new BankAccountsStatisticsFragment();
private BillsStatisticsFragment billsStatisticsFragment = new BillsStatisticsFragment();
private CategoriesStatisticsFragment categoriesStatisticsFragment = new CategoriesStatisticsFragment();
private GoalsStatisticsFragment goalsStatisticsFragment = new GoalsStatisticsFragment();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_statistics, container, false);
mTabLayout = (TabLayout) inflatedView.findViewById(R.id.tl_statistics);
mViewPager = (ViewPager) inflatedView.findViewById(R.id.vp_statistics);
mViewPager.setAdapter(new Adapter(getChildFragmentManager()));
mTabLayout.setupWithViewPager(mViewPager);
return inflatedView;
}
private class Adapter extends FragmentPagerAdapter {
private static final int TABS = 4;
public Adapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0: return bankAccountsStatisticsFragment;
case 1: return billsStatisticsFragment;
case 2: return categoriesStatisticsFragment;
case 3: return goalsStatisticsFragment;
default: throw new IllegalStateException("Couldn't find a fragment for position " + position);
}
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0: return getString(R.string.tab_bank_accounts);
case 1: return getString(R.string.tab_bills);
case 2: return getString(R.string.tab_categories);
default: return getString(R.string.tab_goals);
}
}
#Override
public int getCount() {
return TABS;
}
}
}
LOGCAT
LOGCAT
Inflating a Layout and creating a Fragment transition (like the "FragmentManager....replace().commit()") are done in the MainThread where the UserInterface is rendered, so the UserInterface will freeze in these moments.
I think you have to pre-load the "bad" Fragment and just HIDE it instead of Destroy it...in this way the next time you want to create it is already ready.
If I use a TabLayout with FragmentPagerAdapter, the overridden function getItem(int position) just return one fragment. In case of lager screen I would like to have 2 fragments to be returned.
How can I do it? should wrap these 2 fragments into one fragment and use that instead or is there any better solution?
FragmentPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: //Ingredients
return IngredientsFragment.newInstance(mRecipe);
case 1: // Details
{
// TODO Here instead of DetailFragment I want to return
// two fragments called DetailFragment and StepFragment.
return DetailFragment.newInstance(mRecipe);
}
default:
throw new RuntimeException(this.toString() + " Wrong fragment!");
}
}
And then in my Activity onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initializing, etc.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
I do not see you have much of a wiggle room here.
These two options are exlusive:
You are returning one Fragment with two nested fragments (Wrapped fragment)
You are returning two fragments each anchored to his own tab.
If you would like two fragments to appear on singular page of ViewPager, you have no choice but to wrap them.
Otherwise swiping would go between these two fragments, which is the same as if they are completely different, that is non-correlated.
OK I managed to solve the problem using a Fragment consist of two other fragments as children.
Just do not forget in this case the FragmentManager object should be populated with getChildFragmentManager() to work properly.
For more info look at my BakingApp project DetailStepWideScreenFragment.java
GitHub Repos
Fragment Wrapper:
public class DetailStepWideScreenFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail_step_wide_screen, container, false);
// I added the fragments here. StepFragment can be replaced using
// replaceStepFragment function.
DetailFragment detailFragment = DetailFragment.newInstance(mRecipe);
StepFragment stepFragment = StepFragment.newInstance(mRecipe, mStepId);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.fl_detail_fragment_wide_screen, detailFragment);
transaction.add(R.id.fl_step_fragment_wide_screen, stepFragment);
transaction.commit();
return view;
}
public void replaceStepFragment(String stepId) {
mStepId = stepId;
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
StepFragment stepFragment = StepFragment.newInstance(mRecipe, mStepId);
transaction.replace(R.id.fl_step_fragment_wide_screen, stepFragment);
transaction.commit();
}
}
PagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private DetailStepWideScreenFragment currentFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
currentFragment = null;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: //Ingredients
return IngredientsFragment.newInstance(mRecipe);
case 1: // Details
{
// Show base on screen size.
if (mIsLargeScreen) {
DetailStepWideScreenFragment detailStepWideScreenFragment = DetailStepWideScreenFragment.newInstance(mRecipe, "0");
currentFragment = detailStepWideScreenFragment;
return detailStepWideScreenFragment;
} else {
return DetailFragment.newInstance(mRecipe);
}
}
default:
throw new RuntimeException(this.toString() + " Wrong fragment!");
}
}
#Override
public int getCount() { return 2; }
public DetailStepWideScreenFragment getCurrentFragment() {
return currentFragment;
}
}
I'm sorry I am not fluent in English.
I wanna change the starting fragment in tab layout.
There is a value from another activity that gave with intent.
so I just want to start tab layout's fragment with my selection.
This is main activity's onCreate()
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
And this is the code of changing in fragment
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
return new TotalFragment();
case 1:
return new StatsFragment();
case 2:
return new FeedbackFragment();
case 3:
return new ManageFragment();
}
return null;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "TOTAL";
case 1:
return "STATS";
case 2:
return "FEEDBACK";
case 3:
return "MANAGE";
}
return null;
}
}
}
Jisu Lee, we can set the starting tab in tab layout with the help of setCurrentItem() to viewpager.
We can set it by two types :
Example :
1.Simple setting tab:
Explation : Set the currently selected page. If the ViewPager has already been through its first layout with its current adapter there will be a smooth animated transition between the current item and the specified item.
viewPager.setCurrentItem(1);
2.For enableing smoothScroll :
Secand parameter "True" to smoothly scroll to the new item, "false" to transition immediately
viewPager.setCurrentItem(1, true);
Or for getting the CurrentItem we have : viewPager.getCurrentItem();
It will return the starting tab index (int).
mViewPager.setCurrentItem({index you want}, true);
I am using view pager which is having 3 fragments
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private static final String TAG="ViewPageAdapter";
private final int PAGES = 3;
private String[] title = new String[]{"Frag1",
"Frag2", "Frag2"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Log.d(TAG, "position is " + position);
switch (position) {
case 0:
return new Frag1();
case 1:
return new Frag2();
case 2:
return new Frag3();
default:
throw new IllegalArgumentException(
"The item position should be less or equal to:" + PAGES);
}
}
#Override
public int getCount() {
return PAGES;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
I am setting view pager from main activity:
viewPageAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.addOnPageChangeListener(onPageChangeListener);
viewPager.setAdapter(viewPageAdapter);
When i execute viewPageAdapter.notifyDataSetChanged();
or viewPager.setAdapter(viewPageAdapter); method to recreate fragments the activity is getting destroyed.don't know what causing the issue.
I checked many solution nothing worked.
ViewPager PagerAdapter not updating the View
Update ViewPager dynamically?
I am asking another problem because which is related to above question.
The problem which is described in link
ViewPager onPageSelected for first page
where i am not able to get first page tab title on load.i am using android.support.v4.view.PagerTabStrip
Please help me in getting solved. Wasted my whole day to get rid of this :(
i want to set different pages for ViewPager from class. for example:
i have 4 activity A + B + C and D.
A include my ViewPager.
B +C + D are different pages that must be shown in ViewPager.(each of them have different contex)
my question is that, how can i recognize these pages into ViewPager?
thanks
You can add but better way is that you will create a activity and your four activity should be child fragment of the same .
In short your all activity should be fragments jst load the fregment when you swipe viewpage indicater
sample code for adapter
//User adapter according condition
final FragmentPagerAdapter adapter = new FragmentChildPageAdapter(getChildFragmentManager());
//final FragmentPagerAdapter adapter = new FragmentChildPageAdapter(getActivity().getSupportFragmentManager()));
final ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
pager.setAdapter(adapter);
final TabPageIndicator indicator = (TabPageIndicator) v.findViewById(R.id.indicator);
indicator.setViewPager(pager);
//Adapter
class FragmentChildPageAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {
public FragmentChildPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
//first fregment
break;
case 1:
//Second fregment
break;
case 2:
//third fragment
break;
case 3:
//Forth fragment
break;
}
return fragment;
}
#SuppressLint("DefaultLocale")
/*#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}*/
#Override
public int getCount() {
//return CONTENT.length;
return CONTENT.size();
}
#Override
public int getIconResId(int position) {
return (Integer) Icon.get(position % CONTENT.size());
}
}