1. 程式人生 > >Android之圓點導航的兩個案例(ViewPager)

Android之圓點導航的兩個案例(ViewPager)

案例一效果:

佈局檔案:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jogger.demo.ViewPagerActivity">


<android.support.v4.view.ViewPager
android:id="@+id/vp_guide"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>

<LinearLayout
android:id="@+id/ll_container"
android:layout_width="match_parent"

android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:gravity="center"
android:orientation="horizontal"></LinearLayout>

</RelativeLayout>

Activity:
public class ViewPagerActivity extends FragmentActivity {


private ViewPager vp_guide;
private List<ImageView> mImgList;//導航圖集合
private LinearLayout ll_container;//小圓點容器
private int mCurrentIndex = 0;//當前小圓點的位置
private int[] imgArray = {R.drawable.guidance1, R.drawable.guidance2, R.drawable.guidance3, R
.drawable.guidance4};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
vp_guide = (ViewPager) findViewById(R.id.vp_guide);
ll_container = (LinearLayout) findViewById(R.id.ll_container);
mImgList = new ArrayList<>();
for (int i = 0; i < imgArray.length; i++) {
ImageView imageView = new ImageView(this);//獲取4個圓點
imageView.setImageResource(imgArray[i]);
mImgList.add(imageView);
ImageView dot = new ImageView(this);
if (i == mCurrentIndex) {
dot.setImageResource(R.drawable.page_now);//設定當前頁的圓點
} else {
dot.setImageResource(R.drawable.page);//其餘頁的圓點
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout
.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
params.leftMargin = 10;//設定圓點邊距
}
dot.setLayoutParams(params);
ll_container.addView(dot);//將圓點新增到容器中
}
vp_guide.setAdapter(new MyAdapter(mImgList));
//新增監聽
vp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int
positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
//根據監聽的頁面改變當前頁對應的小圓點
mCurrentIndex = position;
for (int i = 0; i < ll_container.getChildCount(); i++) {
ImageView imageView = (ImageView) ll_container.getChildAt(i);
if (i == position) {
imageView.setImageResource(R.drawable.page_now);
} else {
imageView.setImageResource(R.drawable.page);
}
}
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
}

public class MyAdapter extends PagerAdapter {
private List<ImageView> mViewList;

public MyAdapter(List<ImageView> viewList) {
mViewList = viewList;
}

@Override
public ImageView instantiateItem(ViewGroup container, int position) {
ImageView view = mViewList.get(position);
container.addView(view);
return view;
}

@Override
public int getCount() {
return mViewList.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViewList.get(position));
}
}
}

案例二效果:


佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jogger.demo.ViewPagerActivity">

<android.support.v4.view.ViewPager
android:id="@+id/vp_guide"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp">

<LinearLayout
android:id="@+id/ll_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>

<ImageView
android:id="@+id/iv_dot_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/page_now"/>
</RelativeLayout>

</RelativeLayout>

Activity:
public class ViewPagerActivity extends FragmentActivity {

private ViewPager vp_guide;
private List<ImageView> mImgList;//導航圖集合
private LinearLayout ll_container;//小圓點容器
private int mCurrentIndex = 0;//當前小圓點的位置
private int mDotDis;//小圓點的距離
private ImageView iv_dot_now;//當前圓點
private int[] imgArray = {R.drawable.guidance1, R.drawable.guidance2, R.drawable.guidance3, R
.drawable.guidance4};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
vp_guide = (ViewPager) findViewById(R.id.vp_guide);
ll_container = (LinearLayout) findViewById(R.id.ll_container);
iv_dot_now = (ImageView) findViewById(R.id.iv_dot_now);
mImgList = new ArrayList<>();
for (int i = 0; i < imgArray.length; i++) {
ImageView imageView = new ImageView(this);//獲取4個圓點
imageView.setImageResource(imgArray[i]);
mImgList.add(imageView);
ImageView dot = new ImageView(this);
dot.setImageResource(R.drawable.page);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout
.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
params.leftMargin = 10;//設定圓點邊距
}
dot.setLayoutParams(params);
ll_container.addView(dot);//將圓點新增到容器中
}
vp_guide.setAdapter(new MyAdapter(mImgList));
//新增監聽
vp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int
positionOffsetPixels) {
// 滾動過程中
// 紅色小圓點的移動距離=移動百分比*兩個圓點的間距
// 更新小紅點距離
int dis = (int) (mDotDis * positionOffset) + position * mDotDis;//
// 因為移動完一個介面後,百分比會歸0,所以要加上移動過的單位position*mPointDis
//獲取小圓點的佈局屬性,更新左邊距
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_dot_now
.getLayoutParams();
params.leftMargin = dis;// 修改左邊距
// 重新設定佈局引數
iv_dot_now.setLayoutParams(params);
}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int state) {

}
});
// 監聽layout方法結束的事件,位置確定之後獲取圓點間距
// 檢視樹
iv_dot_now.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver
.OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
//檢視樹移除監聽
iv_dot_now.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//計算得到小圓點距離
mDotDis = ll_container.getChildAt(1).getLeft() - ll_container.getChildAt(0)
.getLeft();
}
});
}

public class MyAdapter extends PagerAdapter {
private List<ImageView> mViewList;

public MyAdapter(List<ImageView> viewList) {
mViewList = viewList;
}

@Override
public ImageView instantiateItem(ViewGroup container, int position) {
ImageView view = mViewList.get(position);
container.addView(view);
return view;
}

@Override
public int getCount() {
return mViewList.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mViewList.get(position));
}
}
}