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