Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ViewPager not showing correct View
- private ViewPager mBandsPager;
- private BandsPagerAdapter mBandsPagerAdapter;
- private static final String TAG = "Paging";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.awesomepager);
- // Build a couple bands;
- ArrayList<Band> bands = new ArrayList<Band>();
- bands.add(new Band(1, "Led Zeppelin"));
- bands.add(new Band(37, "Brand New"));
- bands.add(new Band(49, "Jay-Z"));
- mBandsPagerAdapter = new BandsPagerAdapter(this, bands);
- mBandsPager = (ViewPager) findViewById(R.id.awesomepager);
- mBandsPager.setAdapter(mBandsPagerAdapter);
- }
- private Context mCtx;
- private ArrayList<Band> mBands;
- public BandsPagerAdapter(Context ctx, ArrayList<Band> bands) {
- mCtx = ctx;
- mBands = bands;
- }
- @Override
- public int getCount() {
- return mBands.size();
- }
- @Override
- public Object instantiateItem(View collection, int position) {
- // Inflate and create the view
- LayoutInflater layoutInflater = (LayoutInflater)mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = layoutInflater.inflate(R.layout.featured_bands_layout, null);
- // Build UI
- TextView name = (TextView)collection.findViewById(R.id.featured_band_name);
- // Populate UI with band data
- Band band = mBands.get(position);
- if(name != null) {
- name.setText("Name: " + band.getName() + ", Pos: " + position);
- }
- // Add View to the ViewPager collection
- ((ViewPager)collection).addView(view,0);
- return view;
- }
- @Override
- public void destroyItem(View collection, int position, Object view) {
- ((ViewGroup) collection).removeView((View)view);
- }
- @Override
- public boolean isViewFromObject(View view, Object object) {
- return view==((View)object);
- }
- @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) {}
- public class BandsPagerAdapter extends PageAdaptor{
- private ArrayList<Band> mBands;
- public BandsPagerAdapter(ArrayList<Band> bands) {
- mBands = bands;
- }
- @Override
- public int getCount() {
- return mBands.size();
- }
- @Override
- public Object instantiateItem(View collection, int position) {
- // Inflate and create the view
- LayoutInflater layoutInflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = layoutInflater.inflate(R.layout.featured_bands_layout, null);
- // Build UI
- TextView name = (TextView) view.findViewById(R.id.featured_band_name);
- // Populate UI with band data
- Band band = mBands.get(position);
- if(name != null) {
- name.setText("Name: " + band.getName() + ", Pos: " + position);
- }
- // Add View to the ViewPager collection
- ((ViewPager) collection).addView(view,0);
- return view;
- }
- @Override
- public void destroyItem(View collection, int position, Object view)
- ((ViewPager) collection).removeView((View) view);
- }
- @Override
- public boolean isViewFromObject(View view, Object object) {
- return view == ((View) object);
- }
- @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) {}
- }
Add Comment
Please, Sign In to add comment