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 ...
随机推荐
- 从外部导入jar包的三种方式
我们在用Eclipse开发程序的时候,经常要用到第三方jar包.引入jar包不是一个小问题,由于jar包位置不清楚,而浪费时间.下面配图说明3种Eclipse引入jar包的方式. 1.最常用的普通操作 ...
- poj 3164 Command Network
http://poj.org/problem?id=3164 第一次做最小树形图,看着别人的博客写,还没弄懂具体的什么意思. #include <cstdio> #include < ...
- LeetCode_Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- MSP430F149模拟IIC读写24C02程序
板子上设置了EEPROM存储器,型号为AT24C02.板子的硬件连接为:SCL--->P2.4,SDA--->P2.5.直接了当,贴上程序! ======================= ...
- javascript之尺寸,位置,溢出
一.offsetWidth:元素的宽度,包括边框,内容,内边距. 二.offsetHeight:元素的高度,包括边框,内容,内边距. 三.offsetLeft:元素的X坐标(相对于最近已定位的祖先元素 ...
- Theme.AppCompat.Light报错
style文件中的Theme.AppCompat.Light报错,Error retrieving parent for item: No resource found that matches th ...
- Path Sum 解答
Question Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...
- openstack vm image
1,openstack 基于iso生成镜像
- 【C++基础之十四】函参的缺省
可能会有这么一个函数,在大部分的情况下,我们不用给它传递参数,但在某些特殊情况下,我们需要给它传递参数,那怎么办呢? 简单啊,写两个一样的方法,一个带参,一个不带参... 这样也太没水准了.来点高端的 ...