How can I determine the direction of a vertical ScrollView? - android

How can I determine the direction of a vertical ScrollView?
I need to hide and display another linear layout accoding to scrollview scroll

You can use the onScrollChanged(int l, int t, int oldl, int oldt) method for that. As there isn't a setter for a listener, but just that method, you will have to create your own ScrollView class and override that method and do your thing in your implementation.

public class ObservableScrollView extends ScrollView {
private static final int DEFAULT_THRESHOLD_DP = 4;
private ScrollDirectionListener scrollDirectionListener;
private int scrollThreshold;
public ObservableScrollView(Context context) {
this(context, null);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ObservableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
scrollThreshold = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_THRESHOLD_DP, context.getResources().getDisplayMetrics());
}
#Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (Math.abs(y - oldY) > scrollThreshold && scrollDirectionListener != null) {
if (y > oldY) {
scrollDirectionListener.onScrollUp(Math.abs(y - oldY));
} else {
scrollDirectionListener.onScrollDown(Math.abs(y - oldY));
}
}
}
public void setScrollThresholdInPx(int px) {
scrollThreshold = px;
}
public void setScrollThresholdInDp(Context context, float dp) {
scrollThreshold = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
public void setOnScrollDirectionListener(ScrollDirectionListener listener) {
scrollDirectionListener = listener;
}
public interface ScrollDirectionListener {
void onScrollDown(int pixels);
void onScrollUp(int pixels);
}}
got answer :) use this custom scrollview
ScrollView.setOnScrollDirectionListener(new ObservableScrollView.ScrollDirectionListener() {
#Override
public void onScrollDown(int pixels) {
contenttool.setVisibility(View.INVISIBLE);
}
#Override
public void onScrollUp(int pixels) {
contenttool.setVisibility(View.VISIBLE);
}
});

Related

How to do actions when scroll view reached bottom end?

I have an Activity with a scroll view and 2 buttons, When open this activity both buttons are disabled. What I want is, when I reach the end of the scroll view, enable both buttons to user to accept or decline the content. How can I do this in simple way.
Button before enabled
Thanks in advance.
I have done this things with webView here is my code, hope it will help you.
webView.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() {
#Override
public void onScroll(int l, int t) {
int tek = (int) Math.floor(webView.getContentHeight() * webView.getScale());
if (tek - webView.getScrollY() <= webView.getHeight() + 5) {
// enable your button
}
}
});
this is the class
public class ObservableWebView extends WebView
{
private OnScrollChangedCallback mOnScrollChangedCallback;
public ObservableWebView(final Context context)
{
super(context);
}
public ObservableWebView(final Context context, final AttributeSet attrs)
{
super(context, attrs);
}
public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
}
#Override
protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt)
{
super.onScrollChanged(l, t, oldl, oldt);
if(mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t);
}
public OnScrollChangedCallback getOnScrollChangedCallback()
{
return mOnScrollChangedCallback;
}
public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback)
{
mOnScrollChangedCallback = onScrollChangedCallback;
}
/**
* Impliment in the activity/fragment/view that you want to listen to the webview
*/
public static interface OnScrollChangedCallback
{
public void onScroll(int l, int t);
}
}
happy coding.

How to detect the end of scroll of a webivew

I have a custom WebView with the following code:
public class ObservableWebView extends WebView {
private OnScrollChangedCallback mOnScrollChangedCallback;
public ObservableWebView(final Context context) {
super(context);
}
public ObservableWebView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t);
}
public OnScrollChangedCallback getOnScrollChangedCallback() {
return mOnScrollChangedCallback;
}
public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback) {
mOnScrollChangedCallback = onScrollChangedCallback;
}
/**
* Implement in the activity/fragment/view that you want to listen to the webview
*/
public static interface OnScrollChangedCallback {
public void onScroll(int l, int t);
}
}
I use the following method to detect whether scroll has reached the end:
#Override
public void onScroll(int l, int t) {
int tek = (int) Math.floor(webView.getContentHeight() * webView.getScale());
if(tek - webView.getScrollY() == webView.getHeight())
Toast.makeText(getActivity(), "End", Toast.LENGTH_SHORT).show();
}
Problem is it doesn't always work and fails sometimes. Is there are surer way of detecting the end of scroll of a webview that is backward compatible all the way to api level 16?

Scrolling parallel views simultaneously

I have two ScrollView's side by side and by using the code below I can scroll them simultaneously but I still can scroll them each independently throwing off the scroll positions. How can I make each view scroll simultaneously and disable scrolling each view by itself? I apologize if there's any confusion in my question. Any help would be appreciated, thanks.
ScrollView sv1;
ScrollView sv2;
View clickSource;
View touchSource;
sv1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(touchSource == null)
touchSource = v;
if(v == touchSource) {
sv2.dispatchTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_UP) {
clickSource = v;
touchSource = null;
}
}
return false;
}
});
sv2.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if(touchSource == null)
touchSource = v;
if(v == touchSource) {
sv1.dispatchTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_UP) {
clickSource = v;
touchSource = null;
}
}
return false;
}
});
Hopefully I understand your question correctly. If you want both ScrollViews to scroll simultaneously then the code below should do the trick (untested):
First create an interface to listen to scroll events:
public interface ScrollChangeListener {
public void onScrollChanged(View view, int x, int y, int oldx, int oldy);
}
Next, create a custom view so you can listen for scroll changes:
public class ObservableScrollView extends ScrollView {
private ScrollChangeListener mScrollChangeListener;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObservableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ObservableScrollView(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setScrollChangeListener(ScrollChangeListener listener) {
mScrollChangeListener = listener;
}
#Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
if (mScrollChangeListener != null) {
mScrollChangeListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
}
Use your custom view and create a listener for both ScrollViews.
ObservableScrollView mScrollView1;
ObservableScrollView mScrollView2;
...
ScrollChangeListener listener = new ScrollChangeListener() {
#Override
public void onScrollChanged(View view, int x, int y, int oldx, int oldy) {
ScrollView scrollView;
if (view == mScrollView1) {
scrollView = mScrollView2;
} else if (view == mScrollView2) {
scrollView = mScrollView1;
} else {
return;
}
scrollView.scrollTo(x, y);
}
};
...
mScrollView1.setScrollChangeListener(listener);
mScrollView2.setScrollChangeListener(listener);
Try this
sv1.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollX = sv1.getScrollX(); // for horizontalScrollView
int scrollY = sv1.getScrollY(); // for verticalScrollView
// DO SOMETHING WITH THE SCROLL COORDINATES
sv2.scrollTo(scrollX, scrollY);
}
});

Changing View Height inside a custom ScrollView onScrollChanged

I'm trying to make the following effect:
I have a custom ScrollView (in oder to get the onScrollChanged listener) and a custom View inside it. In the custom View I succeed to place the item as I want.
Here my customView:
public class CustomView extends FrameLayout {
private TextView nameView;
private TextView emailView;
private ImageView addressView;
private Tracks track ;
private double scrollProgress = 0.0;
private double topViewScaleFactor = 2.0;
private double collapsedViewHeight = 200.0;
private double expandedViewHeight = 700.0;
private double scrollProgressPerView = expandedViewHeight;
View v;
View firstItem;
View secondView;
int itemMinHeight = 200;
int itemMaxHeight = 700;
public CustomView(MyScrollView paramEventListView, Context paramContext){
super(paramContext);
}
public CustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int m = getMeasuredWidth();
int itemWidth = (r-l)/getChildCount();
// int itemHeight = (b-t)/getChildCount();
firstItem = getChildAt(0);
//firstItem.layout(0, 0, r-l, itemMaxHeight);
firstItem.measure(View.MeasureSpec.makeMeasureSpec(m, MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(itemMaxHeight, MeasureSpec.EXACTLY));
firstItem.layout(0, 0, r-l, itemMaxHeight);
secondView = getChildAt(1);
secondView.measure(View.MeasureSpec.makeMeasureSpec(m, MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(itemMinHeight, MeasureSpec.EXACTLY));
secondView.layout(0, itemMaxHeight, r-l, itemMinHeight+itemMaxHeight);
int FirstAndSEcondItemHeight = firstItem.getHeight() + secondView.getHeight();
for(int i=2; i< this.getChildCount(); i++){
v = getChildAt(i);
// v.layout(itemWidth*i, 0, (i+1)*itemWidth, b-t);
v.layout(0, FirstAndSEcondItemHeight + (itemMinHeight*(i-2)), r-l, FirstAndSEcondItemHeight + ((i-1)*itemMinHeight));
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int heightMeasured = 0;
/*for each child get height and
heightMeasured += childHeight;*/
for(int i=0; i< this.getChildCount(); i++){
heightMeasured += getChildAt(i).getHeight();
}
//If I am in a scrollview i got heightmeasurespec == 0, so
if(heightMeasureSpec == 0){
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasured, MeasureSpec.EXACTLY);
}
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec));
}
Here my Custom ScrollView:
public class MyScrollView extends ScrollView{
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
add(context);
}
public MyScrollView(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);
// How can I acces to each child in the customView class, and change their height depending on the scrollChanged
}
But now I need to change the item height when I scroll the scrollview. I don't know what to put in the onScrollChanged...
If someone has an idea?
Thanks
Perhaps you want to force the view to redraw by calling invalidate()?
From comment: I think you need to move your code in the onMeasure method to replace the "TODO" stub, so that super.onMeasure() is called after your manipulations. And make sure you're passing the new width/height calculations to it.

Load more data when ScrollView is scrolled to bottom

I've got a scrollView with dinamically loading content. Sometimes there can be a lot of content, so I would like to load more when user scrolls to bottom.
I searced for suitable metods and found two:
onScrollChanged()
and
getScrollY()
But I dont know how to use it for my purposes.
Please give me some advice.
Scroll view does not provide any method to check if you've reached bottom of the view so the best technique is to extend your own custom view with scroll view . This is how i implemented in my app.
Step 1:Create a custom class for scroll view
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) {
View view = (View) getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY()));
if (diff == 0) { // if diff is zero, then the bottom has been reached
if (scrollViewListener != null) {
scrollViewListener.onScrollEnded(this, x, y, oldx, oldy);
}
}
super.onScrollChanged(x, y, oldx, oldy);
}
}
Step 2:Create a scroll view Listener Interface
public interface ScrollViewListener {
void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
Step 3:Add the view in your layout
<com.platinumapps.facedroid.ObservableScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.platinumapps.facedroid.ObservableScrollView>
Step 4:Implement that interface in your class
public class MyFragment extends Fragment implements ScrollViewListener
#Override
public void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
//Perform your action
}
And you are done
I think the better way would be to use ListView.But if you only required ScrollView.
First you fix a threshold height of your ScrollView so whenever you crosses that limit load new data & append to ScrollView & reset your threshold height.
This is how you can try..
Create a customized ScrollView like this.
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);
}
}
}
& then create an interface
public interface ScrollViewListener
{
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
& your XML layout should be like this
<LinearLayout 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"
android:orientation="vertical" >
<com.solve.stackoverflow.ObservableScrollView
android:id="#+id/endless_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/endless_scrollview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</com.solve.stackoverflow.ObservableScrollView>
</LinearLayout>
& finally use all these in your Activity
public class EndLessActivity extends Activity implements ScrollViewListener
{
ObservableScrollView scrollView;
LinearLayout layout;
int threshold = 20; //Setting threshold
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.endless_scrollview);
scrollView = (ObservableScrollView) findViewById(R.id.endless_scrollview);
layout = (LinearLayout) findViewById(R.id.endless_scrollview_layout);
scrollView.setScrollViewListener(this);
appendData();
}
#Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy)
{
// TODO Auto-generated method stub
if (y > threshold)
appendData();
}
void appendData()
{
for (int i = 0; i < 15; i++)
{
threshold += 10;//Reseting threshold.
TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.ic_launcher);
layout.addView(tv);
}
}
}
Try this & let me know, whether it solve your problem or not.
Thanks

Categories

Resources