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 ...
随机推荐
- openssl 第一篇
自从老罗赞助了openssl以及心脏出血事件的新闻,得以了解了openssl.那么什么是openssl呢?下面摘自官网: The OpenSSL Project is a collaborative ...
- POJ 1190 生日蛋糕(DFS)
生日蛋糕 Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit Status Descrip ...
- information_schema.triggers 学习
mysql实例中的每一个trigger 对应到information_schema.triggers 中有一行 1.information_schema.triggers 表的常用列: 1.trigg ...
- 简单的html5布局
<!DOCTYPE html><html><meta charset="utf-8"><head><style>html ...
- SmartBusinessDevFramework架构设计-3:考虑开源?
掖着藏着,终归不是好的办法.说的跟花一样,究竟里子是什么东西.一个好的被子,里料是羽绒还是棉花还是丝绵还是黑心棉?有时候,真的是看过之后,才能体验其中的奥秘. 这个架构的设计初衷,总体是为了方便.ne ...
- 【Xamarin挖墙脚系列:Xamarin.IOS的多个Storybord分隔视图的导航】
在实际开发中,我是推荐使用画板Storybord的.也可以适当的添加xib进行界面的绘制.尽量不要用或者少用代码绘制视图.(少一些装B分子,可以极大的缩短项目的周期). 不要讲性能,不是不讲性能,ap ...
- Java 舍入模式 数字的格式化
舍入模式: UP向远离0的方向舍入 始终对非零舍弃部分前面的数字加 1.此舍入模式始终不会减少计算值的绝对值. 例如:1.6 → 2 -1.6 → -2 1.1 → 2 ...
- Yii2 框架下bootstrap 弹窗预览视频等~
Yii2 本身已经引用了'yii\bootstrap\BootstrapAsset',所以使用bootstrap 非常简洁. 1 在PHP页面引用命名空间 use app\assets\AppAsse ...
- find命令笔记
find 命令: 文件查找:locate: 非实时,模糊匹配,查找是根据全系统文件数据库进行的:# updatedb, 手动生成文件数据库速度快 find: 实时 精确 支持众 ...
- Xshell不能连接SSH的解决
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 重新启动看看:/etc/init.d/ssh restart (/etc/ini ...