I'm using ViewPager in a fragment. It has 3 tabs
Here's the code of my fragmentOagerAdapter:
#Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = Fragment.instantiate(this.context, ClassA.class.getName());
break;
case 1:
fragment = Fragment.instantiate(this.context, ClassB.class.getName());
break;
case 2:
fragment = Fragment.instantiate(this.context, ClassC.class.getName());
break;
}
return fragment;
}
#Override
public CharSequence getPageTitle(final int position) {
String res = "";
switch (position) {
case 0:
res = "nameA";
break;
case 1:
res = "nameB";
break;
case 2:
res = "nameC";
break;
}
return res;
}
#Override
public int getCount() {
return 3;
}
I'm adding a a button in my main fragment that will allow me to switch between a list display and a grid display.
So what I want is to have a dynamic fragment depending on some enum stored within the Preferences
I want to do this for the 1st tab (position 0)
So I do the following:
#Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
switch (Settings.getInstance().getProductDisplayMode()) {
case LARGE_GRID:
fragment = Fragment.instantiate(this.context, ClassALargeGrid.class.getName());
break;
case SMALL_GRID:
fragment = Fragment.instantiate(this.context, ClassASmallGrid.class.getName());
break;
case LIST:
default:
fragment = Fragment.instantiate(this.context, ClassA.class.getName());
break;
}
break;
case 1:
fragment = Fragment.instantiate(this.context, ClassB.class.getName());
break;
case 2:
fragment = Fragment.instantiate(this.context, ClassC.class.getName());
break;
}
return fragment;
}
...
So it works fine when first opening the activity, but I don't know how to switch the fragment displayed in the first tab dynamically.
I tried calling destroyItem() on the FragmentPagerAdapter but it doesn't help...
Any idea ?
I actually had this problem a while back. I wound up having to take the fragment out of the viewpager and then use the FragmentManager to swap out the fragments dynamically.
Then, when I wanted to switch to another "page," I wrote some custom animations to "slide in" a fragment that hosted a ViewPager
Not a pretty solution, but it worked.
Related
I need to replace fragment in view pager in an activity on a nework call success in another fragment. View pager is inside my dashboard activity. Here is get item method
#Override
public Fragment getItem(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new SponsorshipOffersFragment();
break;
case 2:
if (mPreferenceManager.getConnectProfileStatus()) {
fragment = new CollaborationFragment();
} else {
fragment = new ConnectSettingFragment();
}
break;
case 3:
fragment = new ProtfolioFragment();
break;
case 4:
fragment = new SocialSummaryFragment();
break;
}
return fragment;
}
For case 2, first time getConnectProfileStatus will return false and ConnectSettingFragment will get call. After submitting profile from ConnectSettingFragment, on success I need to call CollaborationFragment.
Can someone help me to do ? I am calling below method from fragment
public void replaceFrament() {
mViewPager.setCurrentItem(2);
}
that is inside activity but nothing happening.
Actually I want to change color of TabLayout, StatusBar and Toolbar every time.
I change fragment like:
fragment1: blue
fragment2: red
fragment3: yellow
fragment 4: orange.
Color is changing but not according to given by me, it picked color randomly.
I create a method "changeHeader()" in main and call that method in fragments.
Main Activity Method
public void changeHeader(int toolbarcolor, int statusbarcolor, int tablayoutcolor) {
toolbar.setBackgroundColor(toolbarcolor);
tabLayout.setBackgroundColor(tablayoutcolor);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
getWindow().setStatusBarColor(statusbarcolor);
}
navHeader.setBackgroundColor(tablayoutcolor);
}
Calling in Fragment 1
((Main)getActivity()).changeHeader(
getResources().getColor(R.color.blue1),
getResources().getColor(R.color.blue2),
getResources().getColor(R.color.blue3)
);
calling in Fragment 2
((Main)getActivity()).changeHeader(
getResources().getColor(R.color.red1),
getResources().getColor(R.color.red2),
getResources().getColor(R.color.red3)
);
Calling in Fragment 3
((Main)getActivity()).changeHeader(
getResources().getColor(R.color.yellow1),
getResources().getColor(R.color.yellow2),
getResources().getColor(R.color.yellow3)
);
Calling in Fragment 4
((Main)getActivity()).changeHeader(
getResources().getColor(R.color.orange1),
getResources().getColor(R.color.orange2),
getResources().getColor(R.color.orange3)
);
I called "changeHeader" in in onCreateView of fragment, please provide solution for me.
This is because of your tab layout, it initializes three tabs at once (current, previous and next). You should create a PageChangeListener and add it to your ViewPager.
Create PageChangeListener
public class DetailOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
private int currentPage;
#Override
public void onPageSelected(int position) {
currentPage = position;
String colorCode = "#FFFFFF";
Toast.makeText(getActivity(), "Position: " + position, Toast.LENGTH_SHORT).show();
switch (position) {
case 0:
colorCode = "#FF0000";
break;
case 1:
colorCode = "#00FF00";
break;
case 2:
colorCode = "#0000FF";
break;
case 3:
colorCode = "#33BCFD";
break;
case 4:
colorCode = "#F44CFD";
break;
case 5:
colorCode = "#FAB55D";
break;
case 6:
colorCode = "#6ABCF6";
break;
case 7:
colorCode = "#F777FD";
break;
case 8:
colorCode = "#FAB888";
break;
case 9:
colorCode = "#999CFD";
break;
case 10:
colorCode = "#F00CFD";
break;
}
((MainActivity)getActivity()).changeHeader(colorCode);
}
public final int getCurrentPage() {
return currentPage;
}
}
Add Listener to your ViewPager
DetailOnPageChangeListener myPageChangeListener = new DetailOnPageChangeListener();
mViewPager.addOnPageChangeListener(myPageChangeListener);
Remove Listener in destroy method
mViewPager.removeOnPageChangeListener(myPageChangeListener);
changeHeader method in you MainActivity should look like this
public void changeHeader(String toolbarcolor) {
this.getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(toolbarcolor)));
}
I create a custom view pager like this:
public class TaskPagerAdapter extends FragmentStatePagerAdapter {
public TaskPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
return fragment;
}
#Override
public int getCount() {
return 5;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
and used on main fragment :
mPagerAdapter = new TaskPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(-2);
in the setCurrentItem I have to which number to use until that i do not get an nullpoiner error?I want NotificationFragment() class is first start on view pager
Edit
whit mViewPager.setCurrentItem(-1); i got blow error:
Process: safarayaneh.com.anotify.debug, PID: 21484
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:120)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1248)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18788)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1028)...
Read setCurrentItem
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.
Problem
mViewPager.setCurrentItem(-1);
Don't set -1 or -2 . index starts from 0 .
EDIT
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
return fragment;
WHY 1,2,3,4 BLANK . You should try with
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
}
return fragment;
mViewPager.setCurrentItem(0);
Since NotificationFragment has the first position.
It's index is 0.
Therefore, it should be something like mViewPager.setCurrentItem(0);
I have one activity. In that when I launch a single fragment, i should get different image for each launch of fragment.
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1)).commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
// mTitle = getString(R.string.title_section1);
break;
case 2:
// mTitle = getString(R.string.title_section2);
break;
case 3:
//`enter code here` mTitle = getString(R.string.title_section3);
break;
}
}
I see you use Android Studio but you have to be more specific with how you want to determine the image if you want to determine it based on integer sent to fragment try this
public static class CustomFragment extends Fragment{
.....
#Override
public void onCreateView()
{
position = getArguments().getInt("ARG_SECTION_NUMBER")
View view = getView(getActivity());
return view;
}
public View getView(context)
{
View view = context.getLayoutInflater().inflate(R.id.view_layout,null);
ImageView image= (ImageView)view.findViewById(R.id.image_view);
switch(position)
{
case(1):
image.setImageResource(your_image_res_for_1);
break;
.....
default:
break
}
}
}
or if you want to determine based on something else use an if statement or even switch depending on what it is you are using to determine.....hope i helped
I have built an Android application that uses tabs to navigate. I am using fragments for the majority of the application, however I need to display one activity. How do i display an activity in case 2. Below is the code i have at the moment, i know it returns a fragment but how do i enable the use of both?
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
System.out.println("current position = " + position);
Fragment fragment = null;
switch(position){
case 0:
return new diaryFragment();
case 1:
return new newEntryFragment();
case 2:
return new Calendarnew();
}
return null;
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new diaryFragment();
break;
case 1:
fragment = new newEntryFragment();
case 2:
fragment = new Calendarnew();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
Hi I dont actually get what you are up to but try reading this, maybe it could somehow solve your issue. https://stackoverflow.com/a/2958586/4470313