水平滚动时禁用特定视图的垂直滚动

时间:2014-09-25 16:07:21

标签: android webview scrollview

我在滚动视图中有一个WebView,我想让webview中的滚动对用户更有经验 - 当你开始水平滚动>我想关闭垂直滚动。与此相反。我试过这个解决方案: https://stackoverflow.com/a/24734588 但它完全禁用了webview的垂直滚动,因此我无法向上或向下滚动。

我的代码:

package com.tachles;

import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class History_04_01 extends TextSet {

    WebView myweb;
    LinearLayout ll1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String formattedText = getString(R.string.buildcountry01);
        ll1 = (LinearLayout) findViewById(R.id.linear2);
        Spanned result = Html.fromHtml(formattedText);
        text.setText(result);

        myweb = new WebView(this);
        myweb.loadUrl("file:///android_asset/tavla.png");
        ll1.addView(myweb);

        // your HorizontalScrollView inside scrollview
        myweb.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;

                case MotionEvent.ACTION_UP:
                    // Allow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }

                // Handle HorizontalScrollView touch events.
                v.onTouchEvent(event);
                return true;
            }
        });

    }

    public void sizeUp(MenuItem item) {

        super.sizeUp(item);
    }

    public void sizeDown(MenuItem item) {

        super.sizeDown(item);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.textbar, menu);
        return true;
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onStop() {
        super.onStop();

    }
}

和XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

        <RelativeLayout
            android:id="@+id/linear1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:gravity="right" />

            <LinearLayout
                android:id="@+id/linear2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView1"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:orientation="vertical" >
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

0 个答案:

没有答案