Как предотвратить прокрутку прокрутки в браузере после загрузки данных?
Итак, у меня есть захватывающая проблема. Несмотря на то, что я не выполняю рутинную или программную прокрутку своего представления, мой WebView автоматически прокручивается после загрузки данных внутри него.
У меня есть фрагмент в viewpager. Когда я впервые загружаю пейджер, он работает так, как ожидалось, и все отображается. Но как только я "переворачиваю страницу", данные загружаются, и WebView всплывает вверху страницы, скрывая над ним представления, что нежелательно.
Кто-нибудь знает, как предотвратить это?
Мой макет выглядит так:
<?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 после его загрузки. Как это предотвратить?
Ответы
Ответ 1
Вы можете просто добавить это в свой LinearLayout: android: focusableInTouchMode = "true". Это работает для меня.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical" >
Ответ 2
У меня была такая же проблема, после нескольких попыток попробовать несколько идей, что в конечном итоге сработало для меня, просто добавило атрибут descendantFocusability
к ScrollView, содержащему LinearLayout, со значением blockDescendants
. В вашем случае:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
У вас не возникла проблема с повторением:)
Ответ 3
Вы должны создать новый класс, расширяющий ScrollView, затем Override 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 больше не будет прокручивать веб-интерфейс.
Ответ 4
Добавление этой строки в основной макет решает проблему
android:descendantFocusability="blocksDescendants"
Ответ 5
Вот так:
<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" >
Ответ 6
Возможно, есть люди, у которых такая же проблема, что и у меня, поэтому я помогу.
Я пытался установить 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 */
Ответ 7
Мне нужно было использовать полное имя для MyScrollView, иначе я получил исключение раздувания.
<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" >