Как предотвратить прокрутку scrollview в webview после загрузки данных?



Итак, у меня есть увлекательная проблема. Несмотря на то, что я не вручную или программно прокручиваю свое представление, Мой WebView автоматически прокручивается после загрузки данных внутри него.



У меня есть фрагмент в viewpager. Когда я впервые загружаю пейджер, он работает так, как ожидалось, и все показано. Но как только я "переворачиваю страницу", данные загружаются, и веб-представление появляется в верхней части страницы, скрывая представления над ней, что нежелательно.



тут кто-нибудь знает, как предотвратить это?



мой макет выглядит так:



<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="2dp"
android:text="Some Title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/article_title"
android:textStyle="bold" />

<LinearLayout
android:id="@+id/LL_Seperator"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@color/text"
android:orientation="horizontal" >
</LinearLayout>

<WebView
android:id="@+id/article_content"
android:layout_width="match_parent"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/article_link"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:text="View Full Article"
android:textColor="@color/article_title"
android:textStyle="bold" />
</LinearLayout>

</ScrollView>


Я также не уделяю внимания ничему. По умолчанию он автоматически прокручивается в WebView после его загрузки. Как мне это предотвратить?

516   7  

7 ответов:

вы можете просто добавить это в свой LinearLayout: android: focusableInTouchMode="true". Это работает на меня.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

у меня была та же проблема, после нескольких часов попыток нескольких идей, что, наконец, сработало для меня, просто добавив descendantFocusability атрибут ScrollView, содержащий LinearLayout, со значением blockDescendants. В вашем случае:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants" >

не было проблема повторяется с тех пор:)

вы должны создать новый класс extend ScrollView, а затем переопределить requestChildFocus:

public class MyScrollView extends ScrollView {

@Override 
public void requestChildFocus(View child, View focused) { 
    if (focused instanceof WebView ) 
       return;
    super.requestChildFocus(child, focused);
}
}

затем в вашем XML-макете, используя:

<MyScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background" >

это работает для меня. ScrollView больше не будет автоматически прокручивать веб-представление.

добавление этих строк в основной макет решает проблему

android:descendantFocusability="blocksDescendants"

такой:

<com.ya.test.view.MyScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="beforeDescendants"
        android:focusable="true"
        android:focusableInTouchMode="true" >

вероятно, есть люди, у которых есть та же проблема, что и у меня, поэтому я помогу.

Я пытался поставить android: descendantFocusability= "beforeDescendants" в моем основном ScrollView следующим образом:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants">
    /*my linearlayout or whatever to hold the views */

и это не работало, поэтому мне пришлось сделать RelativeLayout родитель ScrollView, и поместите android: descendantFocusability= "beforeDescendants" в родителе также.

поэтому я решил это делать следующее:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
/*my linearlayout or whatever to hold the views */

мне пришлось использовать полное имя для MyScrollView,иначе я получил исключение inflate.

<com.mypackagename.MyScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background" >

Comments

    Ничего не найдено.