android 中的 scrollView 控件的默认位置和子控件的焦点冲突

最近在开发 dribbble 客户端, 遇到一个这样的问题。

在 scrollView 控件中,如果有子控件一开始就获得焦点,例如设置了 

android:focusable="true"

或者设置了 

android:textIsSelectable="true"

那么当 scrollView 第一次出现的时候,scrollView 会默认滑动到获得焦点的子控件位置,

android 中的 scrollView 控件的默认位置和子控件的焦点冲突_第1张图片

为了修正这个问题,应该在 scrollView 控件的第一个子控件设置如下属性

android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"

android 中的 scrollView 控件的默认位置和子控件的焦点冲突_第2张图片


布局文件

<ScrollView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:scrollbars="none"

   >

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

      <ImageView
         android:id="@+id/shot_imageview"
         android:layout_width="match_parent"
         android:layout_height="200dp"
         android:src="@drawable/shot_empty"
         android:focusable="true"
         android:focusableInTouchMode="true"
         android:descendantFocusability="beforeDescendants"
         />

      <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="@dimen/activity_vertical_margin"
         >

         <ImageView
            android:id="@+id/user_imageview"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/shot_empty"
            android:layout_marginRight="8dp"
            />

         <TextView
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/user_imageview"
            android:layout_marginBottom="8dp"
            android:text="Jon Ivy"/>

         <ImageView
            android:id="@+id/view_imageview"
            android:layout_width="@dimen/shot_count"
            android:layout_height="@dimen/shot_count"
            android:layout_toRightOf="@+id/user_imageview"
            android:layout_below="@+id/username"
            android:src="@mipmap/view_off"
            android:layout_marginRight="6dp"
            />

         <TextView
            android:id="@+id/view_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/view_imageview"
            android:layout_below="@+id/username"
            android:text="999"/>

      </RelativeLayout>

      <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="@dimen/activity_vertical_margin"
         >


         <LinearLayout
            android:id="@+id/comment"
            style="@style/count_style"
            >


            <ImageView
               android:layout_width="@dimen/shot_count"
               android:layout_height="@dimen/shot_count"
               android:padding="3dp"
               android:src="@mipmap/comment_off"
               />

            <TextView
               android:id="@+id/comment_textview"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="999"
               />

         </LinearLayout>

         <LinearLayout
            android:id="@+id/like"
            style="@style/count_style"
            >


            <ImageView
               android:layout_width="@dimen/shot_count"
               android:layout_height="@dimen/shot_count"
               android:padding="3dp"
               android:src="@mipmap/like"
               />

            <TextView
               android:id="@+id/like_textview"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="999"
               />

         </LinearLayout>

         <LinearLayout
            android:id="@+id/bucket"
            style="@style/count_style"
            >


            <ImageView
               android:layout_width="@dimen/shot_count"
               android:layout_height="@dimen/shot_count"
               android:src="@mipmap/bucket"
               android:padding="3dp"
               />

            <TextView
               android:id="@+id/bucket_textview"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="999"
               />

         </LinearLayout>

      </LinearLayout>

      <TextView
         android:id="@+id/descript_textview"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="@dimen/activity_vertical_margin"
         android:textIsSelectable="true"
         />
   </LinearLayout>
</ScrollView>






你可能感兴趣的:(scrollview,控件的默认位置,子控件的焦点冲突)