本文主要是介绍仿优酷客户端viewpager定时换图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1写布局文件
<LinearLayout 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:background="#FFFFFF"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="140dip" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="中国家庭院校园区域名字体现"
android:textColor="#123123" />
<LinearLayout
android:id="@+id/ll_dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dip" >
<View
android:id="@+id/v_dot0"
style="@style/dot_style"
android:background="@drawable/dot_focused" />
<View
android:id="@+id/v_dot1"
style="@style/dot_style" />
<View
android:id="@+id/v_dot2"
style="@style/dot_style" />
<View
android:id="@+id/v_dot3"
style="@style/dot_style" />
<View
android:id="@+id/v_dot4"
style="@style/dot_style" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
2、定义圈的样式
<style name="dot_style">
<item name="android:layout_width">5dip</item>
<item name="android:layout_height">5dip</item>
<item name="android:background">@drawable/dot_normal</item>
<item name="android:layout_marginLeft">1.5dip</item>
<item name="android:layout_marginRight">1.5dip</item>
</style>
3、主要逻辑代码
package com.example.viewpagertest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
public class MainActivity extends Activity {
private ViewPager mPager;
private List<ImageView> imageViews;
private String[] titles;
private int[] imageResId;
private List<View> dots;
private TextView tv_title;
private int currentitem = 0;
private ScheduledExecutorService service;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
mPager.setCurrentItem(currentitem);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupView();
addLisenner();
}
private void addLisenner() {
// TODO Auto-generated method stub
mPager.setOnPageChangeListener(new OnPageChangeListener() {
private int oldposition = 0;
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
currentitem = arg0;
tv_title.setText(titles[arg0]);
dots.get(oldposition).setBackgroundResource(
R.drawable.dot_normal);
dots.get(arg0).setBackgroundResource(R.drawable.dot_focused);
oldposition = currentitem;
}
@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
}
});
}
private void setupView() {
// TODO Auto-generated method stub
mPager = (ViewPager) findViewById(R.id.vp_test);
tv_title = (TextView) findViewById(R.id.tv_title);
imageResId = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e };
titles = new String[imageResId.length];
titles[0] = "巩俐不低俗,我就不能低俗";
titles[1] = "扑树又回来啦!再唱经典老歌引万人大合唱";
titles[2] = "揭秘北京电影如何升级";
titles[3] = "乐视网TV版大派送";
titles[4] = "热血屌丝的反杀";
imageViews = new ArrayList<ImageView>();
for (int i = 0; i < imageResId.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageResId[i]);
imageView.setScaleType(ScaleType.CENTER_CROP);
imageViews.add(imageView);
}
tv_title.setText(titles[0]);
dots = new ArrayList<View>();
dots.add(findViewById(R.id.v_dot0));
dots.add(findViewById(R.id.v_dot1));
dots.add(findViewById(R.id.v_dot2));
dots.add(findViewById(R.id.v_dot3));
dots.add(findViewById(R.id.v_dot4));
mPager.setAdapter(new MyAdapter());
}
private class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageResId.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
((ViewPager) container).addView(imageViews.get(position));
return imageViews.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);//这个一定要去掉
((ViewPager) container).removeView((View) object);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(new ScrollTask(), 1, 2, TimeUnit.SECONDS);
}
private class ScrollTask implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (mPager) {
System.out.println("currentItem: " + currentitem);
currentitem = (currentitem + 1) % imageViews.size();// 加1代表下一个
// 第二张图
handler.obtainMessage().sendToTarget();
}
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
service.shutdown();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这篇关于仿优酷客户端viewpager定时换图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!