Skip to content

Instantly share code, notes, and snippets.

@gturedi
Last active February 10, 2016 10:04
Show Gist options
  • Save gturedi/eac41e6d21e53fb1e65a to your computer and use it in GitHub Desktop.
Save gturedi/eac41e6d21e53fb1e65a to your computer and use it in GitHub Desktop.
small useful custom view classes
<resources>
<declare-styleable name="ScrollViewWithMaxHeight">
<attr name="maxHeight" format="dimension"/>
</declare-styleable>
</resources>
// click and scrol listener supported webview
package gturedi.gist;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
public class ObservableWebView
extends WebView {
public interface OnScrollChangedCallback {
void onScroll(int l, int t);
}
private final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density;
private boolean mMoveOccurred;
private float mDownPosX;
private float mDownPosY;
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
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mMoveOccurred = false;
mDownPosX = ev.getX();
mDownPosY = ev.getY();
break;
case MotionEvent.ACTION_UP:
// click occurred
if (!mMoveOccurred) {
performClick();
}
break;
case MotionEvent.ACTION_MOVE:
// scroll occurred
if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) {
mMoveOccurred = true;
}
break;
}
return super.onTouchEvent(ev);
}
@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;
}
}
// custom scrollview with maxHeight field
package gturedi.dev.external;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ScrollView;
import gturedi.dev.R;
public class ScrollViewWithMaxHeight
extends ScrollView {
public static int WITHOUT_MAX_HEIGHT_VALUE = -1;
private int maxHeight = WITHOUT_MAX_HEIGHT_VALUE;
public ScrollViewWithMaxHeight(Context context) {
super(context);
}
public ScrollViewWithMaxHeight(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.ScrollViewWithMaxHeight, 0, 0);
float value = typedArray.getDimension(R.styleable.ScrollViewWithMaxHeight_maxHeight, 0);
typedArray.recycle();
setMaxHeight((int) value);
}
public ScrollViewWithMaxHeight(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (maxHeight != WITHOUT_MAX_HEIGHT_VALUE && heightSize > maxHeight) {
heightSize = maxHeight;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
getLayoutParams().height = heightSize;
} catch (Exception e) {
e.printStackTrace();
} finally {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
}
}
// aware clicks in adapter with clickable children
package gturedi.com.gitsamples.gist.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
public class TouchAwareRelativeLayout
extends RelativeLayout {
private final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density;
private boolean mMoveOccured;
private float mDownPosX;
private float mDownPosY;
public TouchAwareRelativeLayout(Context context) {
super(context);
}
public TouchAwareRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mMoveOccured = false;
mDownPosX = ev.getX();
mDownPosY = ev.getY();
break;
case MotionEvent.ACTION_UP:
if (!mMoveOccured) {
performClick();
}
break;
case MotionEvent.ACTION_MOVE:
if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) {
mMoveOccured = true;
}
break;
}
return super.onInterceptTouchEvent(ev);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment