1. Download our Official Android App: Forums for Android!

Apps Viewpager

Discussion in 'Android Development' started by mini139, Feb 13, 2019.

  1. mini139

    mini139 Newbie
    Thread Starter
    Rank:
     #816
    Points:
    15
    Posts:
    26
    Joined:
    Apr 9, 2013

    Apr 9, 2013
    26
    0
    15
    How to implement viewpager for textview. I am trying to slide the textview horizontally on a button click. Please provide a link with example.
     

    Advertisement

  2. Alexandr0617

    Alexandr0617 Lurker
    Rank:
    None
    Points:
    6
    Posts:
    9
    Joined:
    Jan 18, 2019

    Jan 18, 2019
    9
    1
    6
    Hello!

    activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:eek:rientation="vertical"
    tools:context="com.androidmads.customurlscheme.MainActivity">

    <android.support.v4.view.ViewPager android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v4.view.ViewPager>

    </LinearLayout>

    MainActivity.java
    .....
    ViewPager viewPager = findViewById(R.id.viewpager);
    PagerAdapter pagerAdapter = new PagerAdapter() {
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);

    @override
    public int getCount() {
    return 5;
    }

    @override
    public Object instantiateItem(ViewGroup container, int position) {
    View view =layoutInflater.inflate(R.layout.textview_layout, container, false);
    ((TextView)view).setText(position+"");
    container.addView(view);
    return view;
    }

    @override
    public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View)object);
    }

    @override
    public boolean isViewFromObject(View view, Object object) {
    return view == object;
    }
    };
    viewPager.setAdapter(pagerAdapter);
    ...
     

Share This Page

Loading...