I wanted my view pager to call onCreate every time so am using the below code:
#Override
public void setUserVisibleHint(boolean isVisibleToUser)
{
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && isChanged())
{
isChanged = false;
getFragmentManager().beginTransaction().detach(this).attach(this).commit();
viewPagerUpdatedInterface.viewPagerUpdated(); // inteface called in main method this will adapter.notifyDataSetChanged();
}
}
Here I am able to see correct value of string which I needed to update in logs but the same is not showing on screen.
My adapter class is below:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
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);
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Related
I have an Activity inside the activity I have tab layout with two tabs(tab 1, tab 2). Each tab have recyclerview with list of items using Recycleradapter. I want to change the tab title when the user is clicking the recycler child item.
FYI :- I have a child clicklistener inside the adapter.
Activity code
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "Tab1");
adapter.addFragment(new Tab2Fragment(), "Tab2");
viewPager.setAdapter(adapter);
}
Adapter Code
holder.parentlayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want to change the tab text here
});
You can get the Tab object from your TabLayout using following code and then you can apply your updated text in it:
mTabLayout.getTabAt(index).setText()
Try this way make one method for title update into viewPagerAdapter like below..
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void setTitle(String title)
{
mFragmentTitleList.add(title);
}
#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);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
mFragmentList = null;
mFragmentTitleList = null;
container.removeAllViews();
super.destroyItem(container, position, object);
}
public void updateTitle(String title,int position){
mFragmentTitleList.set(position,title);
notifyDataSetChanged();
}
#Override
public Parcelable saveState() {
return null;
}
}
then when you update title then call below method..
mViewPagerAdapter.updateTitle("Your update title");
I have a bottomnavigation in the main activity that has 5 tabs in each file
There are 5 tabs in each of the Fragment
Everything works fine
But when it comes to other Fragment bottomnavigation, viewpager is not updated
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();
}
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void setupViewPager(ViewPager viewPager) {
FrgPlayer.ViewPagerAdapter adapter = new FrgPlayer.ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new FrgJadval(), "a");
adapter.addFragment(new FrgKholase(), "b");
adapter.addFragment(new FrgAmar(), "c");
adapter.addFragment(new FrgRokhdad(), "d");
viewPager.setAdapter(adapter);
}
I do not know why the cause of the non-updating of ViewPager
Try calling notifyDataSetChanged():
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
notifyDataSetChanged();
}
I have search a lot about this solution. I tried remove from mFragmentList and mFragmentTitleList directly. And I also tried call the destroyItem. Many methods have been tried. None of them worked. I have been stuck on this problem for few days. Is there Any answer could help? Thanks.
public final class OrdersViewPagerAdapter extends android.support.v4.app.FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public OrdersViewPagerAdapter(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 void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
if (position <= getCount()) {
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void removeView(String title, ViewGroup view, TabLayout tabLayout) {
int index=-1;
int count=mFragmentTitleList.size();
for (int i=0;i<count;i++){
String mtitle=mFragmentTitleList.get(i);
if (mtitle.equals(title)){
index=i;
break;
}
}
if (index!=-1) {
mFragmentTitleList.remove(index);
tabLayout.removeTabAt(index);
destroyItem(view,index,mFragmentList.get(index));
notifyDataSetChanged();
}
}
}
I am using ViewPager,
I have to display more data in two fragments, but it does not show anything.
Is There an alternative layout to do it?
this is layout code:
<android.support.v4.view.ViewPager
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.8">
</android.support.v4.view.ViewPager>
this is adapter:
public class HitoeAdapter extends FragmentPagerAdapter {
private static HitoeAdapter adapter;
private final HeartRateFragment mHeartRateFragment;
private final ECGFragment mECGFragment;
public HitoeAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
mHeartRateFragment = new HeartRateFragment();
mECGFragment = new ECGFragment();
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return mHeartRateFragment;
case 1:
return mECGFragment;
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
}
Please help me.
Thanks.
Create the adapter class like below--
public 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);
}
}
Now add this method to set your fragments with viewpager -
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HeartRateFragment(), "HeartRateFrag");
adapter.addFragment(new ECGFragment(), "ECGFragment");
viewPager.setAdapter(adapter);
}
I'm working in an android projet. I use tab to display data with fragment. In the third fragment, when i update data, viewPager return to the first. I try everything.
This is my viewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private int baseId=0;
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);
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public long getItemId(int position) {
// give an ID different from position when position has been changed
return baseId + position;
}
/**
* Notify that the position of a fragment has been changed.
* Create a new ID for each position to force recreation of the fragment
* #param n number of items which have been changed
*/
public void notifyChangeInPosition(int n) {
// shift the ID returned by getItemId outside the range of all previous fragments
baseId += getCount() + n;
}
}
Thanks.