国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android仿UC瀏覽器左右上下滾動(dòng)功能

2020-04-11 11:06:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文要解決在側(cè)滑菜單右邊加個(gè)文本框,并能實(shí)現(xiàn)文本的上下滑動(dòng)和菜單的左右滾動(dòng)。這里推薦可以好好看看android的觸摸事件的分發(fā)機(jī)制,這里我就不詳細(xì)講了,我只講講這個(gè)應(yīng)用。要實(shí)現(xiàn)的功能就像UC瀏覽器(或其它手機(jī)瀏覽器)的左右滾動(dòng),切換網(wǎng)頁(yè),上下滾動(dòng),拖動(dòng)內(nèi)容。
本文的效果:

 

一、功能要求與實(shí)現(xiàn)
1、功能要求:
(1)手指一開始按著屏幕左右移動(dòng)時(shí),只能左右滾動(dòng)菜單,如果這時(shí)手指一直按著,而且上下移動(dòng)了,那么菜單顯示部分保持不變,但文本框也不上下移動(dòng)!                      
(2)手指一開始按著屏幕上下移動(dòng)時(shí),只能上下滾動(dòng)文本框,如果這時(shí)手指一直按著,而且左右移動(dòng)了,那么文本框顯示部分保持不變,但菜單也不左右移動(dòng)!
2、初步實(shí)現(xiàn):
      左邊的菜單項(xiàng)增加一個(gè)listview,為右邊的內(nèi)容項(xiàng)添加一個(gè)textview,并且為了能讓它實(shí)現(xiàn)上下滾動(dòng)的功能,給textview加了個(gè)scrollview。
       這種效果肯定是不對(duì)的,你看,我們手指上下禾移動(dòng)文本時(shí),如果還左右移動(dòng)了,菜單也顯示出來(lái)了。

 

3、修改實(shí)現(xiàn)   
     這時(shí)我就想從觸摸事件的分發(fā)入手,這里因?yàn)槲沂前裇crollView的觸摸事件注冊(cè)到LinearLayout。(LinearLayout中包含了ScrollView,不懂看下面的布局)中去,所以觸摸事件會(huì)先傳遞給LinearLayout。
分以下兩種情況:
(1)如果是手指左右移動(dòng),則把觸摸事件傳給LinearLayout。函數(shù)onTouch返回true,表示觸摸事件不再傳遞下去,那么ScrollView就動(dòng)不了了
(2)如果是手指上下移動(dòng),觸摸事件先傳給LinearLayout,但LinearLayout不做任何處理,直接傳遞給ScrollView,ScrollView來(lái)處理觸摸事件。
這是修改后的效果:

                                             

二、布局與代碼
1、布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  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" >  <LinearLayout   android:id="@+id/menu"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical"   android:background="@drawable/menu" >   <!-- 添加一個(gè)ListView控件 -->    <ListView    android:id="@+id/menuList"    android:layout_width="fill_parent"    android:layout_height="fill_parent"/>    </LinearLayout>    <LinearLayout   android:id="@+id/content"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical"> <ScrollView  android:id="@+id/scrollview"  android:layout_width="fill_parent"  android:layout_height="wrap_content" >   <TextView android:id="@+id/content_text"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/text1"     android:textSize="22px" />  </ScrollView>  </LinearLayout>  </LinearLayout> 

2、代碼

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 OnTouchListener{    private LinearLayout menuLayout;//菜單項(xiàng)  private LinearLayout contentLayout;//內(nèi)容項(xiàng)  private LayoutParams menuParams;//菜單項(xiàng)目的參數(shù)  private LayoutParams contentParams;//內(nèi)容項(xiàng)目的參數(shù)contentLayout的寬度值    private int disPlayWidth;//手機(jī)屏幕分辨率  private float xDown;//手指點(diǎn)下去的橫坐標(biāo)  private float xMove;//手指移動(dòng)的橫坐標(biāo)  private float xUp;//記錄手指上抬后的橫坐標(biāo)  private float yDown;//手指點(diǎn)下去的縱坐標(biāo)  private float yMove;//手指移動(dòng)的縱坐標(biāo)    private VelocityTracker mVelocityTracker; // 用于計(jì)算手指滑動(dòng)的速度。  private float velocityX;//手指左右移動(dòng)的速度  public static final int SNAP_VELOCITY = 400; //滾動(dòng)顯示和隱藏menu時(shí),手指滑動(dòng)需要達(dá)到的速度。   private boolean menuIsShow = false;//初始化菜單項(xiàng)不可

主站蜘蛛池模板:
阳江市|
饶河县|
芮城县|
南充市|
池州市|
香格里拉县|
延吉市|
中西区|
辽中县|
杭州市|
夏津县|
左贡县|
伊宁县|
临泽县|
庐江县|
顺昌县|
洛宁县|
周宁县|
兰西县|
桃源县|
海口市|
化州市|
大庆市|
东城区|
云林县|
株洲县|
那曲县|
奉化市|
都安|
峨眉山市|
两当县|
澄江县|
耿马|
大港区|
枞阳县|
腾冲县|
额尔古纳市|
汉川市|
洪泽县|
廉江市|
肃宁县|