LinearLayout遇到的问题——利用LinearLayout做横向滑动冲突
问题:当我添加两个TextView的时候,然后滑动,发现只生成了一个TextView。
就是
<?xml version="1.0" encoding="utf-8"?>
<com.maikefengchao.viewcompflict.HorzonScrollLinearLayout 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"
android:orientation="horizontal"
tools:context="com.maikefengchao.viewcompflict.MainActivity"> <TextView
android:id="@+id/main_tv_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:text="我是第一页"
android:gravity="center"/> <TextView
android:id="@+id/main_tv_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:text="第二页"
android:gravity="center"/>
</com.maikefengchao.viewcompflict.HorzonScrollLinearLayout>
activity_main
public class HorzonScrollLinearLayout extends LinearLayout {
private int mLastX;
private int mLastY;
private int mCurrentX;
private int mCurrentY;
private int mTouchLastX;
private int mTouchLastY; public HorzonScrollLinearLayout(Context context) {
super(context);
} public HorzonScrollLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
} public HorzonScrollLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
public boolean onTouchEvent(MotionEvent event) {
int scrollX = (int) event.getX();
int scrollY = (int) event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int deltaX = mLastX - scrollX;
scrollBy(deltaX,0);
break;
case MotionEvent.ACTION_UP:
break;
}
mLastX = scrollX;
mLastY = scrollY;
return true;
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean isIntercept = false;
mCurrentX = (int) ev.getX();
mCurrentY = (int) ev.getY(); switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
isIntercept = false;
break;
case MotionEvent.ACTION_MOVE:
//往左滑动为正
int deltaX = mLastX - mCurrentX;
int deltaY = mLastY - mCurrentY;
if(Math.abs(deltaX) > Math.abs(deltaY)){
isIntercept = true;
}
else {
isIntercept = false;
}
break;
case MotionEvent.ACTION_UP:
isIntercept = false;
break;
}
mLastX = mCurrentX;
mLastY = mCurrentY;
return isIntercept;
}
HorzonScrollLinearLayout
发生滑动的时候,只有第一个显示出来
为什么会这样子呢,因为我忘了LinearLayout的大小是屏幕的宽度,所以整体大小值有一个屏幕宽,第二个TextView因为第一个TextView占满的屏幕,根据LinearLayout的源码
就是当前容器的宽度大小 减去 上一子类View占用的控件,就是当前子类的可用空间。
解决办法:
1、在java文件中,将TextView加入LinearLayout中,并设置TextView的LayoutParam.width 为 屏幕宽度。
TextView textView = new TextView(this);
textView.getLayoutParams().width = screenWidth;
textView.getLayoutParams().height = screenHeight; mHorzonScroll.addView(textView);
解决办法
LinearLayout遇到的问题——利用LinearLayout做横向滑动冲突的更多相关文章
- 使用CollectionView做横向滑动分页效果:
一开始几页滑动是没有问题的,等滑到三四个页面之后,就出现奇怪的缝隙,一开始死活找不到原因,最后在layout的代理方法minimumLineSpacingForSectionAtIndex返回值设置为 ...
- Java基础-继承 利用接口做参数,写个计算器,能完成+-*/运算
38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 ...
- 利用SCI做的一个足球答题系统
SCI,异步串行通信接口,内置独立的波特率产生电路和SCI收发器,可以选择发送8或9个数据位(其中一位可以指定为奇或偶校验位). SCI是全双工异步串行通信接口,主要用于MCU与其他计算机或设备之间的 ...
- 利用toString做类型的判断
//利用toString做类型的判断 : /*var arr = []; alert( Object.prototype.toString.call(arr) == '[object Array]' ...
- 利用boost做string到wstring转换,以及字符集转换 - Error - C++博客
利用boost做string到wstring转换,以及字符集转换 - Error - C++博客 利用boost做string到wstring转换,以及字符集转换 #include <boost ...
- [转载] ul li css 做横向菜单
原文地址: http://www.cnblogs.com/amylis_chen/archive/2011/09/24/2188398.html 第一步:建立一个无序列表 我们先建立一个无序列表,来建 ...
- 利用Python做绝地科学家(外挂篇)
i春秋作家:奶权 前言 玩吃鸡时间长的鸡友们 应该都知道现在的游戏环境非常差 特别在高端局 神仙满天飞 搞得很多普通玩家非常没有游戏体验 因为吃鸡的火爆 衍生出了一条巨大的外挂利益链 导致市面上出 ...
- python idea 利用树莓派做家庭报警系统
1 利用树莓派做家庭报警系统idea 功能如下: 1.程序家侧人不在家(7:00-6:00) 2.树莓派搭配摄像头,对这门进行图像识别,如果变化,门开了,就报警: 3.报警的方式是给我发短信,采信,或 ...
- Unity利用AnimationCurve做物体的各种运动
之前一直都是自己学习Unity各种做Demo,最近开始正式使用Unity来做一个款2d的游戏. 其中在做一个类似小球弹跳运动的时候遇到了点问题,查找了很多资料,无意间发现AnimationCurve ...
随机推荐
- mysql按月,按日分组统计数据
group by DATE_FORMAT(createtime,'%Y-%m');//按月统计数据 group by DATE_FORMAT(createtime,'%Y-%m-%d');//按天统计 ...
- Hello Word!
第一次来博客园,作为技术的基站,多余的话不说了,就一个helloword! <script type="text/javascript"> //等待dom元素加载完毕. ...
- 学习javascript中this用法的一些感悟
要想真正的掌握this,应该先要掌握javascript代码执行上下文环境的创建.转换和销毁机制:
- UML中九种图的理解
1.用例图. 用例图是用来描述用户需求的,从用户的角度来描述系统的功能,并指出各个执行者.强调谁在使用,系统的执行者是谁. 2.类图. 用来定义系统中的类,包括描述类的结构和类之间的关系.类图的主要作 ...
- php读取和保存base64编码的图片内容
<?php header('Content-type:text/html;charset=utf-8'); //读取图片文件,转换成base64编码格式 $image_file = './429 ...
- hdu 1337 The Drunk Jailer
http://acm.hdu.edu.cn/showproblem.php?pid=1337 #include <cstdio> #include <cstring> #def ...
- scheme I/0 输入输出操作
2.1. open-input-file, read-char, and eof-object? The function (open-input-file filename) is availabl ...
- UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>
G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- Jump Game II 解答
Question Given an array of non-negative integers, you are initially positioned at the first index of ...
- LeeCode-Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...