您的位置:首页 > 移动开发 > Android开发

Android中ViewPager的使用(二):实现图片轮播效果

2016-04-20 13:00 971 查看
还是使用ViewPager+PagerAdapter进行实现:

xml代码:

<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">

    <android.support.v4.view.ViewPager

        android:id="@+id/viewPager"

        android:layout_width="match_parent"

        android:layout_height="match_parent"/>

    <LinearLayout 

        android:id="@+id/linearLayout"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        android:gravity="center"

        android:layout_alignParentBottom="true">

        <ImageView 

            android:id="@+id/imageView1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/selector_icon"/>

        <ImageView 

            android:id="@+id/imageView2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/selector_icon"/>

        <ImageView 

            android:id="@+id/imageView3"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/selector_icon"/>

        <ImageView 

            android:id="@+id/imageView4"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/selector_icon"/>

        <ImageView 

            android:id="@+id/imageView5"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/selector_icon"/>

    </LinearLayout>

</RelativeLayout>

这里使用到了selector,在drawable下新建selecotor_icon:
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_enabled="true"

        android:state_selected="true"

        android:drawable="@drawable/icon02"/>

    <item android:state_enabled="true"

        android:state_selected="false"

        android:drawable="@drawable/icon01"/>

</selector>

java代码:
public class MainActivity extends Activity implements OnPageChangeListener{

private ViewPager viewPager;
private MyPagerAdapter adapter;
private List<ImageView> list;
private int[] imageId=new int[]{R.drawable.txt_theme12,R.drawable.txt_theme14,
R.drawable.txt_theme17,R.drawable.txt_theme26,R.drawable.txt_theme27};
private ImageView[] icon;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initData();//初始化要向ViewPager中添加的View

        initView();//初始化View

        initBottom();//处理底部图标

    }
private void initBottom() {

LinearLayout linearLayout=(LinearLayout) findViewById(R.id.liinearLayout);
icon=new ImageView[5];
for(int i=0;i<icon.length;i++){
icon[i]=(ImageView) linearLayout.getChildAt(i);
icon[i].setEnabled(true);
icon[i].setTag(i);
icon[i].setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//设置ViewPager中条目为当前item
viewPager.setCurrentItem((Integer) v.getTag());
}
});
}
icon[0].setEnabled(false);

}
private void initData() {

list=new ArrayList<ImageView>();
ImageView imageView=null;
for(int i=0;i<imageId.length;i++){
imageView=new ImageView(this);
imageView.setImageResource(imageId[i]);
list.add(imageView);
}
}
private void initView() {
viewPager=(ViewPager) findViewById(R.id.viewPager);
adapter=new MyPagerAdapter();
viewPager.setAdapter(adapter);
//设置ViewPager的监听事件,实现三个方法
viewPager.setOnPageChangeListener(this);
}

/*创建MyPagerAdapter 继承PagerAdapter*/
class MyPagerAdapter
extends PagerAdapter{

/*得到Viewpager中View的总条目数*/
@Override
public int
getCount() {

return list.size();
}
/*在Viewpager中添加View*/
@Override
public Object
instantiateItem(ViewGroup container, int position) {

container.addView(list.get(position));
return list.get(position);
}
/*判断view是否与object关联*/
@Override
public boolean
isViewFromObject(View view, Object object) {

return view==object;
}
/*在ViewPager中移除当前View*/
@Override
public void
destroyItem(ViewGroup container, int position, Object object) {
container.removeView(list.get(position));
}
}

/*View滑动状态改变时回调*/
@Override
public void onPageScrollStateChanged(int arg0) {

}
/*View滑动时回调*/
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}
/*View被选中时回调*/
@Override
public void
onPageSelected(int position) {

//随着ViewPager中View的改变底部的icon也随之改变
for(int i=0;i<icon.length;i++){
icon[i].setEnabled(true);
}
//设置当前icon为选中状态
icon[position].setEnabled(false);
}

}


运行效果:



滑动到第二张,图标也进行改变:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  viewpager PagerAdapter