首页 > 解决方案 > ScrollView SmoothScrollBy 不显示最后一项

问题描述

更新 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100">
    <!--App Bar Layout Pane-->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7"
        android:background="@drawable/AppBarBackground"
        android:weightSum="100">

    <!--Unrelated XML Code-->

    </LinearLayout>
    <!--App Bar Layout Pane end-->
    <!--Body Pane-->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="93"
        android:weightSum="100"
        android:background="@drawable/AppBackground">
    <!--Left Pane-->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="37"
            android:background="@color/whiteWithAlpha75">

    <!--Unrelated XML Code-->

        </LinearLayout>
    <!--Left Pane end-->
    <!--Right Pane-->
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="43">
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="100">
            <!--Right Pane Header-->
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="8"
                    android:weightSum="100"
                    android:background="@color/whiteWithAlpha10"
                    android:paddingLeft="15dp"
                    android:paddingRight="15dp">
                    <TextView
                        android:text="Item(s)"
                        android:fontFamily="@string/fontFamily"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="24"
                        android:textColor="@color/grayDark"
                        android:gravity="center|left"
                        android:textSize="@dimen/textSizeXSmall" />
                    <TextView
                        android:text="Unit Price"
                        android:fontFamily="@string/fontFamily"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="17"
                        android:textColor="@color/grayDark"
                        android:gravity="center"
                        android:textSize="@dimen/textSizeXSmall" />
                    <TextView
                        android:text="Quantity"
                        android:fontFamily="@string/fontFamily"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="28"
                        android:textColor="@color/grayDark"
                        android:gravity="center"
                        android:textSize="@dimen/textSizeXSmall" />
                    <TextView
                        android:text="Discount"
                        android:fontFamily="@string/fontFamily"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="16"
                        android:textColor="@color/grayDark"
                        android:gravity="center"
                        android:textSize="@dimen/textSizeXSmall" />
                    <TextView
                        android:text="Total"
                        android:fontFamily="@string/fontFamily"
                        android:gravity="center|right"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="15"
                        android:textColor="@color/grayDark"
                        android:textSize="@dimen/textSizeXSmall" />
                </LinearLayout>
            <!--Right Pane Header end-->
            <!--Right Pane Item Selected-->
                <android.support.v4.widget.NestedScrollView
                    android:id="@+id/scrollItemSelectedArea"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="92"
                    android:paddingBottom="5dp"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    android:background="@color/whiteWithAlpha15">
                    <LinearLayout
                        android:id="@+id/llItemSelectedArea"
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="5dp" />
                </android.support.v4.widget.NestedScrollView>
            <!--Right Pane Item Selected end-->
            <!--Right Pane Functions-->
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="39"
                    android:weightSum="100">

    <!--Unrelated XML Code-->

                </LinearLayout>
            <!--Right Pane Functions end-->
            </LinearLayout>
        </FrameLayout>
    <!--Right Pane end-->
    </LinearLayout>
<!--Body Pane end-->
</LinearLayout>

当列表中的项目达到滚动视图高度的末尾时,它应该进行滚动以显示最后选择的项目,我在每个项目选择中调用 smoothScrollBy。但是当我有例如 6 个项目并且我的滚动视图只能在屏幕上容纳 5 个项目时,它不会向上滚动第 6 个项目。当我添加另一个项目时,它现在显示第 6 个项目,但第 7 个项目刚刚取代了第 6 个项目被隐藏。我的滚动视图高度是matched_pa​​rent,底部没有重叠布局,所以最后一个项目没有地方可以隐藏。我不知道滚动视图中是否有额外的空间。

我该如何解决这个问题?

这是我添加新项目的地方

    public void renderItemSelected(ENT_TransactionItems entityTransactionItem, string displayName)
    {
        entityTransactionItemList.Add(entityTransactionItem);

        View vNewView = activity.LayoutInflater.Inflate(Resource.Layout.Layout_ItemSelected, llItemSelected, false);

        TextView tvItemName = vNewView.FindViewById<TextView>(Resource.Id.tvItemName);
        TextView tvItemAmount = vNewView.FindViewById<TextView>(Resource.Id.tvItemPrice);
        EditText etItemQuantity = vNewView.FindViewById<EditText>(Resource.Id.tvItemQuantity);
        Button btnDiscount = vNewView.FindViewById<Button>(Resource.Id.btnDiscount);
        TextView tvTotal = vNewView.FindViewById<TextView>(Resource.Id.tvTotal);

        //other code

        llItemSelected.AddView(vNewView);
        viewList.Add(vNewView);

        scrollItemSelectedArea.SmoothScrollBy(0, llItemSelected.Height);
    }

这是示例图像

在此处输入图像描述

标签: androidxamarinxamarin.androidandroid-scrollview

解决方案


推荐阅读