我已經在很多網站中檢查了這個概念,並且希望在我的應用中使用它。這個想法很簡單,有一個靜態背景,例如在應用程序中使用顏色作爲背景,當滾動到底時,它會在底部顯示一些樹。在到達NestedScrollView的末尾時顯示另一個背景
這就像將第二個背景放置在滾動視圖的底部。我試圖只放置一個背景,但它顯示在應用程序的底部,而不是在滾動視圖中。
有沒有辦法做到這一點?因爲我已經搜查,並沒有找到任何可靠的來源。
我已經在很多網站中檢查了這個概念,並且希望在我的應用中使用它。這個想法很簡單,有一個靜態背景,例如在應用程序中使用顏色作爲背景,當滾動到底時,它會在底部顯示一些樹。在到達NestedScrollView的末尾時顯示另一個背景
這就像將第二個背景放置在滾動視圖的底部。我試圖只放置一個背景,但它顯示在應用程序的底部,而不是在滾動視圖中。
有沒有辦法做到這一點?因爲我已經搜查,並沒有找到任何可靠的來源。
如果我理解正確,您可以使用此XML佈局實現您正在尋找的內容(或以編程方式構建相同的結構)。
你只需要把一個帶有繪圖的ImageView作爲NestedScrollView內ViewGroup底部的「src」(因爲ScrollViews只能有一個孩子)。你會得到一個ScrollView,裏面有一個ViewGroup,你的內容跟着你的圖像。
當用戶滾動到內容下方的底部時,圖像將從底部顯示。在下面的代碼中,我向您展示了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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_background">
<!-- YOU CAN SET YOUR APP BACKGROUND COLOR OR DRAWABLE UP HERE -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- YOUR CONTENT GOES HERE -->
<TextView android:text="Your content here instead of this TextView"
android:layout_width="match_parent"
android:layout_height="1500dp" />
<!-- YOUR FOOTER IMAGE HERE -->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/trees" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
如果你想在底部的圖像顯示了滾動內容,而不是下面背後,你需要刪除的ImageView在上面的XML,而是設置背景繪製到在的LinearLayout裏面NestedScrollView,並使用下面的XML資源繪製作爲背景:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/trees"
android:gravity="bottom|center_horizontal" />
此XML將創建一個新的可繪製時設置爲背景對齊到視圖的底部。只需保存在一個名爲trees_background.xml在繪製文件夾中的XML文件的代碼,並更改的LinearLayout有
非常感謝!它像一個魅力:) – Jaeger
不客氣!我很高興它幫助! –
您可以檢測到您是否通過以下方式達到NestedScrollView的底部或不是,然後可以應用你自己的邏輯來相應地改變背景。
NestedScrollView nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY < oldScrollY) {
// Scroll UP
} else if (scrollY > oldScrollY) {
//Scroll down
} else if (scrollY == 0) {
// TOP SCROLL
} else if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
//You have reached the bottom of nested scrollview
//Add you logic here to change background
}
}
});
希望我能幫到你。
你可以附加一些gif或視頻例如期望的行爲? – Gaket