王学岗左右滚动
来源:互联网 发布:如何映射网络驱动器 编辑:程序博客网 时间:2024/05/06 19:53
左右滑动的滚动条,先看下效果
最重要的一个知识点是如何消除滑动惯性
不罗嗦了,直接上代码!
先看MainActivity 类
package com.tz.katescrollview_tz;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.DisplayMetrics;import android.view.ActionMode;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;public class MainActivity extends Activity implements OnTouchListener { private LinearLayout first, second, third; private HorizontalScrollView hsv; private int width; private int height; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } // 根据屏幕的高度设置每一个页面的高度 private void initView() { // 获取屏幕的宽度 DisplayMetrics outMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(outMetrics); width = outMetrics.widthPixels; System.out.println(width); height = outMetrics.heightPixels; first = (LinearLayout) findViewById(R.id.first); second = (LinearLayout) findViewById(R.id.second); third = (LinearLayout) findViewById(R.id.third); // 通过代码动态设置每个页面的宽 first.setMinimumWidth((int) (width * 0.8)); second.setMinimumWidth(width); third.setMinimumWidth(width); hsv = (HorizontalScrollView) findViewById(R.id.hsv); // 为HorizontalScrollView设置事件。 hsv.setOnTouchListener(this); } Handler handler = new Handler() { public void handleMessage(Message msg) { int distance = msg.arg1; hsv.smoothScrollTo(distance, 0); }; }; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { // 当鼠标松开的时候决定页面是否跳转 case MotionEvent.ACTION_UP: // 滚动控件滑动过的距离 int scrollX = (int) hsv.getScrollX(); // 滚动控件最终滑动的距离 int scrollDistance = 0; // 1,滑动控件滑动过的距离不大于半个屏幕的距离,停留在第一个页面 if (scrollX < 0.5 * width) { scrollDistance = 0; System.out.println(scrollX + ":" + scrollDistance); } else if (scrollX > 0.5 * width && scrollX <= 1.5 * width) { // 2.停留在第二个页面 scrollDistance = (int) (0.8 * width); System.out.println(scrollX + ":" + scrollDistance); } else { // 3,停留在第三个页面 scrollDistance = (int) (1.8 * width); System.out.println(scrollX + ":" + scrollDistance); } // 该方法有滑动惯性(就是滑动停止了依旧会向前滑动一段距离),立即滑动到指定位置,滑动速度快,所以 有惯性 // hsv.scrollTo(scrollDistance, 0); // hsv.smoothScrollTo(scrollDistance, 0);// 缓慢滑动到指定位置, 但也有惯性 // 注意我们通过handler机制解决关系惯性问题 Message msg = handler.obtainMessage(); msg.arg1 = scrollDistance; handler.sendMessage(msg); break; default: break; } return false; }}
这是.xml文件
<!-- 内容可以左右滚动的视图 :ListView --><HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/hsv" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <!-- 第1个孙子 --> <LinearLayout android:id="@+id/first" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#440000ff" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱装B" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱LOL" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱跳舞" /> </LinearLayout> <!-- 第2个孙子 --> <LinearLayout android:id="@+id/second" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#4400ff00" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱足球" /> </LinearLayout> <!-- 第3个孙子 --> <LinearLayout android:id="@+id/third" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#44ff0000" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱篮球" /> </LinearLayout> </LinearLayout></HorizontalScrollView>
0 0
- 王学岗左右滚动
- QT 字幕滚动(左右滚动)
- 图片左右滚动
- 左右滚动图片
- Javascript无缝左右滚动
- 文字左右滚动标签
- js 左右滚动幻灯片
- js-左右滚动
- 图片左右滚动代码
- 左右无缝滚动图片
- jquery 左右滚动
- js图片左右滚动
- JQuery左右滚动插件
- ViewPager 左右 上下滚动
- js 左右无缝滚动
- superSlide显示左右滚动
- 新闻无缝滚动 左右
- 文字左右无缝滚动
- 路上歇息一下
- 1035. Password (20)
- Hibernate-HQL基础
- RSA加密
- 联想Y450电源管理无法调节屏幕亮度的解决办法
- 王学岗左右滚动
- 1036. Boys vs Girls (25)
- iOS本地推送(本地通知)
- 亲密接触Redis-第二天(Redis Sentinel)
- svn版本控制心得
- cocos2d-x中的Slider控件
- HDU 2089 不要62(数位dp&记忆化搜索)
- 集群环境下的问题
- oracle数据库学习(一)