ViewPagers in ListView showing empty list items

[EDIT] : Thanks for all the informative answers, now the problem is solved, thanks to your help. Similar problem: Android: I don't have ViewPager WRAP_CONTENT

I am trying to implement the user interface of my application: I want a ListView with a ViewPager in each row.

Here are my files:

MainActivity.java

 package com.condi; import android.app.ListActivity; import android.os.Bundle; import android.view.Menu; public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setListAdapter(new CardListAdapter(this, getFragmentManager())); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } } 

CardListAdapter.java

 package com.condi; import java.util.List; import android.app.FragmentManager; import android.content.Context; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; public class CardListAdapter extends BaseAdapter { private Context context; private FragmentManager fragmentManager; private List<Profile> profiles; CardListAdapter(Context context, FragmentManager fragmentManager) { this.context = context; this.fragmentManager = fragmentManager; this.profiles = new DatabaseHelper(context).getProfiles(); } @Override public int getCount() { return profiles.size(); } @Override public Profile getItem(int position) { return profiles.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = View.inflate(context, R.layout.profile_card, null); CardPagerAdapter mCardPagerAdapter = new CardPagerAdapter( fragmentManager); ViewPager viewPager = (ViewPager) convertView.findViewById(R.id.pager); viewPager.setAdapter(mCardPagerAdapter); viewPager.setId(R.id.pager); return convertView; } } 

profile_card.xml (the problem arose from wrap_content ).

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="4dp" android:background="@drawable/card_background" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </FrameLayout> 

CardPagerAdapter.java

 package com.condi; import android.app.Fragment; import android.app.FragmentManager; import android.support.v13.app.FragmentPagerAdapter; public class CardPagerAdapter extends FragmentPagerAdapter { public CardPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new Fragment1(); case 1: return new Fragment2(); } return null; } @Override public int getCount() { return 2; } } 

Fragment1.java

 package com.condi; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { public Fragment1() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.preference_category, container, false); } } 

Fragment2.java

 package com.condi; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { public Fragment2() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.preference_category, container, false); } } 

The database in which the profiles are presented works well. The problem is that I get an empty list with the correct number of items, but the ViewPager is not displayed at all. screenshot of my application

What am I doing wrong?

+7
java android xml listview android-viewpager
source share
6 answers

Try changing the height of the ViewPager in xml. wrap_content does not work.

+5
source share

To make it clear, its a bad practice to use viewers as a list, as this design amazes ui performance. The best way to deal with this problem:

  • Make a manual infusion of viewers and add to the layout.

  • Add a unique identifier to each view so that it is uniquely identified by the system.

  • Extend the custom viewpager and onmeasure () to define the child’s layout that is bloated on the selected page. You can do this by setting a callback in your custom view and call the callback from the viewpagers onPageScrolled listener, passing the position to define the child layout.
  • In the onMeaure () method, measure the height of the layout of the child and set it as the height of the viewpagers using super.onMeasure (), which passes the new measured parameters.

Key points to note:

NB Set a unique identifier for viewing that is inflated by you.

+2
source share

try this problem in layout

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="4dp" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="4dp" android:background="@drawable/card_background" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </FrameLayout> 
+2
source share

Bad practice of using wrap_content for your viewer height. You can use match_parent or static dp to height your viewpager.

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="4dp" android:background="@drawable/card_background" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </FrameLayout> 

If you need to use wrap_content to height your viewpager, you can override your viewpager onMeasure() and calculate the height. Here is an example below about it.

Android: I don't have ViewPager WRAP_CONTENT

Hope this helps you.

+1
source share

ViewPager does not support wrap_content, because it stands now, because it does not load all of its children at the same time, that is, it cannot receive the corresponding dimension. You must fix the height in your xml or create your own ViewPager (read more on here ) and use it in your xml

0
source share

Try using a fixed height ViewPager inside a ListView . ViewPager does not support wrap_content

0
source share

All Articles