8789

Text does not display on ViewPager

Question:

I am having an issue where my ViewPager text does not display when I start up my application. However, the texts do end up showing when I swipe the tab. I am not sure what the problem is and I would like the text to appear as soon as my application starts. Any help would be appreciated! thanks.

Countries Activity(<strong>MAIN ACTIVITY</strong>)

package myapps.countryapp; import android.content.Context; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import static myapps.countryapp.R.id.pager; public class CountriesActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_countries); ViewPager viewPager = (ViewPager) findViewById(pager); viewPager.setAdapter(new MyAdapter(getSupportFragmentManager())); } public class MyAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; //Create tab tiles. private String tabTiles[] = new String[] {"Countries", "Map"}; Context context; public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if(position == 0){ Fragment fragmentList = new Countries(); return fragmentList; }else { Countries fragmentList = new Countries(); return fragmentList; } } @Override public int getCount() { return PAGE_COUNT; } @Override public CharSequence getPageTitle(int position) { return tabTiles[position]; } } }//end of class

Countries.java(fragment file)

package myapps.countryapp; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.support.v4.app.Fragment; /** * A simple {@link Fragment} subclass. */ public class Countries extends Fragment { public Countries() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_countries, container, false); return view; } }

activity_countries(<strong>MAIN XML FILE</strong>)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="myapps.countryapp.CountriesActivity"> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id = "@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.PagerTabStrip android:layout_width="match_parent" android:layout_height = "40dp" android:paddingTop = "4dp" android:paddingBottom = "10dp" android:layout_gravity = "top" android:id = "@+id/pagerStrip"> </android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager>

fragment_countries.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="myapps.countryapp.Countries"> <!-- TODO: Update blank fragment layout --> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/MyListView"> </ListView> </FrameLayout>

Answer1:

Ok, here is my example of activity, viewpager and fragment:

activity:

public class KataloziSlike extends AppCompatActivity { private AdapterCustomPage mAdapterCustomPage; private ViewPager mViewPager; private TextView tv, brojStranica; private ImageButton settings, share, locirajMe, back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_katalozi_slike); if (Static.katalozi.getSlikeKataloga().size() == 1) { Message.message(KataloziSlike.this, "Katalog ima samo jednu stranu"); } brojStranica = (TextView) findViewById(R.id.tvBrojStranicaKataloga); brojStranica.setText(String.valueOf(1) + "/" + String.valueOf(Static.katalozi.getSlikeKataloga().size() + 1)); mAdapterCustomPage = new AdapterCustomPage(getFragmentManager(), this); mViewPager = (ViewPager) findViewById(R.id.view_pagerZaSlikeKataloga); mViewPager.setAdapter(mAdapterCustomPage); mViewPager.setCurrentItem(0); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (Static.katalozi.getSlikeKataloga().size() != 1) { if (position == 0) { Message.message(KataloziSlike.this, "Na prvoj stranici kataloga ste"); } else if (position == Static.katalozi.getSlikeKataloga().size() - 1) { Message.message(KataloziSlike.this, "Na poslednjoj stranici kataloga ste"); } } if (position <= Static.katalozi.getSlikeKataloga().size() - 1) { YourFragmentInterface fragment = (YourFragmentInterface) mAdapterCustomPage.instantiateItem(mViewPager, position); if (fragment != null) { fragment.fragmentBecameVisible(); } } brojStranica.setText(String.valueOf(position + 1) + "/" + String.valueOf(Static.katalozi.getSlikeKataloga().size() + 1)); } @Override public void onPageScrollStateChanged(int state) { } }); } }

adapter:

public class AdapterCustomPage extends FragmentStatePagerAdapter { protected Context mContext; int maxBrojStrana = Static.katalozi.getSlikeKataloga().size(); int pozicija; private ArrayList<Katalozi> mKatalozi; public AdapterCustomPage(FragmentManager fm, Context context) { super(fm); mContext = context; } @Override public Fragment getItem(int position) { // Create fragment object if (position == maxBrojStrana) { Fragment fragment = new ImageFragmentMapKatalozi(); return fragment; } else { Fragment fragment = new ImageFragmentKatalozi(); fragment.onPause(); Bundle args = new Bundle(); Log.i("Poizicija", String.valueOf(position)); String url = Static.katalozi.getSlikeKataloga().get(position); args.putString("url", url); fragment.setArguments(args); String link = Static.katalozi.getUrl(); args.putString("linkKataloga", link); return fragment; } } @Override public int getItemPosition(Object object) { pozicija = super.getItemPosition(object); return super.getItemPosition(object); } @Override public int getCount() { return maxBrojStrana + 1; } }

and fragment:

public class ImageFragmentKatalozi extends Fragment implements KataloziSlike.YourFragmentInterface { private PhotoViewAttacher mAttacher; private ImageView imageView; private Bundle args; private Button button; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_katalozi_slike, container, false); args = getArguments(); imageView = (ImageView) rootView.findViewById(R.id.imageViewAdapterZaListanjeKataloga); Callback imageLoadedCallback = new Callback() { @Override public void onSuccess() { if (mAttacher != null) { mAttacher.update(); } else { mAttacher = new PhotoViewAttacher(imageView); } } @Override public void onError() { // TODO Auto-generated method stub } }; Picasso.with(getActivity()).load(args.getString("url")).placeholder(R.drawable.load).error(R.drawable.error).into(imageView, imageLoadedCallback); mAttacher = new PhotoViewAttacher(imageView); button = (Button) rootView.findViewById(R.id.zaProveru); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); return rootView; } @Override public void onResume() { super.onResume(); Toast.makeText(getActivity(), "sadsadasd", Toast.LENGTH_SHORT); } @Override public void fragmentBecameVisible() { mAttacher.update(); } }

What is problem with yours is that it isnt instatiated when first fragment is made, only when you swipe(on fragment change) then its showing. Reffer to my example

Recommend

  • Fragment state lost when device left idle
  • Error inflating class Fragment when testing fragment using DataBinding
  • Why Touch not work on google map v2?
  • Android Music Player inside a fragment
  • Fragment tabhost is not working in Fragment
  • Binary XML file line #26: Duplicate id, tag null, or parent id with another fragment
  • Navigation drawer is always inflated
  • Is it OK to write code after [super dealloc]?
  • Android List Fragment with fixed Header
  • How can I detect errors in an HTML document fragment with Ruby?
  • Running a Service from ASyncTask or Thread?
  • Drag and Drop for imageview not working
  • Back press fragments issue in android
  • Is the listview recycling early? Or is Universal ImageLoader canceling in error? Or is my Logic wron
  • Variant from android-autofittextview library : scaling makes strange behaviour
  • SearchView WITHOUT ACTIONBAR [duplicate]
  • Google Maps getMap returns null
  • How to Make a Spinner (In a Fragment) That Changes the App's Language?
  • Using same constraints in multiple classes
  • LiveData is abstract android
  • Wrong row deleted from custom listview with spinner
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • Caching attributes in superclass
  • Webgrid not refreshing after delete MVC
  • Could not find rake using whenever rails
  • Jquery UI tool tip close icon
  • how to do an event when i swipe from fragment to the other
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Traverse Array and Display in markup
  • Cannot Parse HTML Data Using Android / JSOUP
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • coudnt use logback because of log4j
  • Getting Messege Twice Using IMvxMessenger
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Qt: Run a script BEFORE make
  • How to load view controller without button in storyboard?