[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?