ViewPager & ViewPagerIndicator does not display anything

I have a problem with my application, my ViewPager and ViewPagerIndicator ( http://viewpagerindicator.com/ ) do not display anything, and I do not understand why. I checked everything several times (setting the adapter correctly, adding the view to instantiateItem, returning a good size to getCount (), ...), I tried several things (changing the layout settings in xml, reworking the MyPagerAdapter class) even finding a similar problem on Google and here but nothing worked / was not my problem.

I also have a swamp, I donโ€™t know if this has a link, but the instantiateItem method in my PagerAdapter is only called 2 times, and getCount () returns 3 well.

I really donโ€™t know where this can come from, if anyone has an idea, I will take it with great pleasure:

OnCreate (from an Activity containing ViewPager and ViewPagerIndicator):

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_infos_vehicle); title = (TextView) findViewById(R.id.title); ll = (LinearLayout) findViewById(R.id.mygallery); filenames = new ArrayList<String>(); [...] MyPagerAdapter adapter = new MyPagerAdapter(getResources().getStringArray(R.array.infos_vehicle)); ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(adapter); TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.titles_pager); indicator.setFooterIndicatorStyle(IndicatorStyle.Triangle); indicator.setViewPager(pager); [...] } 

MyPagerAdapter class (the third case was here for a more visible test, see if these were my fillGeneral or fillEquipments methods):

private class MyPagerAdapter extends PagerAdapter {

 private final String[] TITLES; public MyPagerAdapter(String[] titles) { TITLES = titles; } @Override public int getCount() { return (TITLES.length); } @Override public Object instantiateItem(View collection, int position) { LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = null; switch (position) { case (0): v = inflater.inflate(R.layout.linear_layout, null); fillGeneral((ViewGroup) v.findViewById(R.id.layout)); break; case (1): v = inflater.inflate(R.layout.linear_layout, null); fillEquipments((ViewGroup) v.findViewById(R.id.layout)); break; case (2): v = new ImageView(getApplicationContext()); ((ImageView) v).setImageDrawable(getApplicationContext().getResources().getDrawable(android.R.drawable.alert_dark_frame)); break; } ((ViewPager) collection).addView(v); return (v); } @Override public CharSequence getPageTitle(int position) { return (TITLES[position]); } @Override public void destroyItem(View collection, int position, Object view) { ((ViewPager) collection).removeView((View) view); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return (arg0.equals(arg1)); } @Override public Parcelable saveState() { return null; } 

}

And xml activity:

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/hello_world" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/first_color" /> <Button android:id="@+id/try_vehicle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:onClick="tryingAlert" android:text="@string/try_vehicle" /> <Button android:id="@+id/ask_informations" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/try_vehicle" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:onClick="informationsAlert" android:text="@string/ask_informations" /> <Button android:id="@+id/remove_selection" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="removeFromSelection" android:layout_below="@+id/ask_informations" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="@string/remove_selection" /> <HorizontalScrollView android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/remove_selection" android:layout_marginTop="18dp" android:scrollbars="none" > <LinearLayout android:id="@+id/mygallery" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > </LinearLayout> </HorizontalScrollView> <com.viewpagerindicator.TitlePageIndicator android:id="@+id/titles_pager" android:layout_below="@id/scroll" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/titles_pager" /> </RelativeLayout> </ScrollView> 

What this gives me (the action ends here and, as shown, ViewPagerIndicator does not display anything): http://i.imgur.com/vRa1O.jpg (External link, cannot post photos, sorry!)

+4
source share
3 answers

Okay, so in the end there was no problem, only I made mistakes.

As already mentioned, the ViewPagerIndicator was just white on white, so I donโ€™t see it, now itโ€™s good.

For ViewPager, this was only at the very end of my ScrollView, which means that there was no โ€œrealโ€ screen space for it, so android:layout_height="match_parent" was ineffective. This seems like a problem with ViewPager, I will see what I can do to adapt this.

Anyway, thanks for the help, I hope this post can be useful to someone!

+3
source

I had the same problem, I could not see the tab indicator. So, the problem is that I did not copy the style.xml file in the project from the ViewPagerIndicator project. Hope this can help someone in the future.

+3
source

Faced with the same problem and came up with a solution for it. Maybe a little spike, but it works) First, in a set of xml files

 <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_below="@+id/tabs"/> 

After creating a custom listener to check the fragment size inside the ViewPager

 public interface ViewPagerListener { void onChange(int height); } 

Embed it in your snippet

 public class FriendProfileTabstFragment extends AbstractFragment implements ViewPagerListener .... pager = (ViewPager) fragmentView.findViewById(R.id.pager); adapter = new TabsPagerAdapter(getChildFragmentManager(),user,this); 

And we implement the onChange method, where you can set the height of the layout

 @Override public void onChange(int height) { RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) pager.getLayoutParams(); params.height = height; pager.setLayoutParams(params); } 

Inside your fragment of your child you can get layout_height

 View fragmentView = inflater.inflate(R.layout.tab_without_list_fragment, null); userRecordsList = (LinearLayout) fragmentView.findViewById(R.id.user_records_list); 

when you fill the view, get the height of the layout

 userRecordsList.measure(userRecordsList.getWidth(),userRecordsList.getHeight()); int height = userRecordsList.getMeasuredHeight(); listener.onChange(height); 

Hope this help. Please correct me if you find a better solution.

+1
source

Source: https://habr.com/ru/post/1435234/


All Articles