Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
+ [android] add scroll event on horizontal scroller
Browse files Browse the repository at this point in the history
  • Loading branch information
misakuo committed Jul 5, 2017
1 parent e56f343 commit 1b44388
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,48 +148,61 @@ public ViewGroup getInnerView() {
@Override
public void addEvent(String type) {
super.addEvent(type);
if (Constants.Event.SCROLL.equals(type) && getInnerView() != null && getInnerView() instanceof WXScrollView) {
((WXScrollView) getInnerView()).addScrollViewListener(new WXScrollViewListener() {
@Override
public void onScrollChanged(WXScrollView scrollView, int x, int y, int oldx, int oldy) {
if (shouldReport(x, y)) {
Rect frame = scrollView.getContentFrame();
if (Constants.Event.SCROLL.equals(type) && getInnerView() != null) {
if (getInnerView() instanceof WXScrollView) {
((WXScrollView) getInnerView()).addScrollViewListener(new WXScrollViewListener() {
@Override
public void onScrollChanged(WXScrollView scrollView, int x, int y, int oldx, int oldy) {
if (shouldReport(x, y)) {
fireScrollEvent(scrollView.getContentFrame(), x, y, oldx, oldy);
}
}

Map<String, Object> event = new HashMap<>(2);
Map<String, Object> contentSize = new HashMap<>(2);
Map<String, Object> contentOffset = new HashMap<>(2);
@Override
public void onScrollToBottom(WXScrollView scrollView, int x, int y) {
//ignore
}

int viewport = getInstance().getInstanceViewPortWidth();
@Override
public void onScrollStopped(WXScrollView scrollView, int x, int y) {
//ignore
}

contentSize.put(Constants.Name.WIDTH, WXViewUtils.getWebPxByWidth(frame.width(), viewport));
contentSize.put(Constants.Name.HEIGHT, WXViewUtils.getWebPxByWidth(frame.height(), viewport));
@Override
public void onScroll(WXScrollView scrollView, int x, int y) {
//ignore
}
});
} else if (getInnerView() instanceof WXHorizontalScrollView) {
((WXHorizontalScrollView) getInnerView()).addScrollViewListener(new WXHorizontalScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(WXHorizontalScrollView scrollView, int x, int y, int oldx, int oldy) {
if (shouldReport(x, y)) {
fireScrollEvent(scrollView.getContentFrame(), x, y, oldx, oldy);
}
}
});
}
}
}

contentOffset.put(Constants.Name.X, - WXViewUtils.getWebPxByWidth(x, viewport));
contentOffset.put(Constants.Name.Y, - WXViewUtils.getWebPxByWidth(y, viewport));
private void fireScrollEvent(Rect contentFrame, int x, int y, int oldx, int oldy) {
Map<String, Object> event = new HashMap<>(2);
Map<String, Object> contentSize = new HashMap<>(2);
Map<String, Object> contentOffset = new HashMap<>(2);

event.put(Constants.Name.CONTENT_SIZE, contentSize);
event.put(Constants.Name.CONTENT_OFFSET, contentOffset);
int viewport = getInstance().getInstanceViewPortWidth();

fireEvent(Constants.Event.SCROLL, event);
}
}
contentSize.put(Constants.Name.WIDTH, WXViewUtils.getWebPxByWidth(contentFrame.width(), viewport));
contentSize.put(Constants.Name.HEIGHT, WXViewUtils.getWebPxByWidth(contentFrame.height(), viewport));

@Override
public void onScrollToBottom(WXScrollView scrollView, int x, int y) {
//ignore
}
contentOffset.put(Constants.Name.X, -WXViewUtils.getWebPxByWidth(x, viewport));
contentOffset.put(Constants.Name.Y, -WXViewUtils.getWebPxByWidth(y, viewport));

@Override
public void onScrollStopped(WXScrollView scrollView, int x, int y) {
//ignore
}
event.put(Constants.Name.CONTENT_SIZE, contentSize);
event.put(Constants.Name.CONTENT_OFFSET, contentOffset);

@Override
public void onScroll(WXScrollView scrollView, int x, int y) {
//ignore
}
});
}
fireEvent(Constants.Event.SCROLL, event);
}

private boolean shouldReport(int x, int y) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.taobao.weex.ui.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -28,10 +29,14 @@
import com.taobao.weex.ui.view.gesture.WXGesture;
import com.taobao.weex.ui.view.gesture.WXGestureObservable;

import java.util.ArrayList;
import java.util.List;

public class WXHorizontalScrollView extends HorizontalScrollView implements IWXScroller, WXGestureObservable {

private WXGesture wxGesture;
private ScrollViewListener mScrollViewListener;
private List<ScrollViewListener> mScrollViewListeners;
private boolean scrollable = true;

@Override
Expand Down Expand Up @@ -60,6 +65,11 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) {
if (mScrollViewListener != null) {
mScrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
}
if (mScrollViewListeners != null) {
for (ScrollViewListener listener : mScrollViewListeners) {
listener.onScrollChanged(this, l, t, oldl, oldt);
}
}
}

public void setScrollViewListener(ScrollViewListener scrollViewListener) {
Expand All @@ -71,6 +81,19 @@ public void destroy() {

}

public void addScrollViewListener(ScrollViewListener scrollViewListener) {
if (mScrollViewListeners == null) {
mScrollViewListeners = new ArrayList<>();
}
if (!mScrollViewListeners.contains(scrollViewListener)) {
mScrollViewListeners.add(scrollViewListener);
}
}

public void removeScrollViewListener(ScrollViewListener scrollViewListener) {
mScrollViewListeners.remove(scrollViewListener);
}

@Override
public void registerGestureListener(WXGesture wxGesture) {
this.wxGesture = wxGesture;
Expand Down Expand Up @@ -100,4 +123,8 @@ public boolean isScrollable() {
public void setScrollable(boolean scrollable) {
this.scrollable = scrollable;
}

public Rect getContentFrame() {
return new Rect(0, 0, computeHorizontalScrollRange(), computeVerticalScrollRange());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import com.taobao.weex.utils.WXReflectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down

0 comments on commit 1b44388

Please sign in to comment.