Add Librarry In build.gradle using app And Import Library
compile 'com.android.support:design:26.+'
MainActivity.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:orientation="vertical"
tools:context="com.example.bhaumik.viewpagerimageslider.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager_slider"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Slider_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/slider_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
package com.example.bhaumik.viewpagerimageslider;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager_slider);
viewPager.setAdapter(new SliderAdapter(MainActivity.this));
}
}
SliderAdapter.java
package com.example.bhaumik.viewpagerimageslider;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
* Created by Bhaumik on 8/28/2017.
*/
public class SliderAdapter extends PagerAdapter{
Context context;
int[] img = {R.drawable.m1,R.drawable.m2,R.drawable.m3,
R.drawable.m4,R.drawable.m5,R.drawable.m6};
public SliderAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return img.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.slider_item,container,false);
imageView = itemView.findViewById(R.id.slider_image_view);
imageView.setImageResource(img[position]);
Log.d("tag","Image Position -> " + img[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
0 comments:
Post a Comment