android- viewpager setCurrentItem make first fragment not loaded - android

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

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

how make Sliding tabs make clickable in Android Development?

I have made a sliding tabs feature which is currently working. I also want to make it clickable
public class MainActivity extends AppCompatActivity {
public List<String> datafragmnt = new ArrayList<String>();
public ViewPager viewPager;
public pagerfragmetAdapter myAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datafragmnt.add("Frgmnt 1 ");
datafragmnt.add("Frgmnt 2 ");
datafragmnt.add("Frgmnt 3 ");
datafragmnt.add("Frgmnt 4 ");
myAdapter = new pagerfragmetAdapter(getSupportFragmentManager());
myAdapter.setMydata(datafragmnt);
myAdapter.setFragmntList(new fragment1());
myAdapter.setFragmntList(new fragment2());
myAdapter.setFragmntList(new fragment3());
myAdapter.setFragmntList(new fragment4());
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(myAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
This is what I have tried so far. Any help is welcome!
Please help me!
You Need to create an Adapter for that view page,something like this:
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);
}
}
And in the SetUpViewPager you add your fragments like this:
private void setupViewPager(ViewPager viewPager) {
Main.ViewPagerAdapter adapter = new Main.ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new fragment1(), "FRAGN+MENT UNO");

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

How to do tabs with icons

I'm trying to do an activity with tabs and pagerview
I found this code in a blog but it does not come like making the tabs have icons instead of text ... I put the code:
ViewPagerAdapter.java
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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Tablayout_in_Android.java
public class Tablayout_in_Android extends Fragment {
public Tablayout_in_Android() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tablayout_in__android, container, false);
}
}
Method onCreate main class
viewPager = (ViewPager) findViewById(R.id.viewpager);
addTabs(viewPager);
tabLayout = (TabLayout) findViewById(R.id.pestañas);
tabLayout.setupWithViewPager(viewPager);
}
private void addTabs(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Tablayout_in_Android(), "ONE");
adapter.addFrag(new Tablayout_in_Android(), "TWO");
adapter.addFrag(new Tablayout_in_Android(), "THREE");
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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
how can i change text for icons? Thanks
Try this:
tabLayout.setupWithViewPager(mViewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.your_icon);
}
To expand on Jonathan's answer, you can also have a list of icons to show in the tabs. Ensure the icons match the number of tabs you have.
tabLayout.setupWithViewPager(mViewPager);
int[] tabIcons = {
R.drawable.ic_icon_1,
R.drawable.ic_icon_2,
R.drawable.ic_icon_3
};
for(int i=0; i<tabLayout.getTabCount(); i++){
if(tabLayout.getTabAt(i) != null){
tabLayout.getTabAt(i).setIcon(tabIcons[i]);
}
}
Also, you may want to have the icon and tab label to be in a horizontal orientation, the use app:tabInlineLabel="true" in the TabLayout's xml layout

Wrong Fragment Picked using View Pager

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

Categories

Resources