本程序需要懂得viewpager的基础知识,也就是说你能够自己写出一个手动滑动的viewpager,下面我将附带加小圆点的知识
那么我们回顾一下图片轮转的基础知识,如果你已经对viewpager很熟悉了就不用看了,浪费时间
1.viewpager是在v4兼容报里面的,使用控件时请带上报名
2.数据来源用list存放,我这里用的是textview做的小圆点list《textview》,layout做的单张幻灯片list《view》,将xml文件inflate成为view,View.inflate()参数我就不写了。
3.viewpager的适配器pageradapter的使用
一切准备就绪之后,你首先要实现的是小圆点与幻灯片的同步
它的逻辑我先带大家理一下
在控件中第一个小圆点为选中状态,就是说一进来第一个小圆点是被选中的。
当幻灯片切换到第二张的时候将第二个小圆点的背景设为你想要设置的被选中的颜色,然后你还要将第一个设为没被选中的颜色。
一直这样下去就形成了一个规律,就有了这样一段代码
if(position==0){ textlist.get(position+1).setTextColor(0x99000000);
}if(position==1){
textlist.get(position-1).setTextColor(0x99000000);
textlist.get(position+1).setTextColor(0x99000000);
}if(position==2){
textlist.get(position-1).setTextColor(0x99000000);
textlist.get(position+1).setTextColor(0x99000000);
}if(position==3){
textlist.get(position-1).setTextColor(0x99000000);
}
也许看完这段代码你会有个疑问就是position从哪来的,也可能有的同学已经知道了,没错他就是有viewpager.setonpagerchangelistener的onpageselected方法里面的
所以将这段代码 放在里面
下面进一步升级,自动循环,我这里用的是简单的handler的方法
首先声明一个handler
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(currentItem==3){
currentItem=-1;
}
aviewpager.setCurrentItem(currentItem+1);
}
};
这里面还需要两个变量也贴在这里
现在所处的张数
int currentItem;
进入无限循环的标识
private boolean isLoop = true;
currentItem的赋值我想到的是onpageselected方法中的position。
那么onpagerselected方法要有一些变动
aviewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
currentItem =position;
textlist.get(position).setTextColor(0x997cfc00);
if(position==0){
textlist.get(position+1).setTextColor(0x99000000);
// 新增一条这里 textlist.get(position+3).setTextColor(0x99000000);
}
if(position==1){
textlist.get(position-1).setTextColor(0x99000000);
textlist.get(position+1).setTextColor(0x99000000);
}
textlist.get(position+1).setTextColor(0x99000000);
}if(position==3){
textlist.get(position-1).setTextColor(0x99000000);
}
}
最后在oncreate方法里面还要新建一个线程
new Thread(new Runnable() {
@Override
public void run() {
while (isLoop) {
SystemClock.sleep(1000);
handler.sendEmptyMessage(0);
}
}
}).start();
设置你想要的时间
下面贴出我的完整代码 以免有些地方看的云里雾里
java代码
public class ApplicationCenter_activity extends Activity {
private ViewPager aviewpager;
private List<View> viewlist;
private List<TextView> textlist;
private TextView tv1, tv2, tv3, tv4;
int currentItem;
private boolean isLoop = true;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(currentItem==3){
currentItem=-1;
}
aviewpager.setCurrentItem(currentItem+1);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.application_layout);
new Thread(new Runnable() {
@Override
public void run() {
while (isLoop) {
SystemClock.sleep(1000);
handler.sendEmptyMessage(0);
}
}
}).start();
viewlist = new ArrayList<View>();
textlist = new ArrayList<TextView>();
aviewpager = (ViewPager) findViewById(R.id.aviewpager1);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4= (TextView) findViewById(R.id.tv4);
View view = View.inflate(getApplicationContext(),R.layout.view1, null);
View view1 = View.inflate(getApplicationContext(),R.layout.view2, null);
View view2 = View.inflate(getApplicationContext(),R.layout.view3, null);
View view3 = View.inflate(getApplicationContext(),R.layout.view4, null);
viewlist.add(view);
viewlist.add(view1);
viewlist.add(view2);
viewlist.add(view3);
textlist.add(tv1);
textlist.add(tv2);
textlist.add(tv3);
textlist.add(tv4);
MyPagerAdapter adapter = new MyPagerAdapter(viewlist);
aviewpager.setAdapter(adapter);
aviewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
currentItem =position;
textlist.get(position).setTextColor(0x997cfc00);
if(position==0){
textlist.get(position+1).setTextColor(0x99000000);
textlist.get(position+3).setTextColor(0x99000000);
}
if(position==1){
textlist.get(position-1).setTextColor(0x99000000);
textlist.get(position+1).setTextColor(0x99000000);
}
if(position==2){
textlist.get(position-1).setTextColor(0x99000000);
textlist.get(position+1).setTextColor(0x99000000);
}if(position==3){
textlist.get(position-1).setTextColor(0x99000000);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
这种方式可能欠妥,最好是用imageview,两种状态的图片为内容
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<android.support.v4.view.ViewPager
android:id="@+id/aviewpager1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="·"
android:textColor="#7cfc00"
android:textSize="50sp" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="·"
android:textColor="#000000"
android:textSize="50sp" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="·"
android:textColor="#000000"
android:textSize="50sp" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="·"
android:textColor="#000000"
android:textSize="50sp" />
</LinearLayout>
</FrameLayout>
下面是简单的适配器
package com.yfbiz.adapter;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MyPagerAdapter extends PagerAdapter {
private List<View> viewlist;
public MyPagerAdapter(List<View> viewlist){
this.viewlist=viewlist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return viewlist.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(viewlist.get(position));
return viewlist.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView(viewlist.get(position));
}
}