It is a dot-shaped pagination that is common in iOS. It is not provided as standard on Android, so you need to create it or use a library. Here, it is called page control after iOS, but in Android, there are many things named Indicator, and a famous library <a href"https://github.com/JakeWharton/ViewPagerIndicator"> ViewPageIndicator > And <a href+"https://github.com/ongakuer/CircleIndicator"> CircleIndicator . However, these libraries could only be used with ViewPager, so I created my own custom view.
ViewPager in RecyclerView by using PagerSnapHelper that can be used with SupportLibrary25.1.0 or higher You can achieve the same behavior. This is useful when you want to avoid ViewPager in ViewPager.
MainActivity.java
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
If you cannot use PagerSnapHelper due to version, the following article will show you how to make snaps. http://qiita.com/hotpepsi/items/1167e5263f048a005e05
There are two types, default and selected.
res/drawable/shape_page_control_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFFFFF" />
</shape>
res/drawable/shape_page_control_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#B7B7B7" />
</shape>
Create a custom view that does the following two things:
--Create as many dots as there are elements displayed by RecyclerView. ――When you snap, the dots also switch in tandem.
When switching dots in conjunction with snapping, the position of the element currently snapped is required, so it is acquired using `findLastVisibleItemPosition ()`
of LinerLayoutManager.
PageControlView.java
public class PageControlView extends LinearLayout {
private ImageView[] imageViews;
public PageControlView(Context context) {
this(context, null);
}
public PageControlView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public PageControlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setRecyclerView(RecyclerView recyclerView, final LinearLayoutManager layoutManager) {
int itemCount = layoutManager.getItemCount();
if (itemCount < 1) {
return;
}
createPageControl(itemCount);
//Switch dots as the RecyclerView scrolls
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int position = layoutManager.findLastVisibleItemPosition();
int itemCount = layoutManager.getItemCount();
for (int i = 0; i < itemCount; i++) {
imageViews[i].setImageResource(R.drawable.shape_page_control_default);
}
imageViews[position].setImageResource(R.drawable.shape_page_control_selected);
}
});
}
//Create as many dots as there are items in RecyclerView
private void createPageControl (int itemCount) {
imageViews = new ImageView[itemCount];
for (int i = 0; i < imageViews.length; i++) {
imageViews[i] = new ImageView(getContext());
imageViews[i].setImageResource(R.drawable.shape_page_control_default);
//Adjust dot size and spacing(The gif image at the top is 10dp)
int dotSize = getResources().getDimensionPixelSize(R.dimen.page_control_dot_size);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dotSize, dotSize);
params.setMargins(dotSize / 2, 0, dotSize / 2, 0);
imageViews[i].setLayoutParams(params);
addView(imageViews[i]);
}
imageViews[0].setImageResource(R.drawable.shape_page_control_selected);
}
}
Arrange it like a normal view.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context="com.hogehoge.fugafuga.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.hogehoge.fugafuga.PageControlView
android:id="@+id/page_control_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_gravity="center"/>
</LinearLayout>
MainActivity.java
PageControlView pageControlView = (PageControlView) findViewById(R.id.page_control_view);
pageControlView.setRecyclerView(recyclerView, layoutManager);
You can also use it with ViewPager by rewriting the setRecyclerView method as follows. This is easier because the listener has a position.
PageControlView.java
public void setViewPager (final ViewPager viewPager) {
int itemCount = viewPager.getAdapter().getCount();
if (itemCount < 1) {
return;
}
createPageControl(itemCount);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
int itemCount = viewPager.getAdapter().getCount();
for (int i = 0; i < itemCount; i++) {
imageViews[i].setImageResource(R.drawable.shape_page_control_default);
}
imageViews[position].setImageResource(R.drawable.shape_page_control_selected);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
Recommended Posts