将焦点更改为Scrollview的最后一个项目结束

时间:2017-11-07 04:29:23

标签: java android android-scrollview

我有scrollview包含自定义的相对布局,在点击时展开和折叠。我需要将scrollview位置更改为scrollview布局的最后一个位置。我试图通过以下代码滚动到最后但它仍然没有滚动到最后扩展。

scrollview.post(new Runnable() {
        @Override
        public void run() {
            main_content.scrollTo(0, scrollview.getBottom());
        }
    }); 

这是我的xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/md_white_1000"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/ll_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

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

                <WebView
                    android:id="@+id/web_contents"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp" />


            </LinearLayout>

            <RelativeLayout
                android:id="@+id/ll_referal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingEnd="16dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:paddingStart="16dp">

                <!--Expanding layout in which data are inflated on run time-->
                <LinearLayout
                    android:id="@+id/ll_link_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="18dp"
                    android:animateLayoutChanges="true"
                    android:background="@color/unselectedColor"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:gravity="top"
                    android:orientation="vertical"
                    android:paddingBottom="10dp"
                    android:paddingTop="15dp" />

                <!--Click to expand ll_link_container-->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="30dp">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="25dp"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="10dp"
                            android:layout_marginStart="10dp"
                            android:background="@drawable/referal_bg"
                            android:gravity="center"
                            android:text="@string/references"
                            android:textAllCaps="true"
                            android:textColor="@color/md_white_1000" />

                        <RelativeLayout
                            android:layout_width="30dp"
                            android:layout_height="match_parent"
                            android:background="@drawable/drawable_referal_iconbg">

                            <TextView
                                android:id="@+id/tv_add_bg"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_margin="5dp"
                                app:srcCompat="@drawable/add_white" />
                        </RelativeLayout>
                    </RelativeLayout>
                </LinearLayout>


            </RelativeLayout>
        </LinearLayout>

    </ScrollView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试以下方面的焦点位置:

listView.post(new Runnable() {

    @Override
    public void run() {
        listView.setSelection(focusedPosition);
        // position for highlight view
        View v = getViewByPosition(0, list);  
        v.setBackgroundResource(R.color.black); // highlight color
    }
});