文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android实现实时滑动ViewPager的2种方式

2022-06-06 07:03

关注

先看看效果图:

activity_main.xml 


<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="com.example.welcome.MainActivity" > 
 <android.support.v4.view.ViewPager 
 android:id="@+id/mViewPager" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" /> 
 <FrameLayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="30dp" > 
 <com.example.welcome.PagerCursor 
  android:id="@+id/pagerCursor" 
  android:layout_width="fill_parent" 
  android:layout_height="5dp" /> 
 </FrameLayout> 
 <FrameLayout 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignParentBottom="true" 
 android:layout_centerHorizontal="true" 
 android:layout_marginBottom="30dp" > 
 <com.example.welcome.CustomeDotGroup 
  android:id="@+id/ll_point_group" 
  android:layout_width="fill_parent" 
  android:layout_height="10dp" 
  android:orientation="horizontal" > 
 </com.example.welcome.CustomeDotGroup> 
 <View 
  android:id="@+id/red_point" 
  android:layout_width="10dp" 
  android:layout_height="10dp" 
  android:background="@drawable/point_red" /> 
 </FrameLayout> 
</RelativeLayout> 

MainActivity


package com.example.welcome; 
import java.util.ArrayList; 
import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.animation.TranslateAnimation; 
import android.widget.ImageView; 
public class MainActivity extends Activity { 
 private ViewPager mViewPager; 
 MyAdapter mAdapter; 
 private ArrayList<ImageView> imageViewList; 
 View red_point; 
 private int lastDis; 
 PagerCursor pagerCursor; 
 @SuppressWarnings("deprecation") 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 pagerCursor = (PagerCursor) findViewById(R.id.pagerCursor); 
 red_point = findViewById(R.id.red_point); 
 imageViewList = new ArrayList<ImageView>(); 
 int[] imagesInt = {R.drawable.p1,R.drawable.p2,R.drawable.p3}; 
 for (int i = 0; i < imagesInt.length; i++) { 
  ImageView imageView = new ImageView(MainActivity.this); 
  imageView.setBackgroundResource(imagesInt[i]); 
  imageViewList.add(imageView); 
 } 
 mViewPager = (ViewPager) findViewById(R.id.mViewPager); 
 mAdapter = new MyAdapter(); 
 mViewPager.setAdapter(mAdapter); 
 mViewPager.setOnPageChangeListener(new OnPageChangeListener() { 
  private boolean isDragging; 
  @Override 
  public void onPageSelected(int arg0) { 
  } 
  @Override 
  public void onPageScrolled(int position, float screenOffSet, int arg2) { 
  TranslateAnimation animation = null; 
  if (isDragging) { 
   int dp2px = PhoneUtils.dp2px(MainActivity.this, 20f); 
   int offSet = (int) (position*dp2px + dp2px*screenOffSet); 
   animation = new TranslateAnimation(lastDis, offSet, 0f, 0f); 
   animation.setDuration(200); 
   animation.setFillAfter(true); 
   red_point.startAnimation(animation); 
   lastDis = offSet; 
  } 
  float[] screenSize = MeasureUtil.getScreenSize(MainActivity.this); 
  float itemWidth = screenSize[0]/3; 
  pagerCursor.SetOffSet(position,screenOffSet,itemWidth); 
  } 
  @Override 
  public void onPageScrollStateChanged(int arg0) { 
  switch (arg0) { 
  case ViewPager.SCROLL_STATE_DRAGGING://滑动 
   isDragging = true; 
   break; 
  case ViewPager.SCROLL_STATE_IDLE://空闲 
   isDragging = false; 
   break; 
  default: 
   break; 
  } 
  } 
 }); 
 } 
 class MyAdapter extends PagerAdapter{ 
 @Override 
 public int getCount() { 
  return imageViewList.size(); 
 } 
 @Override 
 public boolean isViewFromObject(View arg0, Object arg1) { 
  return arg0 == arg1; 
 } 
 @Override 
 public void destroyItem(ViewGroup container, int position, Object object) { 
  container.removeView(imageViewList.get(position)); 
 } 
 @Override 
 public Object instantiateItem(ViewGroup container, int position) { 
  container.addView(imageViewList.get(position)); 
  return imageViewList.get(position); 
 } 
 } 
} 

CustomeDotGroup


package com.example.welcome; 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
public class CustomeDotGroup extends LinearLayout { 
 private Context context; 
 public CustomeDotGroup(Context context, AttributeSet attrs, int defStyle) { 
 super(context, attrs, defStyle); 
 this.context = context; 
 initRes(); 
 } 
 public CustomeDotGroup(Context context, AttributeSet attrs) { 
 this(context, attrs, 0); 
 } 
 public CustomeDotGroup(Context context) { 
 this(context, null); 
 } 
 private void initRes() { 
 int dotWidthOrHeight = PhoneUtils.dp2px(context, 10); 
 for (int i = 0; i < 3; i++) { 
  ImageView dotImageView = new ImageView(context); 
  dotImageView.setBackgroundResource(R.drawable.point_normal); 
  LayoutParams dotImageViewParams = new LayoutParams(dotWidthOrHeight, dotWidthOrHeight); 
  if (i != 0) { 
  dotImageViewParams.leftMargin = dotWidthOrHeight; 
  } 
  dotImageView.setLayoutParams(dotImageViewParams); 
  this.addView(dotImageView); 
 } 
 } 
}

 PagerCursor


package com.example.welcome; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.View; 
public class PagerCursor extends View { 
 Context context; 
 private Paint mPaint; 
 private int screenWidth; 
 private int desRight; 
 private float itemWidth; 
 public PagerCursor(Context context, AttributeSet attrs, int defStyle) { 
 super(context, attrs, defStyle); 
 // TODO Auto-generated constructor stub 
 } 
 public PagerCursor(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 this.context = context; 
 initRes(); 
 // TODO Auto-generated constructor stub 
 } 
 public PagerCursor(Context context) { 
 super(context); 
 // TODO Auto-generated constructor stub 
 } 
 private void initRes() { 
 float[] screenSize = MeasureUtil.getScreenSize((Activity) context); 
 screenWidth = (int) screenSize[0]; 
 Log.d("TAG", ",screenWidth" + screenWidth); 
 mPaint = new Paint(); 
 mPaint.setColor(getResources().getColor(R.color.contentPressColor)); 
 mPaint.setStyle(Paint.Style.FILL); 
 mPaint.setAntiAlias(true); 
 } 
 public void SetOffSet(int position, float screenOffSet, float itemWidth) { 
 int offSet = (int) (position * itemWidth + itemWidth * screenOffSet); 
 this.itemWidth = itemWidth; 
 desRight = offSet; 
 Log.d("TAG", "screenOffSet:" + screenOffSet + ",position" + position 
  + ",desRigh-->t" + desRight + ",itemWidth" + itemWidth); 
 invalidate(); 
 } 
 @Override 
 protected void onDraw(Canvas canvas) { 
 super.onDraw(canvas); 
 Log.d("TAG", "onDraw,desRigh-->t" + desRight + ",itemWidth" + itemWidth); 
 canvas.drawRect(desRight, 0f, desRight + itemWidth, 
  PhoneUtils.dp2px(context, 5), mPaint); 
 } 
} 

MeasureUtil


package com.example.welcome; 
import android.app.Activity; 
import android.util.DisplayMetrics; 
 
public final class MeasureUtil { 
  
 public static float[] getScreenSize(Activity activity) { 
 DisplayMetrics metrics = new DisplayMetrics(); 
 activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); 
 return new float[] { metrics.widthPixels, metrics.heightPixels }; 
 } 
} 

PhoneUtils


package com.example.welcome; 
import android.content.Context; 
public class PhoneUtils { 
 public static int dp2px(Context context,float dpValue){ 
 float scale = context.getResources().getDisplayMetrics().density; 
 return (int)(dpValue * scale +0.5f); 
 } 
} 

源码下载:http://xiazai.jb51.net/201610/yuanma/androidviewpaper(jb51.net).rar

您可能感兴趣的文章:Android中用RxJava和ViewPager实现轮播图Android开发基于ViewPager+GridView实现仿大众点评横向滑动功能Android ViewPager实现图片轮播效果Android使用ViewPager实现无限滑动效果Android开发实战之漂亮的ViewPager引导页Android 使用ViewPager实现图片左右循环滑动自动播放android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)Android利用ViewPager实现滑动广告板实例源码Android ViewPager相册横向移动的实现方法android配合viewpager实现可滑动的标签栏示例分享Android viewpage实现禁止滑动的功能


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯