Adding viewpager as scrollable title to listview

I am trying to add a Viewpager (using the support library4) to the list header, but it does not display anything. this is my code please help. It will not be in the list header as an element, so this should not be a problem.

public class CustomActivity extends ListActivity { private static int NUM_AWESOME_VIEWS = 20; private Context cxt; private CustomPageAdapter pageAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] items = { "this", "is", "my", "list", "view", "data" }; this.cxt = this; ViewPager viewPager = new ViewPager(this); pageAdapter = new CustomPageAdapter(); viewPager.setAdapter(pageAdapter); viewPager.requestLayout(); getListView().addHeaderView(viewPager); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)); } private class CustomPageAdapter extends PagerAdapter{ @Override public int getCount() { return NUM_AWESOME_VIEWS; } /** * Create the page for the given position. The adapter is responsible * for adding the view to the container given here, although it only * must ensure this is done by the time it returns from * {@link #finishUpdate()}. * * @param container The containing View in which the page will be shown. * @param position The page position to be instantiated. * @return Returns an Object representing the new page. This does not * need to be a View, but can be some other container of the page. */ @Override public Object instantiateItem(View collection, int position) { TextView tv = new TextView(cxt); tv.setText("Bonjour PAUG " + position); tv.setTextColor(Color.WHITE); tv.setTextSize(20); ((ViewPager) collection).addView(tv,0); return tv; } /** * Remove a page for the given position. The adapter is responsible * for removing the view from its container, although it only must ensure * this is done by the time it returns from {@link #finishUpdate()}. * * @param container The containing View from which the page will be removed. * @param position The page position to be removed. * @param object The same object that was returned by * {@link #instantiateItem(View, int)}. */ @Override public void destroyItem(View collection, int position, Object view) { ((ViewPager) collection).removeView((TextView) view); } @Override public boolean isViewFromObject(View view, Object object) { return view==((TextView)object); } /** * Called when the a change in the shown pages has been completed. At this * point you must ensure that all of the pages have actually been added or * removed from the container as appropriate. * @param container The containing View which is displaying this adapter's * page views. */ @Override public void finishUpdate(View arg0) { } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } } } 
+8
android
source share
1 answer

listview header and footer after setadapter.

if you try the setadapter and don't see the viewpager. check the width and height of the viewpager.

if the value of the width or height of the image in the Viewpager is 0. create a LienarLayout (or anyViewGroup) and the viewview viewpager in the viewgroup with the layout parameter includes setting the width and height. and addHeader.

my English skill is not good; if you don’t understand, add a comment to me.

I hope for help.

0
source share

All Articles