Beaucoup d’entre vous (comme moi) ont probablement du mal à créer ViewPager
avec des points en bas, comme ceci:
Comment créez-vous un tel ViewPager Android?
Tout ce dont nous avons besoin est: ViewPager , TabLayout et 2 tirables pour les points sélectionnés et par défaut.
Tout d’abord, nous devons ajouter TabLayout
à notre disposition d’écran et le connecter avec ViewPager
. Nous pouvons le faire de deux manières:
TabLayout
imbriqué dans ViewPager
<Android.support.v4.view.ViewPager
Android:id="@+id/photos_viewpager"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<Android.support.design.widget.TabLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"/>
</Android.support.v4.view.ViewPager>
Dans ce cas,
TabLayout
sera automatiquement connecté àViewPager
, maisTabLayout
sera adjacent àViewPager
et non au-dessus.
TabLayout
<Android.support.v4.view.ViewPager
Android:id="@+id/photos_viewpager"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>
<Android.support.design.widget.TabLayout
Android:id="@+id/tab_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"/>
Dans ce cas, nous pouvons mettre
TabLayout
n'importe où, mais nous devons connecterTabLayout
avecViewPager
par programme
ViewPager pager = (ViewPager) view.findViewById(R.id.photos_viewpager);
PagerAdapter adapter = new PhotosAdapter(getChildFragmentManager(), photosUrl);
pager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(pager, true);
Une fois que nous avons créé notre mise en page, nous devons préparer nos points. Nous créons donc trois fichiers: selected_dot.xml
, default_dot.xml
et tab_selector.xml
.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item>
<shape
Android:innerRadius="0dp"
Android:shape="ring"
Android:thickness="8dp"
Android:useLevel="false">
<solid Android:color="@color/colorAccent"/>
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item>
<shape
Android:innerRadius="0dp"
Android:shape="ring"
Android:thickness="8dp"
Android:useLevel="false">
<solid Android:color="@Android:color/darker_gray"/>
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item Android:drawable="@drawable/selected_dot"
Android:state_selected="true"/>
<item Android:drawable="@drawable/default_dot"/>
</selector>
Nous devons maintenant n’ajouter que 3 lignes de code à TabLayout
dans notre structure XML.
app:tabBackground="@drawable/tab_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
Commencez par créer une mise en page, qui donne un LinerLayout for Dots qui apparaît sur votre View Pager
<RelativeLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content">
<Android.support.v4.view.ViewPager
Android:id="@+id/view_pager"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"/>
<LinearLayout
Android:id="@+id/pager_dots"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_alignParentBottom="true"
Android:layout_marginBottom="10dp"
Android:background="@Android:color/transparent"
Android:gravity="center_horizontal"
Android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
Après cela créer 2 drawables
1. Non sélectionné Drawable
<?xml version="1.0" encoding="utf-8"?>
<shape Android:shape="oval" xmlns:Android="http://schemas.Android.com/apk/res/Android">
<solid Android:color="@Android:color/transparent"/>
<size Android:width="12dp" Android:height="12dp"/>
<stroke Android:width="1dp" Android:color="#ffffff"/>
</shape>
2. Sélectionnable
<?xml version="1.0" encoding="utf-8"?>
<shape Android:shape="oval" xmlns:Android="http://schemas.Android.com/apk/res/Android">
<solid Android:color="@Android:color/transparent"/>
<size Android:width="12dp" Android:height="12dp"/>
<stroke Android:width="1dp" Android:color="#000000"/>
</shape>
Après que set adaptateur
private LinearLayout llPagerDots;
private ViewPager viewPager;
private ArrayList<String> eventImagesUrl;
private HomeViewPagerAdapter homeViewPagerAdapter;
private ImageView[] ivArrayDotsPager;
public void setUpViewPager() {
viewPager = (ViewPager) findViewById(R.id.view_pager);
llPagerDots = (LinearLayout) findViewById(R.id.pager_dots);
homeViewPagerAdapter = new HomeViewPagerAdapter(mContext, eventImagesUrl);
viewPager.setAdapter(homeViewPagerAdapter);
setupPagerIndidcatorDots();
ivArrayDotsPager[0].setImageResource(R.drawable.page_indicator_selected);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < ivArrayDotsPager.length; i++) {
ivArrayDotsPager[i].setImageResource(R.drawable.page_indicator_unselected);
}
ivArrayDotsPager[position].setImageResource(R.drawable.page_indicator_selected);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
Créer une méthode setupPagerIndidcatorDots ():
private void setupPagerIndidcatorDots() {
ivArrayDotsPager = new ImageView[eventImagesUrl.size()];
for (int i = 0; i < ivArrayDotsPager.length; i++) {
ivArrayDotsPager[i] = new ImageView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 5, 0);
ivArrayDotsPager[i].setLayoutParams(params);
ivArrayDotsPager[i].setImageResource(R.drawable.page_indicator_unselected);
//ivArrayDotsPager[i].setAlpha(0.4f);
ivArrayDotsPager[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setAlpha(1);
}
});
llPagerDots.addView(ivArrayDotsPager[i]);
llPagerDots.bringToFront();
}
Vous pouvez consulter ma bibliothèque pour traiter votre demande: https://github.com/tommybuonomo/dotsindicator
Dans votre mise en page XML
<com.tbuonomo.viewpagerdotsindicator.DotsIndicator
Android:id="@+id/dots_indicator"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerHorizontal="true"
app:dotsColor="@color/colorPrimary"
app:dotsSize="16dp"
app:dotsWidthFactor="3"
/>
Dans votre code Java
dotsIndicator = (DotsIndicator) findViewById(R.id.dots_indicator);
viewPager = (ViewPager) findViewById(R.id.view_pager);
adapter = new ViewPagerAdapter();
viewPager.setAdapter(adapter);
dotsIndicator.setViewPager(viewPager);
<Android.support.v4.view.ViewPager
Android:id="@+id/vpImage"
Android:layout_width="match_parent"
Android:layout_height="@dimen/_120sdp" />
<Android.support.design.widget.TabLayout
Android:id="@+id/tlImage"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
app:tabBackground="@drawable/selector_product_image"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
app:tabMaxWidth="12dp"
app:tabRippleColor="@null" />
ImageAdapter imageAdapter = new ImageAdapter(context, arrayList);
vpImage.setOffscreenPageLimit(1);
vpImage.setAdapter(imageAdapter);
tlImage.setupWithViewPager(vpImage);
selector_product_image.xml
<selector xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item Android:drawable="@drawable/image_selected" Android:state_selected="true" />
<item Android:drawable="@drawable/image_unselected" />
</selector>
image_selected.xml
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item>
<shape
Android:innerRadius="0dp"
Android:shape="ring"
Android:thickness="4dp"
Android:useLevel="false">
<solid Android:color="@color/colorAccent" />
</shape>
</item>
</layer-list>
image_unselected.xml
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item>
<shape
Android:innerRadius="0dp"
Android:shape="ring"
Android:thickness="4dp"
Android:useLevel="false">
<solid Android:color="@color/colorPrimary" />
</shape>
</item>
</layer-list>
ImageAdapter.Java
class ImageAdapter extends PagerAdapter {
private Context context;
private ArrayList<ImageModel> arrayList;
private LayoutInflater layoutInflater;
public ImageAdapter(Context context, ArrayList<ImageModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
this.layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == ((View) o);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = layoutInflater.inflate(R.layout.row_slider_image, container, false);
AppCompatImageView ivProductImage = view.findViewById(R.id.ivProductImage);
if (!TextUtils.isEmpty(arrayList.get(position).getImage())) {
Glide.with(context)
.load(arrayList.get(position).getImage())
.apply(new RequestOptions().placeholder(R.drawable.no_image).error(R.drawable.no_image))
.into(ivProductImage);
}
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
row_slider_image.xml
<Android.support.v7.widget.LinearLayoutCompat xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical">
<Android.support.v7.widget.AppCompatImageView
Android:id="@+id/ivProductImage"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:src="@drawable/no_image" />
</Android.support.v7.widget.LinearLayoutCompat>
Votre xml
<RelativeLayout
Android:id="@+id/rl_speed"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_above="@+id/ll_dashboard_buttons"
Android:layout_below="@+id/ib_menu">
<com.smart.gps.speedometer.app.utils.SmartViewPager
Android:id="@+id/view_pager"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</com.smart.gps.speedometer.app.utils.SmartViewPager>
<Android.support.design.widget.TabLayout
Android:id="@+id/sliding_tabs"
Android:layout_alignParentBottom="true"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
app:tabBackground="@drawable/tab_selector"
app:tabIndicatorHeight="0dp"
app:tabGravity="center"
/>
créer un drawable. clic droit sur drawable -> new -> Drawable file resourcename ce fichier
tab_selector.xml
<item Android:drawable="@drawable/selected_tab"
Android:state_selected="true"/>
<item Android:drawable="@drawable/unselected_tab"/>
Maintenant, il y a deux autres fichiers XML. créer deux autres fichiers XML avec un nom respecté. ce sont l'indicateur de sélection et l'indicateur non sélectionné
selected_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item>
<shape
Android:innerRadius="0dp"
Android:shape="ring"
Android:thickness="4dp"
Android:useLevel="false">
<solid Android:color="@color/highspeed"/>
</shape>
</item>
</layer-list>
unselected_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item>
<shape
Android:innerRadius="0dp"
Android:shape="ring"
Android:thickness="2dp"
Android:useLevel="false">
<solid Android:color="@Android:color/darker_gray"/>
</shape>
</item>
</layer-list>
Placez ViewFlipper et viewFlipper_linear_dot_lay (Linearlayout) sur la même ligne de base et suivez les instructions ci-dessous.
viewFlipper_linear_dot_lay= (LinearLayout) findViewById(R.id.dots_lay);
setupDotsOnViewPager(images_viewFlipper);
for (int i = 0; i < images_viewFlipper.size(); i++) {
//Add Images to ViewFlipper
}
private void setupDotsOnViewPager(ArrayList images_viewFlipper) {
images_linear = new ImageView[images_viewFlipper.size()];
for (int i = 0; i < images_linear.length; i++) {
images_linear[i] = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 5, 0);
params.gravity = Gravity.BOTTOM | Gravity.CENTER;
images_linear[i].setLayoutParams(params);
images_linear[i].setImageResource(R.drawable.unselected);
viewFlipper_linear_dot_lay.addView(images_linear[i]);
viewFlipper_linear_dot_lay.bringToFront();
}
}
Et OnRight & OnLeft getsures place le code ci-dessous
for (int i = 0; i < images_linear.length; i++) {
images_linear[i].setImageResource(R.drawable.unselected);
}
images_linear[viewFlipper.getDisplayedChild()].setImageResource(R.drawable.selected);