简体   繁体   中英

Android ViewPager - Using a layout xml

I have a simple ViewPager example that just places a TextView with the current position. I want to adapt this to work in an existing app I have written.

My app currently uses a layout and then adjusts the values as needed. I want to use this layout with the ViewPager, but I currently am unable to get the layout.

When constructing my PagerAdapter I pass in the context from the main activity.

I am unable to use findViewById to get my layout though.

Can someone suggest the correct way to get it?

public Object instantiateItem(View collection, int position)
{
    LinearLayout info = (LinearLayout)((Activity)_context).findViewById(R.id.linearlayout);

    ((ViewPager) collection).addView(info, 0);

    return info;
}

The code above is from my PagerAdapter. I have a layout that I want to use as the pages for the pager, which I want to populate with info into the layouts TextViews and other views...

I would pass a LayoutInflater to the adapter and then on the instantiateItem, I would create the View as I wished.

Even if it doesn't compile, could you post the code?

And btw have you check out this implementation ? It helped me once because I was forgetting the line

   ((ViewPager) collection).addView( some view...,0);

in the instantiateItem method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM