• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to make TextView inside ViewPager scroll vertically

polygrimm

Lurker
I've got a ViewPager and a TextView inside it. When content of TextView is larger then it is visible on screen, there shall be possibility to scroll it vertically. But it does not do this automatically :(

Here is an xml for TextView

Code:
   <?xml version="1.0" encoding="utf-8"?>
    <TextView 
        android:id="@+id/txText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#338877"
        android:padding="15dp"
        android:scrollHorizontally="false"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:scrollbars="vertical"
        android:textAppearance="?android:attr/textAppearanceMedium" />
And here is an xml for ViewPager

Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/vpPonkSwiper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/lbPonkFooter" />

    <TextView
        android:id="@+id/lbPonkFooter"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Footer" />
</RelativeLayout>

Here is an Adapter:

Code:
public class PonkPageAdapter extends PagerAdapter
{
    private int             pagesCount  = -1;
    private List<Ponk>      data;
    private App             app;
    private MockPersistence db;
    private Activity        activity;
    private TextView        currentView;
    private LayoutInflater  inflater;

    public PonkPageAdapter(Activity activity, byte section) {
        this.activity = activity;
        this.app = ((App) activity.getApplication());
        this.db = app.getPersistence();
        this.section = section;
        this.data = db.findPonksBySection(section); // new LinkedList<Ponk>();
        this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //
        this.pagesCount = db.countPonksInSection(section);
    }

    @Override
    public int getCount()
    {
        return pagesCount;
    }

    @Override
    public Object instantiateItem(View collection, int position)
    {
        TextView tv = getNewView();
        Ponk j = data.get(position);
        tv.setText(j.text);
        tv.setTag(j.id);
        setFontSizeInSP(tv, app.getFontSize());
        tv.setTag(position);
        ((ViewPager) collection).addView(tv, 0);

        return tv;
    }

    private TextView getNewView()
    {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         TextView tv = (TextView)inflater.inflate(R.layout.Ponk, null);
        return tv;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object)
    {
        currentView = (TextView) object;
    }

    @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;
    }

    @Override
    public Parcelable saveState()
    {
        return null;
    }
}
 
Do you want the text view to only be part of a single "page" of the view pager, or be there for every page?
 
Do you want the text view to only be part of a single "page" of the view pager, or be there for every page?
I am sorry, not sure if I understand your question.
To be more specific, let's say
1) ViewPager will have 5 pages in total
2) Each page can have 100..1024 chars
3) If text is larger then 500 chars (for example), User shall be able to scroll this text vertically. But if user swipes horizontally then viewpager shall act as usual - go to next/prev page
 
Ok, so you want it for each page.

In that case, you don't put the TextView in the same layout as the ViewPager, but you'd put a TextView inside a ScrollView in each page's layout. I use Fragments (via the support library) for pages, as it makes things easier and streamlines the process, but you don't necessarily have to.
 
No, because even though it would make things easy for you, me giving you the code would prevent you from learning and understanding exactly what's going on, and in the end, that is what is most important. Instead, I will provide some links on how ViewPagers and ScrollViews work and you should be able to figure it out from there. If you still can't figure it out, then you should probably take a step back and start with something simpler.

Links:
ViewPager | Android Developers

Thomas Amsler: Android: ViewPager and Fragments

PagerAdapter | Android Developers

FragmentPagerAdapter | Android Developers

ScrollView | Android Developers
 
Back
Top Bottom