Android之ViewPager实现图片+小圆点切换【有图有码】实例

时间:2021-08-17 00:48:24
看了好多小伙伴和我一样在寻找焦点图片对应地下小圆点的例子  我自己做好了 特地来分享一下

话不多说  come  !!!

代码片段,双击复制
010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 package
com.zh.xiaodiantest;
 import
java.util.ArrayList;
 import
android.app.Activity;
import
android.os.Bundle;
import
android.os.Parcelable;
import
android.support.v4.view.PagerAdapter;
import
android.support.v4.view.ViewPager;
import
android.support.v4.view.ViewPager.OnPageChangeListener;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.view.ViewGroup.LayoutParams;
import
android.view.Window;
import
android.widget.ImageView;
import
android.widget.ImageView.ScaleType;
import
android.widget.LinearLayout;
import
android.widget.TextView;
 public
class
MainActivity extends
Activity {
 private
ViewPager viewPager;
private
ArrayList<View> pageViews;
private
ImageView imageView;
private
ImageView[] imageViews;
//
包裹滑动图片LinearLayout
private
ViewGroup main;
//
包裹小圆点的LinearLayout
private
ViewGroup group;
@SuppressWarnings("unused")private
TextView content;
 @Overridepublic
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //
设置无标题窗口
requestWindowFeature(Window.FEATURE_NO_TITLE);int[] img = new
int
[] { R.drawable.no1, R.drawable.no2, R.drawable.no3,
R.drawable.no4,
R.drawable.no5 };
LayoutInflater
inflater = getLayoutInflater();
pageViews
=
new
ArrayList<View>();
for
(
int
i =
0; i < img.length; i++) {
LinearLayout
layout =
new
LinearLayout(
this);
LayoutParams
ltp =
new
LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);final
ImageView imageView =
new
ImageView(
this);
imageView.setScaleType(ScaleType.CENTER_INSIDE);imageView.setImageResource(img<i>);imageView.setPadding(15,30,15,30);layout.addView(imageView,
ltp);
pageViews.add(layout);}imageViews
=
new
ImageView[pageViews.size()];
main
= (ViewGroup) inflater.inflate(R.layout.pager_layout,
null);
group
= (ViewGroup) main.findViewById(R.id.viewGroup);
viewPager
= (ViewPager) main.findViewById(R.id.guidePages);
content
= (TextView) findViewById(R.id.photo_content);
 /***
有几张图片 下面就显示几个小圆点
*/ for
(
int
i =
0; i < pageViews.size(); i++) {
LinearLayout.LayoutParams
margin =
new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);//设置每个小圆点距离左边的间距margin.setMargins(10,0,0,0);imageView
=
new
ImageView(MainActivity.
this);
//设置每个小圆点的宽高imageView.setLayoutParams(new
LayoutParams(
15,15));
imageViews<i>
= imageView;
if
(i ==
0) {
//
默认选中第一张图片
imageViews<i>.setBackgroundResource(R.drawable.page_indicator_focused);}
else
{
//其他图片都设置未选中状态imageViews<i>.setBackgroundResource(R.drawable.page_indicator_unfocused);}group.addView(imageViews<i>,
margin);
}setContentView(main);//给viewpager设置适配器viewPager.setAdapter(new
GuidePageAdapter());
//给viewpager设置监听事件viewPager.setOnPageChangeListener(new
GuidePageChangeListener());
} //
指引页面数据适配器
class
GuidePageAdapter
extends
PagerAdapter {
 @Overridepublic
int
getCount() {
return
pageViews.size();
} @Overridepublic
boolean
isViewFromObject(View arg0, Object arg1) {
return
arg0 == arg1;
} @Overridepublic
int
getItemPosition(Object object) {
//
TODO Auto-generated method stub
return
super
.getItemPosition(object);
} @Overridepublic
void
destroyItem(View arg0, int
arg1, Object arg2) {
//
TODO Auto-generated method stub
((ViewPager)
arg0).removeView(pageViews.get(arg1));
} @Overridepublic
Object instantiateItem(View arg0,
int
arg1) {
//
TODO Auto-generated method stub
((ViewPager)
arg0).addView(pageViews.get(arg1));
return
pageViews.get(arg1);
} @Overridepublic
void
restoreState(Parcelable arg0, ClassLoader arg1) {
//
TODO Auto-generated method stub
 } @Overridepublic
Parcelable saveState() {
//
TODO Auto-generated method stub
return
null
;
} @Overridepublic
void
startUpdate(View arg0) {
//
TODO Auto-generated method stub
 } @Overridepublic
void
finishUpdate(View arg0) {
//
TODO Auto-generated method stub
 }} //
指引页面更改事件监听器
class
GuidePageChangeListener
implements
OnPageChangeListener {
 @Overridepublic
void
onPageScrollStateChanged(int
arg0) {
//
TODO Auto-generated method stub
 } @Overridepublic
void
onPageScrolled(int
arg0,
float
arg1,
int
arg2) {
//
TODO Auto-generated method stub
 } @Overridepublic
void
onPageSelected(int
arg0) {
//遍历数组让当前选中图片下的小圆点设置颜色for
(
int
i =
0; i < imageViews.length; i++) {
imageViews[arg0].setBackgroundResource(R.drawable.page_indicator_focused); if
(arg0 != i) {
imageViews<i>.setBackgroundResource(R.drawable.page_indicator_unfocused);}}}}}</i></i></i></i></i></i>


XML
代码片段,双击复制
010203040506070809101112131415161718192021222324252627 <?xml
version=
"1.0"
encoding=
"utf-8"?>
<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"
package="com.zh.xiaodiantest"android:versionCode="1"android:versionName="1.0"
>
 <uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17"
/>
 <applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
>
<activityandroid:name="com.zh.xiaodiantest.MainActivity"android:label="@string/app_name"
>
<intent-filter><action
android:name=
"android.intent.action.MAIN"
/>
 <category
android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter></activity></application> </manifest>

代码片段,双击复制
01020304050607080910111213141516171819202122232425262728293031323334353637383940 <RelativeLayout
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"
   
tools:ignore="UselessParent"
>
    
<android.support.v4.view.ViewPager
    
   
android:id="@+id/guidePages"
    
   
android:layout_width="match_parent"
    
   
android:layout_height="wrap_content"
    
   
android:layout_above="@+id/scrollview"
/>
    
<LinearLayout
    
   
android:id="@+id/viewGroup"
    
   
android:layout_width="match_parent"
    
   
android:layout_height="wrap_content"
    
   
android:layout_alignParentBottom="true"
    
   
android:layout_alignParentLeft="true"
    
   
android:gravity="center_horizontal"
    
   
android:orientation="horizontal"
    
   
android:layout_marginBottom="5dp"
    
   
>
   
</LinearLayout>
    
<ScrollView
    
   
android:id="@+id/scrollview"
    
   
android:layout_width="match_parent"
    
   
android:layout_height="70dp"
    
   
android:layout_above="@+id/viewGroup"
    
   
android:layout_marginBottom="15dp"
>
     
   
<TextView
    
       
android:id="@+id/photo_content"
    
       
android:layout_width="match_parent"
    
       
android:layout_height="wrap_content"
    
       
android:gravity="center_horizontal"
    
       
android:text="@string/hello_world"
/>
   
</ScrollView>
 </RelativeLayout>