Skip to content

Instantly share code, notes, and snippets.

@alekseyl1992
Created May 5, 2015 21:13
Show Gist options
  • Save alekseyl1992/f7082d1433c127bc2b23 to your computer and use it in GitHub Desktop.
Save alekseyl1992/f7082d1433c127bc2b23 to your computer and use it in GitHub Desktop.
Android custom ViewGroup implementation
package com.example.igor.test;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
public class MyViewGroup extends ViewGroup {
private View viewLeft;
private View viewRight;
private int width;
private int height;
private float initialPosX = -1;
private float initialPosY = -1;
public MyViewGroup(Context context) {
this(context, null);
}
public MyViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
viewLeft = new View(context);
viewRight = new View(context);
viewLeft.setBackgroundColor(Color.BLUE);
viewRight.setBackgroundColor(Color.RED);
addView(viewLeft);
addView(viewRight);
}
@Override
protected void onLayout(boolean bo, int l, int t, int r, int b) {
viewLeft.layout(l, t, r, b);
viewRight.layout(l, t, r, b);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getWidth();//widthMeasureSpec;
height = getHeight();
LayoutParams params = viewLeft.getLayoutParams();
params.height = heightMeasureSpec;
params.width = widthMeasureSpec;
viewLeft.setLayoutParams(params);
params = viewRight.getLayoutParams();
params.height = heightMeasureSpec;
params.width = widthMeasureSpec;
viewRight.setLayoutParams(params);
viewLeft.measure(widthMeasureSpec, heightMeasureSpec);
viewRight.measure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
boolean moving = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int e = event.getAction();
if (e == MotionEvent.ACTION_DOWN) {
System.out.println("action_down");
this.initialPosX = event.getX();
this.initialPosY = event.getY();
return true;
} else if (e == MotionEvent.ACTION_MOVE) {
// System.out.println("action_move");
// int offset = (int) (event.getX() - initialPos);
// System.out.println(offset);
// viewLeft.offsetLeftAndRight(offset);
// viewRight.offsetLeftAndRight(-offset);
int s = width/10;//ViewConfiguration.getTouchSlop();
float dx = event.getX() - initialPosX;
float dy = event.getY() - initialPosY;
if (Math.abs(dx) >= s)
moving = true;
if (moving) {
if (Math.abs(dx) > Math.abs(dy)) {
viewRight.setX(event.getX());
}
}
}
else if (e == MotionEvent.ACTION_UP) {
moving = false;
if (viewRight.getX() > width / 2)
viewRight.setX(width);
else
viewRight.setX(0);
}
invalidate();
requestLayout();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment