Code Example

Android Observable ScrollView Examples

Learn how to create an observable scrollview using the following examples

1. Android ObservableScrollView

This ObservableScrollView is a ScrollView with scroll state change observer.

Here is how you use it:

Step 1: Install it

In your app/build.gradle declare it as a dependency:

dependencies {
    ...
    implementation 'com.jzj.view:ObservableScrollView:0.0.2'
}

Step 2: Add it to Layout

For example you can wrap your TextView with it as shown below:

<?xml version="1.0" encoding="utf-8"?>
<com.jzj.view.ObservableScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</com.jzj.view.ObservableScrollView>

Step 3: Referebce and use

Reference it in your code then set the ScrollListener as showb below:

mScrollView = (ObservableScrollView) findViewById(R.id.scroll_view);

mScrollView.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScrollStateChanged(ObservableScrollView view, int scrollState) {
        switch (scrollState) {
            case SCROLL_STATE_IDLE:
                Log.d("MainActivity", "idle");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.d("MainActivity", "touch scroll");
                break;
            case SCROLL_STATE_FLING:
                Log.d("MainActivity", "fling");
                break;
        }
    }

    @Override
    public void onScroll(ObservableScrollView view, boolean isTouchScroll, int l, int t, int oldl, int oldt) {
        Log.d("MainActivity", "onScroll");
    }
});

Full Example

This example will comprise the following files:

  • MainActivity.java

Step 1: Create Project

  1. Open your AndroidStudio IDE.
  2. Go to File-->New-->Project to create a new project.

Step 2: Dependencies

Install it as has been described.

Step 3: Design Layouts

*(a). activity_main.xml

Create a file named activity_main.xml and design it as follows:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jzj.demo.MainActivity">

    <com.jzj.view.ObservableScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="1000dp"
            android:text="Hello World!"/>

    </com.jzj.view.ObservableScrollView>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="30dp"
        android:textSize="20sp"
        tools:ignore="RtlHardcoded"
        tools:text="idle"/>

</FrameLayout>

Step 4: Write Code

Write Code as follows:

(a). MainActivity.java

Create a file named MainActivity.java

Here is the full code

package com.jzj.demo;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.jzj.view.ObservableScrollView;

import static com.jzj.view.ObservableScrollView.OnScrollListener;

public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private ObservableScrollView mScrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text);
        mScrollView = (ObservableScrollView) findViewById(R.id.scroll_view);
        mTextView.setText("idle");

        mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                Log.d("MainActivity", "getViewTreeObserver - onScrollChanged");
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    Log.d("MainActivity", "setOnScrollChangeListener - onScrollChange");
                }
            });
        }

        // ObservableScrollView提供的OnScrollListener,可监听滚动事件和滚动状态变化
        mScrollView.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(ObservableScrollView view, int scrollState) {
                switch (scrollState) {
                    case SCROLL_STATE_IDLE:
                        mTextView.setText("idle");
                        break;
                    case SCROLL_STATE_TOUCH_SCROLL:
                        mTextView.setText("touch scroll");
                        break;
                    case SCROLL_STATE_FLING:
                        mTextView.setText("fling");
                        break;
                }
                Log.d("MainActivity", "setOnScrollListener - onScrollStateChanged, state = " + scrollState);
            }

            @Override
            public void onScroll(ObservableScrollView view, boolean isTouchScroll, int l, int t, int oldl, int oldt) {
                Log.d("MainActivity", "setOnScrollListener - onScroll");
            }
        });
    }
}

Run

Simply copy the source code into your Android Project,Build and Run.

Read More.

Related Posts