2017-04-12 1 views
-1

많은 웹 사이트에서이 개념을 확인한 후이를 내 앱에서 만들고 싶습니다. 아이디어는 간단합니다. 정적 배경 (예 : 앱의 배경 색상)이 있고 스크롤이 끝 부분에있을 때 하단에 몇 개의 나무가 표시됩니다.NestedScrollView의 끝에 도달하면 다른 배경 표시

두 번째 배경을 Scrollview 하단에 배치하는 것과 같습니다. 배경을 배치하려했지만 화면 아래에 표시되지만 Scrollview에는 표시되지 않습니다.

이 방법이 있습니까? 나는 이미 수색했기 때문에 신뢰할 수있는 출처를 찾지 못했습니다.

+0

원하는 동작의 일부 gif 또는 비디오 예제를 첨부 할 수 있습니까? – Gaket

답변

1

제대로 이해했다면이 XML 레이아웃을 사용하여 원하는 것을 얻을 수 있습니다 (또는 프로그래밍 방식으로 동일한 구조체를 구축 할 수 있음).

NestedScrollView 내부의 ViewGroup 하단에 드로어 블이있는 ImageView를 "src"로두기 만하면됩니다 (ScrollView는 하나의 자식 만 가질 수 있음). ViewGroup이 내부에 있고 ScrollView 뒤에 내용이있는 이미지가있는 ScrollView를 얻을 수 있습니다.

사용자가 콘텐츠 아래쪽으로 스크롤하면 이미지가 아래쪽에서 나타납니다. 아래 코드에서 XML을 보여주고 앱의 배경과 스크롤 뷰의 아래쪽 위치 및 콘텐츠 배치 위치를 보여줍니다. 당신이 바닥 에있는 이미지 대신 아래의있는 ScrollView 내용 뒤에 표시 할 경우

<?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> 

, 당신은 위의 XML에 이미지 뷰를 제거 필요하고, 대신 받는 당김 배경을 설정 NestedScrollView 내부에있는 LinearLayout, 그리고 배경으로 다음과 같은 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 파일에서 해당 코드를 저장하고, 그런 다음 수는 다음과 같은 방법으로 NestedScrollView의 바닥에 도달하지 않았거나 여부를 검색 할 수 있습니다

+0

고마워요! 그것은 매력처럼 작동했습니다 :) – Jaeger

+0

당신을 환영합니다! 나는 그것이 도움이 되었기 때문에 기쁘다! –

1

잘 이해했다면 원하는 동작을 얻는 가장 간단한 방법은 재활용 업체의 어댑터에 ViewType을 추가하고이 항목을 항목 목록 끝에 추가하는 것입니다. Recycler View의 바닥 글로 작동하므로 사용자가 맨 아래로 스크롤하면 나무 나 다른 것이 보입니다.

+0

바닥 글 같은 것을 리사이틀 러 뷰에 추가하는 방법을 모르는 경우 예제를 제공 할 수 있습니다. – Gaket

+0

나는 내 질문을 다시 설명하기 위해 확인하는 예제를 원한다. 두 개의 배경, 하나는 전체 배경으로 표시되며 스크롤이 끝나면 두 번째 배경이 아래쪽에 표시됩니다. – Jaeger

1

을 할 수있는 LinearLayout 변경 자신의 논리를 적용하여 그에 따라 배경을 변경하십시오.

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

희망 사항이 맞습니다.

관련 문제