๐Ÿ’ปTech/๐ŸŒตandroid

[์•ˆ๋“œ๋กœ์ด๋“œ] ScrollView์—์„œ setOnScrollListener() ๋Œ€์‹  ์‚ฌ์šฉ (ScrollView ํŽ˜์ด์ง•์ฒ˜๋ฆฌ)

_viper_ 2016. 2. 3. 16:40
๋ฐ˜์‘ํ˜•

ScrollView๋กœ ํŽ˜์ด์ง€ ์ฒ˜๋ฆฌํ•˜๋Š” Custom ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

 

์•ˆ๋“œ๋กœ์ด๋“œ ListView์—์„œ ํŽ˜์ด์ง€ ์ฒ˜๋ฆฌํ•  ๋•Œ ๊ผญ ํ•„์š”ํ•œ๊ฒŒ setOnScrollListener()์ธ๋ฐ์š”.

์™œ๋ƒํ•˜๋ฉด ์Šคํฌ๋กค์ด ํ™”๋ฉด ๋งจ ๋ฐ”๋‹ฅ์— ๊ฐ์ง€ํ–ˆ์„๋•Œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ 

 

ScrollView์—์„œ๋Š”  setOnScrollListener() ์ด๊ฒŒ ์—†์Šต๋‹ˆ๋‹ค.

Customํด๋ž˜์Šค ์ด์šฉํ•˜์—ฌ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  ํด๋ž˜์Šค๋ถ€ํ„ฐ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์†Œ์Šค๋กœ ํด๋ž˜์Šค ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”.

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    private ScrollViewListener scrollViewListener = null;

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }
}

 

 

[์‚ฌ์šฉ๋ฐฉ๋ฒ•]

 

โ–ถXml ํŒŒ์ผ

<com.custom.scrollview.view.CustomScrollView      -> ์ž์‹ ์˜ ํŒจํ‚ค์ง€๋ช…์„ ์ ์–ด์ฃผ์„ธ์š”
        android:id="@+id/sv_store_history"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

<๋‚ด์šฉ>

</com.custom.scrollview.view.CustomScrollView>

 

โ–ถ์‚ฌ์šฉํ•  ํด๋ž˜์Šค ํŒŒ์ผ

if๋ฌธ์•ˆ์— ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ๋‚˜ ๋ฐ”๋‹ฅ์— ๋‹ฟ์•˜์„๋•Œ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์€ ์ด๋ฒคํŠธ ์ฝ”๋“œ ์ž‘์„ฑํ•ด ์ฃผ์‹œ๋ฉด๋ฉ๋‹ˆ๋‹ค.

private CustomScrollView svStoreHistory;

svStoreHistory = (CustomScrollView)findViewById(R.id.sv_store_history);

svStoreHistory.setScrollViewListener(new ScrollViewListener() {

            @Override
            public void onScrollChanged(CustomScrollView scrollView, int x, int y, int oldx, int oldy) {
                View view = scrollView.getChildAt(scrollView.getChildCount() - 1);
                int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));

                if (diff == 0 && mLockScrollView == false) { // ์Šคํฌ๋กค bottom
                    
                }
            }
        });