Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, 13 May 2016

Auto Scroll ViewPager

Hello guys in this blog we are learn how to scroll automatically scroll viewpager without create any custom viewpager class.

activity_main.xml


<LinearLayout 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="com.samset.MainActivity">

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

</LinearLayout>

MainActivity.java


public class MainActivity extends AppCompatActivity {
   private MyViewPagerAdapter  adapter;
   private int count = 0;
   private Timer timer;

private ViewPager viewpager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         init();
   
    }
    private void init() {
        viewPager = (ViewPager)findViewById(R.id.pager_;
       adapter=new MyViewPagerAdapter(this);
        viewPager.setAdapter(recentItemAdapter);


 // Timer for auto sliding
   
timer  = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(count<=5){
                        viewPager.setCurrentItem(count);
                        count++;
                    }else{
                        count = 0;
                        viewPager.setCurrentItem(count);
                    }
                }
            });
        }
    }, 500, 3000);
}

    }

}

MyViewPagerAdapter.java


/**
 * Created by samset on 13/01/16.
 */
public class MyViewPagerAdapter extends PagerAdapter {
    private int ITEM_1[]={R.drawable.r_f,R.drawable.r_s,R.drawable.r_th,
            R.drawable.r_five,R.drawable.r_si,R.drawable.r_seven,R.drawable.r_five,R.drawable.r_s,
            R.drawable.r_f,R.drawable.r_si,R.drawable.r_seven,R.drawable.r_th,};
    private String ITEM_2[]={"T-Shirt ponty","T-Shirt puma","Top ponty","T-Shirt ponty",
            "Shirt puma","T-Shirt white","T-Shirt ponty","T-Shirt pink",
            "T-Shirt black","T-Shirt blue","T-Shirt white","T-Shirt ponty"};
    Context mContext;
    LayoutInflater mLayoutInflater;

    public MyViewPagerAdapter(Context ctx) {
        this.mContext=ctx;
        mLayoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.e("Adapter""Adapter Step 1");
    }


    @Override
    public int getCount() {
        return ITEM_1.length;
    }

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

    @Override
    public float getPageWidth(int position) {
        return 0.4f;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.item, container, false);
        Log.e("Adapter""Adapter Step 2");
        ImageView imageView= (ImageView) itemView.findViewById(R.id.img_item);
        imageView.setBackgroundResource(ITEM_1[position]);
        TextView itemname=(TextView)itemView.findViewById(R.id.tv_name);
        itemname.setText(ITEM_2[position]);



        container.addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
        Log.e("Adapter""Adapter Step 3");
    }


}

item.xml


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

         <RelativeLayout
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/img_item"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerHorizontal="true"
                android:background="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/img_recentitem"
                android:layout_centerHorizontal="true"
                android:maxLines="2"
                android:padding="5dp"
                android:text="Item" />
        </RelativeLayout>

</RelativeLayout>


Thank you

ViewPager sample




No comments:

Post a Comment