I have a tab layout with 4 instances of the same fragment but with differents data in the list. And i want to get the current page so i can change data using the title. The problem is that the :mViewPager.getCurrentItem()) method isn't always called.
In oncreateview i have :
jours2 = new ArrayList<>();
rv = (RecyclerView) rootView.findViewById(R.id.recyclerView);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new MyAdapter(jours2);
rv.setAdapter(adapter);
String test= mSectionsPagerAdapter.getPageTitle(mViewPager.getCurrentItem()).toString();
for (Cours c : jours)
{
if (c.jour==test)
{
jours2.add(c);
}
}
adapter.notifyDataSetChanged();
So i can i change my code so it always get the current item title.
SectionPageAdapter :
public class SectionsPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments = new ArrayList<>();
ArrayList<String> titles = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public void AddFragment(Fragment fragment,String title)
{
fragments.add(fragment);
titles.add(title);
}
#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).
return fragments.get(position);
}
#Override
public int getCount() {
// Show 3 total pages.
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
You can either use this to get the position
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getPosition();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
or you can simply use tabLayout.getSelectedTabPosition().
Related
Right now I have a tablayout with a ViewPager + adapter below it. I want to pause a fragment when a different tab is selected (not destroy/pop it, just pause it) and resume it when the fragment's tab is selected.
public class PagerFragment extends Fragment {
ViewPagerAdapter pagerAdapter;
ViewPager pager;
TabLayout tabLayout;
int position;
public PagerFragment() {
super();
}
public void setPosition(int position) {
this.position = position;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onStop() {
super.onStop();
}
#Override
public void onPause() {
super.onPause();
pagerAdapter.clear();
pagerAdapter.notifyDataSetChanged();
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onResume() {
super.onResume();
if (!isAdded()) {
pagerAdapter.instantiateItem(pager, position);
}
}
#SuppressLint("InflateParams")
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_pager, null);
pagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
setPosition(position);
pagerAdapter.setDevicesList(devicesList);
pager = root.findViewById(R.id.pager);
pager.setAdapter(pagerAdapter);
tabLayout = root.findViewById(R.id.tab_layout);
for (int i = 0; i < devicesList.size(); i++) {
tabLayout.addTab(tabLayout.newTab().setText(String.valueOf(devicesList.get(i).getName())));
}
TabLayout.Tab tab = tabLayout.getTabAt(position);
if (tab != null) {
tab.select();
pager.setCurrentItem(position);
tabLayout.setupWithViewPager(pager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
int pos = tab.getPosition();
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager));
return root;
}
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<ConnectedBluetoothDevice> devicesList;
ArrayList<DataViewFragment> dataViewFragments;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
/**
* Initializes the adaptor with the device list and the list of data fragments.
* #param devicesList List of devices to be displayed in their own data fragment.
*/
public void setDevicesList(ArrayList<ConnectedBluetoothDevice> devicesList) {
this.devicesList = devicesList;
dataViewFragments = new ArrayList<>();
for (int i = 0; i < devicesList.size(); i++) {
dataViewFragments.add(new DataViewFragment(devicesList.get(i)));
}
}
/**
* GetItem
* #param position position of item in the viewPager list to be returned
* #return fragment to view
*/
#NonNull
public Fragment getItem(int position) {
if (dataViewFragments != null && position < dataViewFragments.size()) {
return dataViewFragments.get(position);
} else {
return new DataViewFragment(null);
}
}
#Override
public int getCount() {
return devicesList.size();
}
/**
* Updates the tabs in tabLayout while swiping pages
* #param position of the viewPage
* #return title of the tab
*/
public CharSequence getPageTitle(int position) {
super.getPageTitle(position);
return devicesList.get(position).getName();
}
public void clear() {
dataViewFragments.clear();
}
}
Some notes:
I'm instructed to fix this bug in ViewPager and FragmentPagerAdapter, so please don't recommend me solutions in ViewPager2 and FragmentStatePagerAdapter :)
I heard that setOffscreenPageLimit(0); isn't the best practice so I want to avoid that situation (especially since I only need to pause the fragment but not destroy it)
This depends on the version of ViewPager you are using as it was added later.
Your are using a deprecated constructor
So as long as your using a recent version of Viewpager you should use the non deprecated constructor:-
public FragmentPagerAdapter(
#NonNull FragmentManager fm,
#FragmentPagerAdapter.Behavior int behavior
)
This constructor with the behaviour BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
then only the current Fragment is in the RESUMED state. All other fragments are capped at STARTED
To get to the STARTED lifecycle state from RESUMED onPause is called.
so change
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
to
public ViewPagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
This method is used to for calling functions to run only when the specified tab is selected.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// add your tab functions here
} else {
// do nothing
}
}
I want to make the webview content change (url change) when tab is changed.
However, my code is late. When I change tabs, it shows the last tab's content.
For example,
App Initialize -> shows first tab and first tab's webview (success)
Click Tab 2 -> shows second tab but first tab's webview (fail)
Click Tab 3 -> shows third tab but first tab's webview (fail)
Return to Tab 2 -> shows second tab and second tab's webview (success)
Please help.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
final ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
SectionsPagerAdapter.java
/**
* A [FragmentPagerAdapter] that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
#StringRes
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2, R.string.tab_text_3, R.string.tab_text_4, R.string.tab_text_5, R.string.tab_text_6, R.string.tab_text_7};
private final Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
#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).
return PlaceholderFragment.newInstance(position + 1);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return mContext.getResources().getString(TAB_TITLES[position]);
}
#Override
public int getCount() {
// Show 2 total pages.
return 7;
}
}
PlaceholderFragment.java
/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment {
String type;
private static final String ARG_SECTION_NUMBER = "section_number";
private PageViewModel pageViewModel;
public static PlaceholderFragment newInstance(int index) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
}
pageViewModel.setIndex(index);
}
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container, false);
final WebView webView = (WebView)root.findViewById(R.id.webView);
webView.setWebViewClient( new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
webView.loadUrl(url);
return true;
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
pageViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
type = s;
}
});
if(type == null)
{
type="1";
}
webView.loadUrl("https://my.domain/list.php?type="+type);
return root;
}
}
PageViewModel.java
public class PageViewModel extends ViewModel {
private MutableLiveData<Integer> mIndex = new MutableLiveData<>();
private LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
#Override
public String apply(Integer input) {
return input.toString();
}
});
public void setIndex(int index) {
mIndex.setValue(index);
}
public LiveData<String> getText() {
return mText;
}
}
You can add a loader till the webview loads in respective tabs. onPageFinished() is a callback when webview has completed loading
web_view.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, weburl: String) {
// Stop loading
}
}
My recommendation is that you should change
webView.loadUrl("https://my.domain/list.php?type="+type);
to
webView.loadUrl("https://my.domain/list.php?type=" + getArguments().getInt(ARG_SECTION_NUMBER));
and see how it works. If it works as you expected, I think you don't need the PageViewModel.java at all.
The root cause of unexpected behavior
In PageViewModel, the value of mIndex is updated by the following line of onCreate in PlaceholderFragment
pageViewModel.setIndex(index);
It also changes the value of mText, which is observed by the following block of onCreateView in PlaceholderFragment:
pageViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
type = s;
}
});
Therefore, PageViewModel and PlaceholderFragment are in a race unexpectedly.
ViewPager creates more Fragment early, as written in setOffscreenPageLimit. It is not a good idea to update the value of mIndex in onCreate of PlaceholderFragment.
ViewPager2 is removing last tab instead of current tab, same problem was occurring when i was using viewpager.
Also when selected tab is last one it removes last tab correctly
dependency
implementation "androidx.viewpager2:viewpager2:1.0.0"
This is main activity code
MainActivity.java
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager2 viewPager;
ViewPagerAdapter adapter;
Button btnAddTab,btnRemoveCurrentTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tabs);
btnAddTab=findViewById(R.id.btn_add);
btnRemoveCurrentTab=findViewById(R.id.btn_remove);
adapter=new ViewPagerAdapter(this);
viewPager.setAdapter(adapter);
new TabLayoutMediator(tabLayout, viewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
#Override public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
tab.setText("Store " + (position + 1));
}
}).attach();
btnRemoveCurrentTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
adapter.removeFrag(tabLayout.getSelectedTabPosition());
}
});
btnAddTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DynamicFragment fView = new DynamicFragment();
adapter.addFrag(fView,"Store "+(adapter.getmFragmentIdList().contains(adapter.getItemCount()+1)?adapter.getItemCount()+2:adapter.getItemCount()+1),(adapter.getmFragmentIdList().contains(adapter.getItemCount()+1)?adapter.getItemCount()+2:adapter.getItemCount()+1));
}
});
}
}
ViewPagerAdapter
This is FragentStateAdapter
public class ViewPagerAdapter extends FragmentStateAdapter {
private final List<DynamicFragment> mFragmentList = new ArrayList<>();
private final List<Integer> mFragmentIdList = new ArrayList<>();
public ViewPagerAdapter(#NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
#NonNull #Override public Fragment createFragment(int position) {
return DynamicFragment.newInstance(String.valueOf(position),mFragmentIdList.get(position));
}
public void addFrag(DynamicFragment fragment, String title, int id) {
mFragmentList.add(fragment);
Log.e("fragment id " ,""+id);
Log.e("fragment array " ,title+ mFragmentList.size());
mFragmentIdList.add(id);
notifyDataSetChanged();
}
public void removeFrag(int pos) {
Log.e("deleted frag",mFragmentIdList.get(pos)+" "+pos);
mFragmentList.remove(pos);
mFragmentIdList.remove(pos);
notifyDataSetChanged();
}
public List<Integer> getmFragmentIdList() {
return mFragmentIdList;
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override public int getItemCount() {
return mFragmentList.size();
}
}
Fragment
This is my dynamic fragment which i want to add and remove
public class DynamicFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private static final String ARG_ID = "id";
private String sectionNumber;
int id;
TextView textView;
EditText editText1,editText2;
public DynamicFragment() {
// Required empty public constructor
}
public static DynamicFragment newInstance(String sectionNumber, int id) {
DynamicFragment fragment = new DynamicFragment();
Bundle args = new Bundle();
args.putString(ARG_SECTION_NUMBER, sectionNumber);
args.putInt(ARG_ID, id);
fragment.setArguments(args);
Log.e("StoreDynamic","dynamic new instance");
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView= inflater.inflate(R.layout.fragment_dynamic, container, false);
textView=rootView.findViewById(R.id.textView);
editText1=rootView.findViewById(R.id.et1);
editText2=rootView.findViewById(R.id.et2);
sectionNumber = getArguments().getString(ARG_SECTION_NUMBER);
id=getArguments().getInt(ARG_ID);
editText1.setText(id+" "+sectionNumber);
textView.setText(id+" "+sectionNumber);
return rootView;
}
}
I ran into the same problem and the solution to this question: remove fragment in viewPager2 use FragmentStateAdapter, but still display worked for me. Mostly the part about overriding the getItemId function to return something other than the position to identify the item because the item's position changes when adding and removing fragments.
You need to overwrite the methods getItemId() and containsItemId()
as shown in this post: https://stackoverflow.com/a/57944197/870242
items variable holds your fragment (mFragmentList )
private val pageIds= items.map { it.hashCode().toLong() }
override fun getItemId(position: Int): Long {
return items[position].hashCode().toLong() // make sure notifyDataSetChanged() works
}
override fun containsItem(itemId: Long): Boolean {
return pageIds.contains(itemId)
}
I have two fragment in one activity, and I need to know how to just hide the second fragment and leave the firs one
So, i want to hide PerfilDatosFragmen();, but i also need to keep "2" in return statement. how can i do that?
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
String[] encabezado = {"Tarjeta", "Datos"};
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new PerfilTarjetaFragment();
case 1:
return new PerfilDatosFragmen();
}
return null;
}
#Override
public int getCount() {
System.out.println(vacio);
// int vacio = getIntent().getExtras().getInt("AcompaƱante");
/* if(vacio.equals("1")){
a= 1;
}else{
a= 2;
}*/
return 2;
}
It's very simple. PagerAdapter.getCount() is defined the count of your fragments. You always return 2. The best solution is:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> fragments = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
fragments.add(new PerfilTarjetaFragment());
fragments.add(new PerfilDatosFragmen());
}
String[] encabezado = {"Tarjeta", "Datos"};
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
//SampleTabsWithIcons.java
public class SampleTabsWithIcons extends FragmentActivity {
private static final int[] ICONS = new int[] {
R.drawable.events,
R.drawable.rss,
R.drawable.user,
R.drawable.bell,
R.drawable.search
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.simple_tabs);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
class GoogleMusicAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TestFragment.newInstance(ICONS[position % ICONS.length]);
}
#Override public int getIconResId(int index) {
return ICONS[index];
}
#Override
public int getCount() {
return ICONS.length;
}
}
}
//TestFragment.java
public final class TestFragment extends Fragment {
private static final String KEY_CONTENT = "TestFragment:Content";
public static TestFragment newInstance(int icons) {
TestFragment fragment = new TestFragment();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 20; i++) {
builder.append(icons).append(" ");
}
builder.deleteCharAt(builder.length() - 1);
fragment.mContent = builder.toString();
return fragment;
}
private String mContent = "???";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
#SuppressWarnings("deprecation")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText(mContent);
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(text);
return layout;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
//TestFragmentAdapter.java
class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
private static final int[] ICONS = new int[] {
R.drawable.events,
R.drawable.rss,
R.drawable.user,
R.drawable.bell,
R.drawable.search
};
private int mCount = ICONS.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TestFragment.newInstance(ICONS[position % ICONS.length]);
}
#Override
public int getCount() {
return mCount;
}
#Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
and my output is,
My question is instead of displaying numbers while selecting each tabs, I want to go to new fragment activity (ie, newpage.java). Now some numbers are showing on each tab selection. How can i implement this.
I have 5 class (first.java,second.java,third.java,four.java,five.java) and 5 layout for each tabs (first.xml... respectively)
I tried a lot but no success. Does anyone know the answer.
Thanks.
Ok you can make your own Fragments and then return them in the getItem(int position); for example, you could have an array of your Fragments calendar, feed, etc. then on each position you return one.
//SampleTabsWithIcons.java
Fragment[] tabFragments = new Fragment[] {CalendarFragment.newInstance(),
FeedFragment.newInstance()}
.....
#Override
public Fragment getItem(int position) {
return tabFragments[position];
}
You can simply use this and set the tab to the viewPager ...
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
Inside fragment pager adapter you can do like this.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
frag = UsageRecoSummaryFragemrent.newInstance(position);
// changeColor(newColor);
break;
Inside fragment
public static UsageRecoSummaryFragemrent newInstance(int position) {
UsageRecoSummaryFragemrent f = new UsageRecoSummaryFragemrent();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, 0);
f.setArguments(b);
return f;
}