How We Can Make viewPager Slider

How We Can Make viewPager Slider

viewPager Slider Android

First Create New Empty Project.

  • After That Create This Code In activity_main.XML  
<android.support.v4.view.ViewPager   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/viewPager">
</android.support.v4.view.ViewPager>
  • After That Create New Layout Page Controler.
<ImageView
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/image"
   android:layout_gravity="center_horizontal"/>
  • After That Create The New Java Page Custom Adapter
public class CustomAdapter extends PagerAdapter
{
   int [] image={R.drawable.football_1,R.drawable.Cricket,R.drawable.Hockey};
   LayoutInflater inflater;
   Context context;
   CustomAdapter (Context context)
   {
       this.context=context;
   }
   @Override
   public int getCount() {
       return image.length;
   }
   @Override
   public boolean isViewFromObject(View view, Object object) {
       return (view==object);
   }
   @Override
   public Object instantiateItem(ViewGroup container, int position)
   {
       inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       View view=inflater.inflate(R.layout.swipe,container,false);
       ImageView imageView=view.findViewById(R.id.image);
       imageView.setImageResource(image[position]);
       container.addView(view);
       return view;
   }
   @Override
   public void destroyItem(ViewGroup container, int position, Object object) {

       container.invalidate();
   }
}
  • After That Create MainActivity.java code
public class MainActivity extends AppCompatActivity {
   CustomAdapter customAdapter;
   ViewPager viewPager;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       viewPager=findViewById(R.id.viewPager);
       customAdapter=new CustomAdapter(this);
       viewPager.setAdapter(customAdapter);
   }
}
public class MainActivity extends AppCompatActivity {
   CustomAdapter customAdapter;
   ViewPager viewPager;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       viewPager=findViewById(R.id.viewPager);
       customAdapter=new CustomAdapter(this);
       viewPager.setAdapter(customAdapter);
   }
}
About the author
Code solution

info@codesolution.co.in

Discussion
  • 0 comments

Add comment To Login
Add comment