handler实现页面切换,小红点跟随移动
来源:互联网 发布:mac图片文件夹在哪里 编辑:程序博客网 时间:2024/06/04 19:27
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--<com.znouy.viewpagerdemo.view.AutoShowView-->
<!--android:id="@+id/asv"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="200dp" >-->
<!--</com.znouy.viewpagerdemo.view.AutoShowView>-->
<!--android:id="@+id/asv"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="200dp" >-->
<!--</com.znouy.viewpagerdemo.view.AutoShowView>-->
<com.example.lenovo.lianxi.view.AutoShowView
android:id="@+id/asv"
android:layout_width="match_parent"
android:layout_height="200dp">
android:id="@+id/asv"
android:layout_width="match_parent"
android:layout_height="200dp">
</com.example.lenovo.lianxi.view.AutoShowView>
</RelativeLayout>
<?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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 轮播图 -->
<android.support.v4.view.ViewPager
android:id="@+id/vp_lunbo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<!-- 文字描述和点的容器点和容器 -->
android:id="@+id/vp_lunbo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<!-- 文字描述和点的容器点和容器 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#22000000"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#22000000"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个点的描述"
android:textColor="#ffffff" />
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个点的描述"
android:textColor="#ffffff" />
<LinearLayout
android:id="@+id/ll_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
android:id="@+id/ll_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
protected static final String tag = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protected static final String tag = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
}
public class PicturePagerAdapter extends PagerAdapter {
private static final String tag = "PicturePagerAdapter";
List<ImageView> list;
private static final String tag = "PicturePagerAdapter";
List<ImageView> list;
public PicturePagerAdapter(List<ImageView> imageViews) {
this.list = imageViews;
}
this.list = imageViews;
}
@Override
public int getCount() {
// 确定要显示的数据量
return Integer.MAX_VALUE;// 无限轮播
}
public int getCount() {
// 确定要显示的数据量
return Integer.MAX_VALUE;// 无限轮播
}
@Override
public boolean isViewFromObject(View view, Object object) {
// 判断当前要显示的view是否等于标记object,true则显示
return view == object;
}
public boolean isViewFromObject(View view, Object object) {
// 判断当前要显示的view是否等于标记object,true则显示
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % list.size();
Log.d(tag, "instantiateItem==" + position);
// 无限轮播-因为position的值从0~Integer.MAX_VALUE
View view = list.get(position);
// 添加view到VIewPager中
container.addView(view);
return view;
}
public Object instantiateItem(ViewGroup container, int position) {
position = position % list.size();
Log.d(tag, "instantiateItem==" + position);
// 无限轮播-因为position的值从0~Integer.MAX_VALUE
View view = list.get(position);
// 添加view到VIewPager中
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// 从容器中移除view
Log.d(tag, "destroyItem==" + position);
container.removeView((View) object);
}
}
public void destroyItem(ViewGroup container, int position, Object object) {
// 从容器中移除view
Log.d(tag, "destroyItem==" + position);
container.removeView((View) object);
}
}
public class AutoShowView extends FrameLayout {
private View view;
private ViewPager vp_lunbo;
private TextView tv_desc;
private LinearLayout ll_points;
private List<ImageView> datas;
private PicturePagerAdapter adapter;
private Handler handler;
private View view;
private ViewPager vp_lunbo;
private TextView tv_desc;
private LinearLayout ll_points;
private List<ImageView> datas;
private PicturePagerAdapter adapter;
private Handler handler;
final AutoScrollTask autoScrollTask = new AutoScrollTask();
protected int selectIndex;
protected int selectIndex;
public AutoShowView(Context context) {
this(context, null);
}
}
public AutoShowView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
this(context, attrs, 0);
}
public AutoShowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
initData();
initListener();
}
super(context, attrs, defStyleAttr);
initView(context);
initData();
initListener();
}
private void initView(Context context) {
view = LayoutInflater.from(context).inflate(R.layout.view_autoshow,
this);
view = LayoutInflater.from(context).inflate(R.layout.view_autoshow,
this);
vp_lunbo = (ViewPager) view.findViewById(R.id.vp_lunbo);
tv_desc = (TextView) view.findViewById(R.id.tv_desc);
ll_points = (LinearLayout) view.findViewById(R.id.ll_points);
tv_desc = (TextView) view.findViewById(R.id.tv_desc);
ll_points = (LinearLayout) view.findViewById(R.id.ll_points);
datas = new ArrayList<ImageView>();
adapter = new PicturePagerAdapter(datas);
if (handler == null) {
handler = new Handler();
}
}
adapter = new PicturePagerAdapter(datas);
if (handler == null) {
handler = new Handler();
}
}
private void initData() {
// 初始化图片数据
initImageView();
// 初始化点数据
initPoints();
// 初始化图片数据
initImageView();
// 初始化点数据
initPoints();
vp_lunbo.setAdapter(adapter);
// 设置ViewPager当前选中的位置,会调用instantiateItem,最终会选中item0
// 原理:一个数减去其余数后肯定能被除数整除
vp_lunbo.setCurrentItem(10000 - 10000 % datas.size());
// 设置ViewPager当前选中的位置,会调用instantiateItem,最终会选中item0
// 原理:一个数减去其余数后肯定能被除数整除
vp_lunbo.setCurrentItem(10000 - 10000 % datas.size());
autoScrollTask.start();
}
}
private void initImageView() {
for (int i = R.drawable.icon1; i < R.drawable.icon1 + 4; i++) {
// 创建ImageView容器
ImageView imageView = new ImageView(getContext());
for (int i = R.drawable.icon1; i < R.drawable.icon1 + 4; i++) {
// 创建ImageView容器
ImageView imageView = new ImageView(getContext());
// 设置图片
imageView.setImageResource(i);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);// 图片填充容器
datas.add(imageView);// 添加到集合中
}
}
imageView.setImageResource(i);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);// 图片填充容器
datas.add(imageView);// 添加到集合中
}
}
private void initPoints() {
// 因为每次页面改变都会调用该方法,所以进入该方法时先清空集合。
ll_points.removeAllViews();
// 因为每次页面改变都会调用该方法,所以进入该方法时先清空集合。
ll_points.removeAllViews();
for (int i = 0; i < datas.size(); i++) {
View view = new View(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
10, 10);
if (i == selectIndex) {
tv_desc.setText("图片" + i + "的描述");
view.setBackgroundResource(R.drawable.point_red);
} else {
view.setBackgroundResource(R.drawable.point_white);
}
params.leftMargin = 10;
view.setLayoutParams(params);// 设置布局参数
ll_points.addView(view);// 添加到点的容器中
}
}
View view = new View(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
10, 10);
if (i == selectIndex) {
tv_desc.setText("图片" + i + "的描述");
view.setBackgroundResource(R.drawable.point_red);
} else {
view.setBackgroundResource(R.drawable.point_white);
}
params.leftMargin = 10;
view.setLayoutParams(params);// 设置布局参数
ll_points.addView(view);// 添加到点的容器中
}
}
private void initListener() {
// viewpager注册页面改变监听器
vp_lunbo.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// 页面选中调用
selectIndex = position % datas.size();
initPoints();// 红点跟着页面改变而移动
}
// viewpager注册页面改变监听器
vp_lunbo.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// 页面选中调用
selectIndex = position % datas.size();
initPoints();// 红点跟着页面改变而移动
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) { // 页面滑动就调用
}
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) { // 页面滑动就调用
}
@Override
public void onPageScrollStateChanged(int state) {
// 页面状态改变调用
public void onPageScrollStateChanged(int state) {
// 页面状态改变调用
}
});
});
// viewpager 注册触摸事件监听器,实现自动轮播
vp_lunbo.setOnTouchListener(new OnTouchListener() {
vp_lunbo.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 按下停止轮播
autoScrollTask.stop();
break;
case MotionEvent.ACTION_MOVE:
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 按下停止轮播
autoScrollTask.stop();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:// 松开,取消 开始轮播
case MotionEvent.ACTION_CANCEL:
autoScrollTask.start();
break;
case MotionEvent.ACTION_UP:// 松开,取消 开始轮播
case MotionEvent.ACTION_CANCEL:
autoScrollTask.start();
break;
default:
break;
}
return false;// 不会影响其他view的事件分发
}
});
}
break;
}
return false;// 不会影响其他view的事件分发
}
});
}
/** 实现轮播图自动轮播 */
class AutoScrollTask implements Runnable {
@Override
public void run() {
int currentItem = vp_lunbo.getCurrentItem();
currentItem++;
vp_lunbo.setCurrentItem(currentItem);
start();
}
public void run() {
int currentItem = vp_lunbo.getCurrentItem();
currentItem++;
vp_lunbo.setCurrentItem(currentItem);
start();
}
/**
* 开始轮播
*/
public void start() {
handler.postDelayed(this, 2000);
}
* 开始轮播
*/
public void start() {
handler.postDelayed(this, 2000);
}
/**
* 停止轮播
*/
public void stop() {
handler.removeCallbacks(this);
}
}
* 停止轮播
*/
public void stop() {
handler.removeCallbacks(this);
}
}
}
阅读全文
0 1
- handler实现页面切换,小红点跟随移动
- 十分经典de实现跟随页面移动的层
- 基于zepto.js的移动端H5单页面跟随手指滑动切换控件pageSlider
- unity 实现宠物跟随移动
- 实现宠物跟随移动效果
- JS跟随页面移动的div
- 在一个Activity页面中View跟随手指移动,并且能实现点击效果
- android 移动开发 手势切换页面实现动画效果
- 实现界面跟随鼠标移动的实例
- JS实现图片跟随鼠标移动
- 游戏人物跟随鼠标移动的实现
- JS实现控件跟随鼠标移动
- Unity 代码实现物体跟随鼠标移动
- 实现跟随手指移动的小球
- 实现跟随手指移动的小球
- Handler线程切换实现原理
- JavaScript实现跟随鼠标移动而移动的文字
- 实现跟随手指移动的小球—跟随移动颜色变化哦!
- FYN OI奋斗之路5~
- [LeetCode] 119: Triangle
- [LeetCode] 120: Two Sum
- [LeetCode] 121: Unique Binary Search Trees
- [LeetCode] 122: Unique Binary Search Trees II
- handler实现页面切换,小红点跟随移动
- [LeetCode] 123: Unique Paths
- 国外物联网平台初探(三) ——IBM Watson IoT
- [LeetCode] 124: Unique Paths II
- eclipse构建maven的web项目
- [LeetCode] 125: Valid Number
- [LeetCode] 126: Valid Palindrome
- [LeetCode] 127: Valid Parentheses
- nginx内存池c++代码及测试