利用viewPager制作轮播图

利用ViewPager这个demo实现的是可以左右循环滑动图片,类似于web焦点图的效果,简单大方,效果使用。

Layout布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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:context=".MainActivity" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="160dp" >

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="5dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#66000000" >

<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:singleLine="true" />

<LinearLayout
android:id="@+id/ll_point_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
></LinearLayout>
</LinearLayout>
</RelativeLayout>

</RelativeLayout>

MainActivity

  • ViewPager在Android-support-v4.jar这个jar包下,所以不要导错jar包
  • 自定义MyAdapter继承自PagerAdapter
  • 监听ViewPager的图片改变OnPageChangeListener
  • 通过记录上一次的图片的位置lastPosition来修改焦点原点的显示状态

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    package com.declan.viewpager;

    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private TextView tv_desc;
    private LinearLayout ll_point_container;
    private int[] resourceId; //图片资源集合
    private ArrayList<ImageView> list; //imageview资源集合
    private LinearLayout.LayoutParams layoutParams;
    private String[] contentDesc; //文字描述资源集合
    private int lastPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initUI();
    initData();
    initAdapter();

    }
    private void initUI() {
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.addOnPageChangeListener(this);
    tv_desc = (TextView) findViewById(R.id.tv_desc);
    ll_point_container = (LinearLayout) findViewById(R.id.ll_point_container);
    }

    /**
    * 初始化数据,包括图片资源、文字资源
    */
    private void initData() {
    //图片资源集合
    resourceId = new int[]{
    R.drawable.a,R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e
    };
    //文字集合
    contentDesc = new String[]{
    "1111",
    "2222",
    "3333",
    "4444",
    "5555"
    };
    ImageView imageView;
    View pointView;
    list = new ArrayList<ImageView>();
    for(int i = 0;i < resourceId.length;i ++){
    imageView = new ImageView(this);
    imageView.setImageResource(resourceId[i]);
    list.add(imageView);

    //给视图添加小白点
    pointView = new View(this);
    pointView.setBackgroundResource(R.drawable.selector_bg);
    layoutParams = new LinearLayout.LayoutParams(10, 10);
    if(i != 0){
    layoutParams.leftMargin = 15;
    }
    pointView.setEnabled(false);
    ll_point_container.addView(pointView,layoutParams);
    }
    }

    /**
    * 初始化适配器
    * 可以将viewPager近似看成横向的listView
    */
    private void initAdapter() {
    //第一次进入初始化数据
    ll_point_container.getChildAt(0).setEnabled(true);
    tv_desc.setText(contentDesc[0]);
    viewPager.setAdapter(new MyAdapter());
    }

    /**
    * 自定义适配器,继承自PagerAdapter
    */
    class MyAdapter extends PagerAdapter{

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

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

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

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

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
    //新的条目被选中时调用
    tv_desc.setText(contentDesc[position]);
    ll_point_container.getChildAt(position).setEnabled(true);
    ll_point_container.getChildAt(lastPosition).setEnabled(false);
    lastPosition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    }