Wrong Fragment Picked using View Pager - android

I have Three different Fragments to show using View Pager. I have added the fragment to the adapter as follows. Title shown is correct but on click its picking the wrong fragment
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new AcceptsFragment(), "Accepts");
adapter.addFragment(new AskedFragment(), "Asked");
adapter.addFragment(new HelpedFragment(), "Helped");
viewPager.setAdapter(adapter);
}
Here is my adapter implementation.
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) {
Log.d("Fragment Clicked ",String.valueOf(position));
return mFragmentList.get(position);
}
#Override
public int getCount() {
//return mFragmentList.size();
return mFragmentist.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}

Related

How to update tab title from Adapter?

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");

Send Data from Activity to TabLayout fragments

I am designing a application in which i am facing problem,
I have a String date; in MainActivity.class and there are two fragments.
InboundDate & OutboundDate
I designed it by TabLayout & ViewPager, I am getting issue when i sending date to fragments & have to call WebServices by POST request on both the fragments.
Below is the date i have in MainActivity.class, How i send this date to both the fragments & call jsonRequest and set Recyclerview to both the fragments Or if i call the jsonRequest to MainActivity itself then how can i display to both the fragments.
date = getIntent().getStringExtra("DATE");
Log.e("DATE",""+date);
ViewPager & TabLayout Code:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new InBoundDate(),"INBOUND");
adapter.addFrag(new OutBoundDate(),"OUTBOUND");
viewPager.setAdapter(adapter);
}
//viewPager adapter ----- inbound & outbound
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 android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I got the answer of this question by searching over the internet.
In this question i have to send string to MainActivity.java to fragments present on same Activity.
Note : We have to create bundle object & have to send string(any dataType) by bundle.
I have two fragments InBoundDate & OutBoundDate.
In MainActivity.java in setupViewpager ()
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//bundle
bundle =new Bundle();
bundle.putString("DATE","2017-10-19");
InBoundDate frag_in = new InBoundDate();
OutBoundDate frag_out = new OutBoundDate();
frag_in.setArguments(bundle);
frag_out.setArguments(bundle);
adapter.addFrag(frag_in,"INBOUND");
adapter.addFrag(frag_out,"OUTBOUND");
viewPager.setAdapter(adapter);
}
ViewPager Adapter :
//viewPager adapter ----- inbound & outbound
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 android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
In your Fragments get String by this way :
String dateTxt;
private Bundle bundle;
//within OncreateView
bundle = this.getArguments();
dateTxt = bundle.getString("DATE");
Log.e("DATE_FRAG_INBOUND",""+dateTxt);

Viewpager Not Updating The View

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();
}

android- viewpager setCurrentItem make first fragment not loaded

i'm using viewpager and FragmentPagerAdapter in my application . in the first activity , this is my code :
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setScrollPosition(2,0f,true);
viewPager.setCurrentItem(2);
private void setupViewPager(ViewPager viewPager) {
Bundle bundle = new Bundle();
bundle.putString("id",id);
Fragment_contacts contact=new Fragment_contacts();
contact.setArguments(bundle);
Fragment_info info=new Fragment_info();
info.setArguments(bundle);
Fragment_Menu menu=new Fragment_Menu() ;
menu.setArguments(bundle);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(contact, "تماس با ما");
adapter.addFragment(info, "درباره ما");
adapter.addFragment(menu, "منو");
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 using setCurrentItem because my application is no in english and the default selected tab should be on the right .
When I use setCurrentItem , the Fragment_contacts is not loading and it's not showing any thing , but when I remove setCurrentItem , it loads perfectly.
what is the problem ? how can I solve this ?
Your ViewPager will take some time at initialization before you could use setCurrentItem. So set it after the intialization of your ViewPager by calling it in a thread, like this:
viewPager.post(new Runnable() {
#Override
public void run() {
viewPager.setCurrentItem(2);
}
});
Instead of keeping reference to your fragment in your activity, try to do the same in your ViewPager
The view pager is probably already initialized when you add your item in the list and there is a kind of "synchronization" issue between the two.
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager, Bundle bundle) {
super(manager);
Fragment_contacts contact=new Fragment_contacts();
contact.setArguments(bundle);
Fragment_info info=new Fragment_info();
info.setArguments(bundle);
Fragment_Menu menu=new Fragment_Menu() ;
menu.setArguments(bundle);
adapter.addFragment(contact, "تماس با ما");
adapter.addFragment(info, "درباره ما");
adapter.addFragment(menu, "منو");
}
#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);
}
}

Webservice call on current displayed fragment viewpager?

I have view pager with 5 fragments.
On all the fragments I have 5 different web services to call.
When the app is launched all the 5 fragments web services are triggered.
Where as only the current displayed fragment's web service should be called.
How to fix this? What is the solution for it?
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new AppFragment1());
adapter.addFragment(new AppFragment2());
adapter.addFragment(new AppFragment3());
adapter.addFragment(new AppFragment4());
adapter.addFragment(new AppFragment5());
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = 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) {
mFragmentList.add(fragment);
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
}

Categories

Resources