Hello friends today we are learn about what is ViewPager and how to use ?
ViewPager :- Viewpager is basically use to image gallery flip left and right data.this is best way to see different different pages dynamically.ViewPager also use fragment.
Lets start how to use viewpager .....
activity_main.xml
MainActivity.java
MyViewPagerAdapter.java
item.xml
Thank you
See Live Sample
ViewPager :- Viewpager is basically use to image gallery flip left and right data.this is best way to see different different pages dynamically.ViewPager also use fragment.
Lets start how to use viewpager .....
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>
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 MyViewPagerAdapter adapter;
private ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
@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);
}
}
private void init() {
viewPager = (ViewPager)findViewById(R.id.pager_;
adapter=new MyViewPagerAdapter(this);
viewPager.setAdapter(recentItemAdapter);
}
}
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");
}
}
* 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>
<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
See Live Sample
No comments:
Post a Comment