• 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);
    解决办法
  • 相关阅读:
    Qt Create or VS 2015 使用 Opencv330 相机静态库链接错误如何解决?
    Qt create 如何构建 ActiveX 控件?
    VB 如何调用 c++ DLL?
    Opencv 330 如何裁剪图片中大的目标?
    Opencv 330 如何進行圖像的旋轉?
    Qt 开发 MS VC 控件终极篇
    Qt 5.9.4 如何静态编译和部署?
    图片理论基础
    iOS平台设置系统状态栏(通知栏、顶部状态栏)样式背景颜色或透明
    jQuery基础
  • 原文地址:https://www.cnblogs.com/rookiechen/p/5560811.html
Copyright © 2020-2023  润新知