Android 滑動小圓點ViewPager的兩種設(shè)置方法詳解流程
第一種方法:
一、測試如下,直接設(shè)置小圓點不是圖標(biāo)

二、準(zhǔn)備工作
1.在drawable創(chuàng)建dot.xml,設(shè)置小圓點,比較方便
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="oval">
<solid android:color="@color/black" />
<corners android:radius="8dp" />
</shape>
</item>
<item android:state_selected="false">
<shape android:shape="oval">
<solid android:color="@color/white" />
<corners android:radius="8dp" />
</shape>
</item>
</selector>
2.布局小圓點的狀態(tài)可以被左右滑動dotview.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="@+id/v_dot"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/dot"/>
</LinearLayout>
3.分頁適配器自定義ViewPagerAdapter.java
public class ViewPagerAdapter extends PagerAdapter {
private List<View> mViewList;
public ViewPagerAdapter(List<View> mViewList) {
this.mViewList = mViewList;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViewList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mViewList.get(position));
return (mViewList.get(position));
}
@Override
public int getCount() {
if (mViewList == null)
return 0;
return mViewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
三、使用工作:
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D4D3D3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FFFFFF"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/ll_dots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" />
</RelativeLayout>
</LinearLayout>
分析下布局結(jié)構(gòu):
(1)首先是一個ViewPager,用于結(jié)合GridView實現(xiàn)左右滑動菜單
(2)下面是一個LinearLayout,有多少個ViewPager的頁面就會inflate出多少個小圓點,并且在ViewPager翻頁時,也就是說在左右滑動時,下面小圓點的狀態(tài)也要相應(yīng)地做出改變
2.MainActivity.java
public class MainActivity extends AppCompatActivity {
private ViewPager mPager;
private LinearLayout mLlDots;
private LayoutInflater inflater;
private List<View> mPagerList;
private int pageCount = 3;//默認(rèn)三個小點
/**
* 當(dāng)前顯示的是第幾頁
*/
private int curIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.viewpager);
mLlDots = (LinearLayout) findViewById(R.id.ll_dots);
inflater = LayoutInflater.from(this);
mPagerList = new ArrayList<View>();
//#FF9800:黃,#4CAF50:綠,#2196F3:藍
String[] colors = {"#FF9800", "#4CAF50", "#2196F3"};
for (int i = 0; i < pageCount; i++) {
LinearLayout mLL = (LinearLayout) inflater.inflate(R.layout.linearlayout, mPager, false);
mLL.setBackgroundColor(Color.parseColor(colors[i]));
mPagerList.add(mLL);
}
//設(shè)置適配器
mPager.setAdapter(new ViewPagerAdapter(mPagerList));
//設(shè)置圓點
setDotLayout();
}
/**
* 設(shè)置圓點
*/
public void setDotLayout() {
for (int i = 0; i < pageCount; i++) {
mLlDots.addView(inflater.inflate(R.layout.dotview, null));
}
// 默認(rèn)顯示第一頁
mLlDots.getChildAt(0).setSelected(true);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// 取消原點選中
mLlDots.getChildAt(curIndex).setSelected(false);
// 原點選中
mLlDots.getChildAt(position).setSelected(true);
curIndex = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
這代碼中一句,布局LinearLayout隨著左右滑動小圓點翻頁
LinearLayout mLL = (LinearLayout) inflater.inflate(R.layout.linearlayout, mPager, false);
布局linearlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
第二種方法:
一、測試如下,小圓點圖標(biāo)

二、dotview.xml
......
<ImageView
android:id="@+id/v_dot"
android:layout_width="10dp"
android:layout_height="10dp"/>
......
三、設(shè)置二個小圓點圖標(biāo)(黑白)
點擊鏈接:二個小圓點圖標(biāo).zip
// 默認(rèn)顯示第一頁
mLlDots.getChildAt(0).findViewById(R.id.v_dot)
.setBackgroundResource(R.drawable.dot_normal);
mLlDots.getChildAt(1).findViewById(R.id.v_dot)
.setBackgroundResource(R.drawable.dot_selected);
mLlDots.getChildAt(2).findViewById(R.id.v_dot)
.setBackgroundResource(R.drawable.dot_selected);
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageSelected(int position) {
// 取消圓點選中
mLlDots.getChildAt(curIndex)
.findViewById(R.id.v_dot)
.setBackgroundResource(R.drawable.dot_selected);
// 圓點選中
mLlDots.getChildAt(position)
.findViewById(R.id.v_dot)
.setBackgroundResource(R.drawable.dot_normal);
curIndex = position;
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
到此這篇關(guān)于Android 滑動小圓點ViewPager的兩種設(shè)置方法詳解流程的文章就介紹到這了,更多相關(guān)Android 滑動小圓點內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
分享Android開發(fā)自學(xué)筆記之AndroidStudio常用功能
這篇文章主要給大家分享Android開發(fā)自學(xué)筆記之AndroidStudio常用功能的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android自定義TextView實現(xiàn)文字圖片居中顯示的方法
下面小編就為大家分享一篇Android自定義TextView實現(xiàn)文字圖片居中顯示的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Windows下搭建Flutter開發(fā)環(huán)境
這篇文章介紹了Windows下搭建Flutter開發(fā)環(huán)境的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
RecyclerView使用payload實現(xiàn)局部刷新
這篇文章主要為大家詳細(xì)介紹了RecyclerView使用payload實現(xiàn)局部刷新,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Android應(yīng)用開發(fā)中Fragment與Activity間通信示例講解
這篇文章主要介紹了Android應(yīng)用開發(fā)中Fragment與Activity間通信實例講解,需要的朋友可以參考下2016-02-02
Android矢量圖之VectorDrawable類自由填充色彩
這篇文章主要介紹了Android矢量圖之VectorDrawable類自由填充色彩的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
Android實現(xiàn)簡單斷點續(xù)傳和下載到本地功能
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)簡單斷點續(xù)傳和下載到本地功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11

