当LinearLayout的第一个元素变为可见时,ScrollView会向上滚动

时间:2014-01-09 11:01:21

标签: android scrollview visibility

LinearLayout内有ScrollViewLinearLayout的前3个元素的可见性初始设置为GONE。当我将可见性更改为VISIBLE时,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;
    }
}

我用来显示元素的代码:

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);

1 个答案:

答案 0 :(得分:0)

提及这3个元素的可聚焦属性。像:

android:focusable="false"

否则,在customScrollView中覆盖以下方法:

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