viewpager同时实现自动切换和手动切换
2015-08-08 17:31
330 查看
下面的代码同时实现了自动切换和手动切换,先把代码贴上
第一段是适配器
<pre name="code" class="html">package com.example.bean;import java.util.List;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.View;import android.view.ViewGroup;public class MyPagerAdapter extends PagerAdapter {List<View> views;public MyPagerAdapter(List<View> views){this.views=views;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn views==null ? 0 : views.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0==arg1;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {ViewPager vp=(ViewPager)container;View v=views.get(position);vp.addView(v);return v;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {ViewPager vp=(ViewPager)container;View v=views.get(position);vp.removeView(v);}}
package com.example.help;import java.util.ArrayList;import java.util.List;import com.example.bean.MyPagerAdapter;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;import android.widget.RadioGroup.OnCheckedChangeListener;public class XiuseActivity extends Activity {ViewPager viewPager;List<View> views;RadioGroup radioGroup;RadioButton radioOne;RadioButton radioTwo;RadioButton radioThree;ImageButton imagebuttonback;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.xiuse_activity_main);init();}@Overrideprotected void onResume() {super.onResume();//activity启动两秒钟后,发送一个message,用来将viewPager中的图片切换到下一个mHandler.sendEmptyMessageDelayed(1, 2000);}private void init() {viewPager=(ViewPager)findViewById(R.id.viewPager_xiuse);radioGroup=(RadioGroup)findViewById(R.id.radioGroup_xiuse);radioOne=(RadioButton)findViewById(R.id.radio3);radioTwo=(RadioButton)findViewById(R.id.radio4);radioThree=(RadioButton)findViewById(R.id.radio5);imagebuttonback=(ImageButton)findViewById(R.id.imageButton1);views=new ArrayList<View>();imagebuttonback.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO 自动生成的方法存根finish();}});LayoutInflater inflater=LayoutInflater.from(XiuseActivity.this);View pageOne = inflater.inflate(R.layout.xiuse_one, null);View pageTwo = inflater.inflate(R.layout.xiuse_two, null);View pageThree = inflater.inflate(R.layout.xiuse_three, null);views.add(pageOne);views.add(pageTwo);views.add(pageThree);MyPagerAdapter adapter=new MyPagerAdapter(views);viewPager.setAdapter(adapter);radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch (checkedId) {case R.id.radio0:viewPager.setCurrentItem(0);break;case R.id.radio1:viewPager.setCurrentItem(1);break;case R.id.radio2:{viewPager.setCurrentItem(2);}break;default:break;}}});viewPager.setOnPageChangeListener(new OnPageChangeListener() {@Overridepublic void onPageSelected(int arg0) {// TODO Auto-generated method stubswitch (arg0) {case 0:radioOne.setChecked(true);break;case 1:radioTwo.setChecked(true);break;case 2:radioThree.setChecked(true);break;default:break;}}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stub}@Overridepublic void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stub}});}private Handler mHandler = new Handler(){public void handleMessage(android.os.Message msg){ switch(msg.what){ case 1:int totalcount = views.size();//autoChangeViewPager.getChildCount();int currentItem = viewPager.getCurrentItem();int toItem = currentItem + 1 == totalcount ? 0 : currentItem + 1;viewPager.setCurrentItem(toItem, true);//每两秒钟发送一个message,用于切换viewPager中的图片this.sendEmptyMessageDelayed(1, 2000);}}};}以上是.java文件的代码,下面是xml文件的代码xiuce_activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><RelativeLayoutandroid:id="@+id/linearLayout_xiuse_biaoti"android:layout_width="match_parent"android:layout_height="55dp"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:background="@drawable/biaoti"android:gravity="center_horizontal"android:orientation="horizontal" ><ImageButtonandroid:id="@+id/imageButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:src="@android:drawable/arrow_down_float" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="110dp"android:text="秀色"android:textColor="#ffffff"android:textSize="40sp" /></RelativeLayout><ImageButtonandroid:id="@+id/imageButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/linearLayout_xiuse_biaoti"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:src="@drawable/left_row" /><RelativeLayoutandroid:id="@+id/qita"android:layout_width="match_parent"android:layout_height="80dp"android:layout_alignParentLeft="true"android:layout_below="@+id/linearLayout_xiuse_biaoti" ><TextViewandroid:id="@+id/textView_xiuse_kecan"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/imageButton_circle"android:layout_alignParentBottom="true"android:layout_alignRight="@+id/imageButton_circle"android:gravity="center"android:text="可餐"android:textSize="15dp" /><TextViewandroid:id="@+id/textView_xiuse_kaipa"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_alignRight="@+id/imageButton_circle1"android:gravity="center"android:text="开趴"android:textSize="15dp" /><ImageButtonandroid:id="@+id/imageButton_circle1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/textView_xiuse_kecan"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:src="@drawable/circle" /><ImageButtonandroid:id="@+id/imageButton_circle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/textView_xiuse_kecan"android:layout_alignParentRight="true"android:layout_marginRight="100dp"android:src="@drawable/circle" /><ImageButtonandroid:id="@+id/imageButton_circle2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/textView_xiuse_kecan"android:layout_alignParentTop="true"android:layout_toRightOf="@+id/textView_xiuse_kecan"android:src="@drawable/circle" /><TextViewandroid:id="@+id/textView_xiuse_kezhan"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/imageButton_circle2"android:layout_alignParentRight="true"android:layout_below="@+id/imageButton_circle"android:gravity="center"android:text="客栈"android:textSize="15dp" /></RelativeLayout><LinearLayoutandroid:id="@+id/guanggao_xiuse"android:layout_width="match_parent"android:layout_height="170dp"android:layout_alignParentLeft="true"android:layout_below="@+id/qita"android:orientation="vertical" ><android.support.v4.view.ViewPagerandroid:id="@+id/viewPager_xiuse"android:layout_width="match_parent"android:layout_height="match_parent"b74bandroid:layout_above="@+id/radioGroup" ></android.support.v4.view.ViewPager></LinearLayout><RadioGroupandroid:id="@+id/radioGroup_xiuse"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_below="@+id/guanggao_xiuse"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/radio3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true" /><RadioButtonandroid:id="@+id/radio4"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/radio5"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RadioGroup></RelativeLayout>下面的分别是是viewpager的页卡xml文件,只增加了一个背景色,其他页卡同理
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#DEB887"android:orientation="vertical" ></LinearLayout>总而言之,手动切换是利用OnCheckedChangeListener和setOnPageChangeListener这两个监听器显示相应滑动的页卡内容,自动切换的重点是利用了handler的handleMessage方法,每隔两秒发送消息更改UI
相关文章推荐
- js判断输入的年月日是一年中的第几天
- 上传图片的方法以及判断
- PageRank及其MapReduce实现
- Mac OS X 10.10中配置Apache + PHP
- django操作mysql时django.db.utils.OperationalError: (2003, "Can't connect to MySQL server")错误的解决办法
- 【codevs1068】乌龟棋noip10年TG----第二个A掉的钻石题
- oracle12创建用户错误ORA-65096: 公用用户名或角色名无效 invalid common user or role name
- 括号配对
- C++设计模式——原型模式
- PhotoShop 图章工具的简单使用
- checkbox的全选与反选
- 阿拉卡拉卡韩国
- A. Toy Cars
- Top 3000+ Python Modules
- C99标准学习笔记(2)——整数提升
- Logistic回归
- ListView性能优化问题总结
- 正则表达式代码生成
- 获取屏幕宽高和密度
- 【原创】文本工具的使用--根据数据库字段快速生成该表对应的Model类属性