http://demonuts.com/2017/03/17/image-slider-with-slideshow-using-viewpager-android/ https://www.androidtutorialpoint.com/basics/android-image-slider-tutorial/
Last active
April 30, 2020 15:06
-
-
Save sagar2093/bea722492e803097de4180d822a05b8f to your computer and use it in GitHub Desktop.
Image Slider Android using viewpager and pagerAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//inside dependency section | |
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1' | |
compile 'com.jakewharton:butterknife:8.7.0' | |
annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/viewPager" | |
android:layout_width="match_parent" | |
android:layout_height="200dp" | |
android:layout_alignParentTop="true" /> | |
<com.viewpagerindicator.CirclePageIndicator | |
android:id="@+id/indicator" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_centerHorizontal="true" | |
android:gravity="bottom" | |
android:padding="10dip" | |
app:centered="true" | |
app:fillColor="#df0623" | |
app:pageColor="#fff" | |
app:snap="false" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImageSlider { | |
private String name; | |
//optional @DrawableRes | |
@DrawableRes | |
private int resId; | |
public ImageSlider(String name, int resId) { | |
this.name = name; | |
this.resId = resId; | |
} | |
@Override | |
public String toString() { | |
return name; | |
} | |
//getters and setters | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImageSliderAdapter extends PagerAdapter { | |
private Context mContext; | |
private List<ImageSlider> imageList; | |
private LayoutInflater inflater; | |
public ImageSliderAdapter(Context context, List<ImageSlider> list) { | |
mContext = context; | |
imageList = list; | |
inflater = LayoutInflater.from(context); | |
} | |
@Override | |
public Object instantiateItem(ViewGroup collection, int position) { | |
ViewGroup imageLayout = (ViewGroup) inflater.inflate(R.layout.slider_home, collection, false); | |
((ImageView) imageLayout.findViewById(R.id.imageView)).setImageResource(imageList.get(position).getResId()); | |
collection.addView(imageLayout); | |
return imageLayout; | |
} | |
@Override | |
public void destroyItem(ViewGroup collection, int position, Object view) { | |
collection.removeView((View) view); | |
} | |
@Override | |
public int getCount() { | |
return CustomPagerEnum.values().length; | |
} | |
@Override | |
public boolean isViewFromObject(View view, Object object) { | |
return view == object; | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
return imageList.get(position).getName(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeFragment extends BaseFragment { | |
@BindView(R.id.viewPager) | |
ViewPager viewPager; | |
public static HomeFragment newInstance() { | |
HomeFragment frag = new HomeFragment(); | |
return frag; | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_home, container, false); | |
unbinder = ButterKnife.bind(this, view); | |
setTitle(R.string.app_name_default); | |
return view; | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
initImageSlider(view); | |
} | |
private List<ImageSlider> getImageList(){ | |
List<ImageSlider> imageList = new ArrayList<>(); | |
imageList.add(new ImageSlider("Logo",R.drawable.logo)); | |
imageList.add(new ImageSlider("Steve aoki",R.drawable.steve_aoki_live)); | |
imageList.add(new ImageSlider("Dancellenium",R.drawable.dancellenium)); | |
return imageList; | |
} | |
int currentPage = 0; | |
int NUM_PAGES = 0; | |
private void initImageSlider(View view){ | |
//Set the pager with an adapter | |
viewPager.setAdapter(new ImageSliderAdapter(mContext,getImageList())); | |
CirclePageIndicator indicator = (CirclePageIndicator) | |
view.findViewById(R.id.indicator); | |
indicator.setViewPager(viewPager); | |
final float density = getResources().getDisplayMetrics().density; | |
//Set circle indicator radius | |
indicator.setRadius(5 * density); | |
NUM_PAGES =getImageList().size(); | |
// Auto start of viewpager | |
final Handler handler = new Handler(); | |
final Runnable Update = new Runnable() { | |
public void run() { | |
if (currentPage == NUM_PAGES) { | |
currentPage = 0; | |
} | |
viewPager.setCurrentItem(currentPage++, true); | |
} | |
}; | |
Timer swipeTimer = new Timer(); | |
swipeTimer.schedule(new TimerTask() { | |
@Override | |
public void run() { | |
handler.post(Update); | |
} | |
}, 3000, 3000); | |
// Pager listener over indicator | |
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { | |
@Override | |
public void onPageSelected(int position) { | |
currentPage = position; | |
} | |
@Override | |
public void onPageScrolled(int pos, float arg1, int arg2) { | |
} | |
@Override | |
public void onPageScrollStateChanged(int pos) { | |
} | |
}); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<ImageView | |
android:id="@+id/imageView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_gravity="center" | |
android:adjustViewBounds="true" | |
android:contentDescription="@string/featured_image" | |
android:scaleType="fitXY" | |
android:src="@drawable/logo" /> | |
</FrameLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment