到达底部时,嵌套滚动视图出现问题。 在nestedScrollView内部,我有一个按钮,在页面底部,我有一个输入字段,用于输入电话号码。当按下按钮时,如果电话号码字段未填写,我想滚动到活动底部以突出显示输入字段。 但是在我的活动中,它只滚动一半,直到最后才滚动。
我尝试了所有心爱的Java代码来解决此问题,但所有工作原理相同(未到达底部,停在中间)。
Java代码
CoordinatorLayout mainCoordinate = findViewById(R.id.mainCoordinate);
NestedScrollView nestedScrollView = findViewById(R.id.nestedScroll);
1。
//Scroll to bottom of view
nestedScrollView.post(new Runnable() {
@Override
public void run() {
nestedScrollView.smoothScrollTo(0, mainCoordinate.getBottom());
}
});
2。
//Scroll to bottom of view
nestedScrollView.post(new Runnable() {
@Override
public void run() {
nestedScrollView.fullScroll(View.FOCUS_DOWN);
}
});
3。
nestedScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final int scrollViewHeight = nestedScrollView.getHeight();
if (scrollViewHeight > 0) {
nestedScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
final View lastView = nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1);
final int lastViewBottom = lastView.getBottom() + nestedScrollView.getPaddingBottom();
final int deltaScrollY = lastViewBottom - scrollViewHeight - nestedScrollView.getScrollY();
/* If you want to see the scroll animation, call this. */
nestedScrollView.smoothScrollBy(0, deltaScrollY);
/* If you don't want, call this. */
nestedScrollView.scrollBy(0, deltaScrollY);
}
}
});
XML代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainCoordinate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:ignore="RtlHardcoded">
<android.support.design.widget.AppBarLayout
android:id="@+id/flexibleAppbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:visibility="invisible">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/flexible.example.collapsing"
android:layout_width="match_parent"
android:layout_height="258dp"
app:contentScrim="?colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/imageMain"
android:layout_width="match_parent"
android:layout_height="258dp"
android:foreground="@drawable/ripple_effect_circle_card"
android:src="@drawable/back_button_white" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:behavior_overlapTop="48dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:fitsSystemWindows="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/price.card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="bottom"
card_view:cardBackgroundColor="@color/card_back"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="8dp">
.........................
.........................
.........................
//My Other All Views
.........................
.........................
.........................
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile.card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="bottom"
card_view:cardBackgroundColor="@color/card_back"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:padding="5dp"
android:text="Phone No." />
<EditText
android:id="@+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:gravity="end"
android:hint="Enter your phone"
android:inputType="phone"
android:text=""/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<Button
android:id="@+id/btnBookNow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
</android.support.design.widget.CoordinatorLayout>
我附加了一段视频,解释了我的确切问题{{3}} 请,有没有人有更好的方法或其他建议来解决此问题?
预先感谢
答案 0 :(得分:0)
请尝试使用Android布局检查器检查NestedScroll的高度。
尝试添加
android:layout_gravity="bottom|center"
到Button。 android:layout_height="wrap_content"