Skip to content

Instantly share code, notes, and snippets.

@makunomark
Last active August 22, 2017 10:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makunomark/c155e2a9cb63fb839db790422a01709a to your computer and use it in GitHub Desktop.
Save makunomark/c155e2a9cb63fb839db790422a01709a to your computer and use it in GitHub Desktop.
Android Banner View with ViewPager
...
CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;
mCustomPagerAdapter = new CustomPagerAdapter(Home.this, imageList);
mViewPager = (ViewPager) findViewById(R.id.pager);
CirclePageIndicator circlePageIndicator = (CirclePageIndicator) findViewById(R.id.titles);
if (mViewPager != null && circlePageIndicator != null) {
mViewPager.setAdapter(mCustomPagerAdapter);
circlePageIndicator.setViewPager(mViewPager);
}
Timer timer = new Timer();
timer.schedule(new RemindTask(imageList.size(), mViewPager), 0, 3000);
...
//include as inner class
class RemindTask extends TimerTask {
private int numberOfPages;
private ViewPager mViewPager;
private int page = 0;
public RemindTask(int numberOfPages, ViewPager mViewPager) {
this.numberOfPages = numberOfPages;
this.mViewPager = mViewPager;
}
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (page > numberOfPages) { // In my case the number of pages are 5
mViewPager.setCurrentItem(0);
page = 0;
} else {
mViewPager.setCurrentItem(page++);
}
}
});
}
}
...
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/primary_text" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/titles"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:layout_gravity="center|bottom"
android:layout_weight="8"
android:padding="8dip" />
</FrameLayout>
...
...
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
...
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
...
}
...
public class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
List<String> mResources;
public CustomPagerAdapter(Context context, List<String> mResources) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mResources = mResources;
}
@Override
public int getCount() {
return mResources.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
Picasso.with(mContext).load(mResources.get(position)).into(imageView);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/paris" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment