ab11
ab11

Reputation: 20090

Android: ViewPager does not respect WRAP_CONTENT?

I would like to create a ViewPager whose width wrap's to its contents, and is centered horizontally in it's parent. The first code snippet uses a LinearLayout to create this effect, as shown in the first screenshot. The second code snippet is my attempt to do this with a ViewPager instead of the LinearLayout, but the result is not the desired behavior, as shown in the second screenshot.

Any suggestions as to how I create the first effect, but using a ViewPager?

  @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        textView = new TextView(this);
        textView.setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        textView.setText("abcabcabcabcabc");
        textView.setBackgroundColor(Color.YELLOW);

        LinearLayout llayout = new LinearLayout(this);
        llayout.setBackgroundColor(Color.BLUE);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        llayout.setLayoutParams(layoutParams);
        llayout.addView(textView);

        layout = new RelativeLayout(this);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        layout.setBackgroundColor(Color.GREEN);
        layout.addView(llayout);

        setContentView(layout);
    }


@Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        textView = new TextView(this);
        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT, ViewPager.LayoutParams.WRAP_CONTENT));
        textView.setText("abcabcabcabcabc");
        textView.setBackgroundColor(Color.YELLOW);

        ViewPager pager = new ViewPager(this);
        pager.setBackgroundColor(Color.BLUE);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        pager.setLayoutParams(layoutParams);
        pager.setAdapter(new ViewPagerAdapter());

        layout = new RelativeLayout(this);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        layout.setBackgroundColor(Color.GREEN);
        layout.addView(pager);

        setContentView(layout);
    }

    class ViewPagerAdapter extends PagerAdapter
    {
        @Override
        public int getCount() 
        {
            return 1;
        }

        public Object instantiateItem(ViewGroup collection, int position) 
        {
            collection.addView(textView, 0);
            return textView;
        }

        @Override
        public void destroyItem(ViewGroup collection, int position, Object view) 
        {
            collection.removeView((View) view);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) 
        {
            return (view==object);
        }

        @Override
        public void finishUpdate(ViewGroup arg0) {}


        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}

        @Override
        public Parcelable saveState() 
        {
            return null;
        }

        @Override
        public void startUpdate(ViewGroup arg0) {}
    }

enter image description here

enter image description here

Upvotes: 1

Views: 1816

Answers (1)

Morrison Chang
Morrison Chang

Reputation: 12121

If you look at your code for where Adapter called instantiateItem()

public Object instantiateItem(ViewGroup collection, int position) 
{
    collection.addView(textView, 0)
    return textView;
}

You are returning a TextView to be the page and the ONLY thing that you want to show.

The relevant part of the documentation is here: http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html

A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int) after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.

Create a View with the layout that you want and return it there to have the effect you desire.

See the bottom of: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

Upvotes: 1

Related Questions