RvewX | ريفيو اكس

Android ViewPager Example Tutorial

Tutorial

Format supervisor that enables the client to flip left and directly through pages of information. You supply a usage of a PagerAdapter to create the pages that the view appears.

ViewPager is frequently utilized related to Fragment, which is an advantageous method to supply and deal with the lifecycle of each page. There are standard connectors actualized for utilizing sections with the ViewPager, which spread the most widely recognized use cases. These are FragmentPagerAdapter and FragmentStatePagerAdapter; every one of these classes have straightforward code telling the best way to construct a full UI with them.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

item_view_pager .xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="TODO"
        android:scaleType="fitXY" />
    
</LinearLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Integer> images = new ArrayList<>();
        images.add(R.drawable.image1);
        images.add(R.drawable.image2);
        images.add(R.drawable.image3);

        ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(new ViewPagerAdapter(MainActivity.this, images));
    }

    protected class ViewPagerAdapter extends PagerAdapter {
        private List<Integer> imageList;
        private Context context;

        ViewPagerAdapter(Context context, List<Integer> images) {
            this.context = context;
            imageList = images;

        }

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            LayoutInflater inflater = LayoutInflater.from(context);
            ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.item_view_pager, container,
                    false);
            ImageView imageView = layout.findViewById(R.id.imageView);
            imageView.setImageResource(imageList.get(position));

            container.addView(layout);
            return layout;
        }

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

Get the full example Github

Leave a Reply

×