2014-01-09 39 views
1

我在ScrollView內有LinearLayoutLinearLayout前3個元素的可見性初始設置爲GONE。當我將可見性更改爲VISIBLE時,將滾動ScrollView。當第一個視圖變得可見時是否可以不滾動它?當其LinearLayout的第一個元素變爲可見時,ScrollView向上滾動

的代碼:

<com.mypackage.customWidgets.CustomScrollView 
      android:id="@+id/scrollView" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_alignParentBottom="true" > 

      <LinearLayout 
       android:id="@+id/layout2" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:orientation="vertical" 
       android:divider="@drawable/table_layout_horizontal_divider" 
       android:showDividers="middle|beginning" > 

       <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal" 
        style="@style/kidProfileServicesRowStyle"> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:layout_marginRight="@dimen/largeSpace" 
         android:layout_gravity="center" 
         android:src="@drawable/consult_now_kid_profile"/> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_weight="1" 
         android:layout_height="wrap_content" 
         android:text="@string/consultNow1Row" 
         android:layout_marginRight="@dimen/largeSpace" 
         android:layout_gravity="center_vertical"/> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:src="@drawable/doctors_arrow_right" 
         android:layout_gravity="right"/> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal" 
        style="@style/kidProfileServicesRowStyle"> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:layout_marginRight="@dimen/largeSpace" 
         android:layout_gravity="center" 
         android:src="@drawable/choose_a_specialist_kid_profile"/> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_weight="1" 
         android:layout_height="wrap_content" 
         android:text="@string/chooseSpecialist" 
         android:layout_marginRight="@dimen/largeSpace" 
         android:layout_gravity="center_vertical"/> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:src="@drawable/doctors_arrow_right" 
         android:layout_gravity="right"/> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal" 
        style="@style/kidProfileServicesRowStyle"> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:layout_marginRight="@dimen/largeSpace" 
         android:layout_gravity="center" 
         android:src="@drawable/other_services_kid_profile"/> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_weight="1" 
         android:layout_height="wrap_content" 
         android:text="@string/otherServices" 
         android:layout_marginRight="@dimen/largeSpace" 
         android:layout_gravity="center_vertical"/> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:src="@drawable/doctors_arrow_right"/> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal" 
        style="@style/kidProfileServicesRowStyle"> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:layout_marginRight="@dimen/largeSpace" 
         android:src="@drawable/medical_card_kid_profile"/> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_weight="1" 
         android:layout_height="wrap_content" 
         android:text="@string/medicalCard"/> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:src="@drawable/doctors_arrow_right" 
         android:layout_gravity="right"/> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal" 
        style="@style/kidProfileServicesRowStyle"> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:layout_marginRight="@dimen/largeSpace" 
         android:src="@drawable/more_about_kid"/> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_weight="1" 
         android:layout_height="wrap_content" 
         android:text="@string/moreAboutKid"/> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@null" 
         android:src="@drawable/doctors_arrow_right" 
         android:layout_gravity="right"/> 
       </LinearLayout> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@null" 
        android:src="@drawable/medical_card_kid_profile" 
        android:visibility="invisible" 
        android:layout_margin="@dimen/mediumSpace"/> 
      </LinearLayout> 
     </com.mypackage.customWidgets.CustomScrollView> 




public class CustomScrollView extends ScrollView 
{ 
    public CustomScrollView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     setHorizontalScrollBarEnabled(false); 
     setVerticalScrollBarEnabled(false); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) 
    { 
     return false; 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) 
    { 
     return false; 
    } 
} 

我使用,以顯示元件的代碼:對於那些3個元素

RelativeLayout.LayoutParams scrollViewParams = (RelativeLayout.LayoutParams) scrollView.getLayoutParams(); 
       scrollViewParams.height = scrollView.getHeight(); 
       layout2.getChildAt(2).setVisibility(View.VISIBLE); 
       layout2.getChildAt(1).setVisibility(View.VISIBLE); 
       layout2.getChildAt(0).setVisibility(View.VISIBLE); 
       scrollView.setLayoutParams(scrollViewParams); 
+0

發表您的XML代碼 –

+0

我沒有得到這查看您正試圖隱藏/顯示 –

+0

中的第一,第二和第三LinearLayouts 「layout2」LinearLayout。 – smb

回答

0

提及可聚焦屬性。像:

android:focusable="false" 

否則,在下面的方法customScrollView覆蓋:

@Override 
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 
return true; 
} 
+0

這沒有幫助... – smb

+0

將custom android:fillViewPort =「true」添加到customScrollview。 – user543

+0

android:fillViewPort用於其他目的... – smb

相關問題