本文要解决在侧滑菜单右边加个文本框,并能实现文本的上下滑动和菜单的左右滚动。这里推荐可以好好看看android的触摸事件的分发机制,这里我就不详细讲了,我只讲讲这个应用。要实现的功能就像UC浏览器(或其它手机浏览器)的左右滚动,切换网页,上下滚动,拖动内容。
目录:
一、功能要求与实现
二、布局与代码
三、原理与说明
?本文源码下载
------------------------------------------分割线------------------------------------------
具体下载目录在 /2015年资料/2月/18日/Android仿UC浏览器左右上下滚动功能(附源码)/
------------------------------------------分割线------------------------------------------
本文的效果:


一、功能要求与实现
1、功能要求:
(1)手指一开始按着屏幕左右移动时,只能左右滚动菜单,如果这时手指一直按着,而且上下移动了,那么菜单显示部分保持不变,但文本框也不上下移动!
(2)手指一开始按着屏幕上下移动时,只能上下滚动文本框,如果这时手指一直按着,而且左右移动了,那么文本框显示部分保持不变,但菜单也不左右移动!
2、实现:
在上一篇中,为左边的菜单项增加一个listview,为右边的内容项添加一个textview,并且为了能让它实现上下滚动的功能,给textview加了个scrollview
这种效果肯定是不对的,你看,我们手指上下禾移动文本时,如果还左右移动了,菜单也显示出来了


这时我就想从触摸事件的分发入手,这里因为我是把ScrollView的触摸事件注册到LinearLayout。(LinearLayout中包含了ScrollView,不懂看下面的布局)中去,所以触摸事件会先传递给LinearLayout。
分以下两种情况:
(1)如果是手指左右移动,则把触摸事件传给LinearLayout。函数onTouch返回true,表示触摸事件不再传递下去,那么ScrollView就动不了了
(2)如果是手指上下移动,触摸事件先传给LinearLayout,但LinearLayout不做任何处理,直接传递给ScrollView,ScrollView来处理触摸事件。
这是修改后的效果:

二、布局与代码
1、布局
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:id="@+id/layout"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="horizontal"
? ? tools:context=".MainActivity" >
? ? ? ? ? ? android:id="@+id/menu"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:orientation="vertical"
? ? ? ? android:background="@drawable/menu" >
? ? ? ?
? ? ? ? ? ? ? ? android:id="@+id/menuList"? ?
? ? ? ? android:layout_width="fill_parent"?
? ? ? ? android:layout_height="fill_parent"/>? ? ? ? ?
? ?
? ? ?
? ? ? ? ? ? android:id="@+id/content"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:orientation="vertical">?
? ? android:id="@+id/scrollview"
? ? android:layout_width="fill_parent"?
? ? android:layout_height="wrap_content" >?
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="@string/text1"
? ? ? ? ? ? android:textSize="22px" />
?
? ?
?
2、代码
/**
?* @作者? 林炳文
?* @时间 2015.2.17
?*/
package com.example.learningjava;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.example.learningjava.R.string;
import android.R.integer;
import android.R.menu;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.LinearLayout;
?
public class MainActivity extends Activity implements On