android scrollview 動態滾動

protected void onScrollChanged(int x, int y, int oldx, int oldy) 

Unfortunately Google never thought that we would need to access it, which is why they made it protected and didn't add a "setOnScrollChangedListener" hook. So we will have to do that for ourselves.android

First we need an interface.ide

package com.test; 
 
public interface ScrollViewListener { 
 
   
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); 
 
} 
this

package com.test; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.ScrollView; 
 
public class ObservableScrollView extends ScrollView { 
 
   
private ScrollViewListener scrollViewListener = null; 
 
   
public ObservableScrollView(Context context) { 
       
super(context); 
   
} 
 
   
public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { 
       
super(context, attrs, defStyle); 
   
} 
 
   
public ObservableScrollView(Context context, AttributeSet attrs) { 
       
super(context, attrs); 
   
} 
 
   
public void setScrollViewListener(ScrollViewListener scrollViewListener) { 
       
this.scrollViewListener = scrollViewListener; 
   
} 
 
   
@Override 
   
protected void onScrollChanged(int x, int y, int oldx, int oldy) { 
       
super.onScrollChanged(x, y, oldx, oldy); 
       
if(scrollViewListener != null) { 
            scrollViewListener
.onScrollChanged(this, x, y, oldx, oldy); 
       
} 
   
} 
 

spa

相關文章
相關標籤/搜索