在主布局声明一个android.support.v4.view.ViewPager的布局Javapackagecom.example.viewpager_text2;importjava.util.ArrayList;importandroid.R.integer;importandroid.annotation.SuppressLint;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importandroid.support.v4.view.PagerAdapter;importandroid.support.v4.view.ViewPager;importandroid.support.v4.view.ViewPager.OnPageChangeListener;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.View.OnTouchListener;importandroid.view.ViewGroup;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.widget.TextView;importandroid.widget.Toast;publicclassMainActivityextendsActivity{ ViewPager viewPager; TextView text_main_tatle; LinearLayout ll_main_point_group; ArrayList imageviews;//图片资源int[] imageids={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, };//图片的标题集合String [] imagetion={"有型有范","周末放假","家电买一送一","预约迎新机","京东火锅节"};intpreposition=0;//设置高亮的位置Handler handler=newHandler(){publicvoidhandleMessage(android.os.Message msg){intitem=viewPager.getCurrentItem()+1; viewPager.setCurrentItem(item);//延迟发消息handler.sendEmptyMessageDelayed(0,3000); }; };booleanisdragging=false;//判断划屏幕的操作@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//加载布局初始化viewPager=(ViewPager) findViewById(R.id.viewpager_main); text_main_tatle=(TextView) findViewById(R.id.text_main_tatle); ll_main_point_group=(LinearLayout) findViewById(R.id.ll_main_point_group);// listview 的使用// 1,在布局中文件定义viewpager// 2,在代码中实例化Viewpager// 3,准备数据imageviews=newArrayList();for(inti=0;i滑动 或 滑动-->静止 或 静止-->拖拽@OverridepublicvoidonPageScrollStateChanged(intarg0){// TODO Auto-generated method stubif(arg0==ViewPager.SCROLL_STATE_DRAGGING){//拖拽isdragging=true; }elseif(arg0==ViewPager.SCROLL_STATE_SETTLING){//滚动}elseif(arg0==ViewPager.SCROLL_STATE_IDLE&& isdragging){//静止isdragging=false; handler.removeCallbacksAndMessages(null); handler.sendEmptyMessageDelayed(0,3000); } }// 当前页面滚动的时候回调此方法// arg0 当期页面的位置// arg1滑动页面的百分比// arg2 在屏幕上滑动的像素@OverridepublicvoidonPageScrolled(intarg0,floatarg1,intarg2){// TODO Auto-generated method stub}//当某个界面被选中时的回调方法// arg0本选中页面的位置@OverridepublicvoidonPageSelected(intarg0){// TODO Auto-generated method stubintrealpostion=arg0%imageviews.size();//设置对应页面的文本信息text_main_tatle.setText(imagetion[realpostion]);//坝上一个高亮的设置成灰色ll_main_point_group.getChildAt(preposition) .setEnabled(false);//当前设置成红色ll_main_point_group.getChildAt(realpostion) .setEnabled(true); preposition=realpostion; } }classMypagerextendsPagerAdapter{//得到图片的总数@OverridepublicintgetCount(){// TODO Auto-generated method stub// return imageviews.size();returnInteger.MAX_VALUE;//int类型的最大值}//相当于getView的方法//container Viewpager自身//position 当前实例化页面的位置(下标)@OverridepublicObjectinstantiateItem(ViewGroup container,intposition){intrealPostion=position% imageviews.size();// TODO Auto-generated method stubImageView imageview = imageviews.get(realPostion); container.addView(imageview);//添加到Viewpager中imageview.setOnTouchListener(newOnTouchListener() {@OverridepublicbooleanonTouch(View v, MotionEvent event){// TODO Auto-generated method stubswitch(event.getAction()) {caseMotionEvent.ACTION_DOWN://手指按下时的操作handler.removeCallbacksAndMessages(null);break;caseMotionEvent.ACTION_MOVE://手指移动时的操作break;caseMotionEvent.ACTION_CANCEL://事件取消handler.removeCallbacksAndMessages(null); handler.sendEmptyMessageDelayed(0,3000);break;caseMotionEvent.ACTION_UP://手指抬起时的操作handler.removeCallbacksAndMessages(null); handler.sendEmptyMessageDelayed(0,3000);break; }returnfalse; } }); imageview.setTag(realPostion); imageview.setOnClickListener(newOnClickListener() {@SuppressLint("ShowToast")@OverridepublicvoidonClick(View v){// TODO Auto-generated method stubintposition = (Integer) v.getTag(); String text=imagetion[position]; Toast.makeText(MainActivity.this, text,0).show(); } });returnimageview; }//比较View和Object是否是同一个实例//arg0 就是页面//arg1是instantiateItem返回的结果@OverridepublicbooleanisViewFromObject(View arg0, Object arg1){// TODO Auto-generated method stubreturnarg0==arg1; }//释放资源// container就是Viewpager// position 要释放的位置// object要释放的页面@OverridepublicvoiddestroyItem(ViewGroup container,intposition, Object object){ container.removeView((View) object); } }}android:layout_width="match_parent"android:layout_height="match_parent"tools:context="${relativePackage}.${activityClass}"android:id="@+id/viewpager_main"android:layout_width="match_parent"android:layout_height="180dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:layout_alignBottom="@id/viewpager_main"android:background="#44000000"android:id="@+id/text_main_tatle"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="3dp"android:gravity="center_horizontal"android:text="广告题"android:textColor="#fff"android:id="@+id/ll_main_point_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_gravity="center_horizontal">
网友评论