Skip to content
This repository has been archived by the owner on Apr 19, 2018. It is now read-only.

Are fragments required? PageAdapter not working #38

Closed
funston opened this issue Dec 17, 2011 · 4 comments
Closed

Are fragments required? PageAdapter not working #38

funston opened this issue Dec 17, 2011 · 4 comments

Comments

@funston
Copy link

funston commented Dec 17, 2011

I've tried to use the pageadapter and it just shows blank views. I have the examples compiling fine, but switching to use a PagerAdapter to return the view for the position is creating a valid view, yet I get a blank page...

@JakeWharton
Copy link
Owner

Fragments are not required. In fact, ViewPagerIndicator doesn't care what's in the actual pager at all. You could use anything. Fragments, views, or whatever other fancy whowhatsit you can shove into a PagerAdapter.

I personaly have never used ViewPager with only views. In the eyes of ViewPager, fragments are just views passed in from a FragmentPagerAdapter. Try looking at its source to see if it gives you any clue. Replace it's call to Fragment's createView() with returning a TextView or something obvious. Then work your way backwards from there to strip out anything fragment-related.

If you end up figuring it out I'm sure another sample would be welcome to demonstrate that to others.

@funston
Copy link
Author

funston commented Dec 17, 2011

Will do. I worked back from the example into what is now a mess, but just plain PageAdapter. If I can't make progress in getting something visible, will post the code with a proper issue if there is something not working. Just wanted to make sure before dug in to deep with regular ol' view adapter

@funston funston closed this as completed Dec 17, 2011
@funston funston reopened this Dec 19, 2011
@funston
Copy link
Author

funston commented Dec 19, 2011

The views show up without fragments, but not sure why the tabs aren't showing (w/halo, etc).

package com.viewpagerindicator.sample;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitleProvider;

/**
 * Created by IntelliJ IDEA.
 * User: rich
 * Date: 12/16/11
 * Time: 9:39 PM
 * To change this template use File | Settings | File Templates.
 */
public class SimpleTabActivity extends Activity {
    private static final String[] CONTENT = new String[]{"Recent", "Artists", "Albums", "Songs", "Playlists", "Genres"};

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_tabs);
        PagerAdapter mAdapter = new ImagePagerAdapter();

        ViewPager mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        TabPageIndicator mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
    }


    class ImagePagerAdapter extends PagerAdapter implements TitleProvider {
        public int getCount() {
            return CONTENT.length;
        }

        public String getTitle(int position) {
            return CONTENT[position];
        }

        public Object instantiateItem(View collection, int position) {
            Context c = SimpleTabActivity.this.getApplicationContext();
            LinearLayout l = new LinearLayout(collection.getContext());
            l.setOrientation(LinearLayout.HORIZONTAL);
            l.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                  LinearLayout.LayoutParams.WRAP_CONTENT));
            l.setPadding(0,50,0,0);
            ImageView v = new ImageView(c);
            v.setImageDrawable(c.getResources().getDrawable(R.drawable.icon));
            l.addView(v);
            TextView tv = new TextView(c);
            tv.setText(CONTENT[position]);
            l.addView(tv);
            ((ViewPager) collection).addView(l, 0);
            return l;
        }


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


        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((LinearLayout) 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) {
        }
    }
}

@funston
Copy link
Author

funston commented Dec 20, 2011

I just took this example: http://blog.stylingandroid.com/archives/537 and changed the TitlePage indicator to a TabPage Indicator and see the exact same issue. The halo/text/styles don't work.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants