自定义fragment
:
public class MyDrawer extends FrameLayout {
private LinearLayout content;
private LinearLayout menu;
private int width=400;
//系统存值
PointF pointF=new PointF();
int x;
int y;
public MyDrawer(Context context) {
super(context);
init();
}
public MyDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyDrawer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void addViewContont(View view){
if (view==null){
return;
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
view.setLayoutParams(params);
content.addView(view);
}
public void addViewMenu(View view){
if (view==null){
return;
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
view.setLayoutParams(params);
menu.addView(view);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
public void init(){
//主菜单
content=new LinearLayout(getContext());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
content.setBackgroundColor(Color.GREEN);
content.setOrientation(LinearLayout.VERTICAL);
content.setLayoutParams(params);
//侧滑菜单
menu=new LinearLayout(getContext());
FrameLayout.LayoutParams pr = new FrameLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
menu.setBackgroundColor(Color.RED);
menu.setOrientation(LinearLayout.VERTICAL);
pr.leftMargin-=width;
menu.setLayoutParams(pr);
addView(content);
addView(menu);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
pointF.x = ev.getX();
pointF.y = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
float a = ev.getX()-pointF.x;
float b = ev.getY()-pointF.y;
if (Math.abs(a)>Math.abs(2*b)){
return true;
}
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
pointF.x = event.getX();
pointF.y = event.getY();
break;
case MotionEvent.ACTION_MOVE:
moveDrawer(event);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
private boolean moveDrawer(MotionEvent event){
if (Math.abs(pointF.x-event.getX())>5){
int x= (int) (event.getX()-pointF.x);
FrameLayout.LayoutParams layoutParams = (LayoutParams) menu.getLayoutParams();
int nowx=layoutParams.leftMargin;
layoutParams.leftMargin=nowx+x;
if (layoutParams.leftMargin>0){
layoutParams.leftMargin=0;
}
if (layoutParams.leftMargin<-width){
layoutParams.leftMargin=-width;
}
menu.requestLayout();
pointF.x=event.getX();
pointF.y=event.getY();
}
return true;
}
}
activity代码:
private MyDrawer md;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
md = (MyDrawer) findViewById(R.id.md);
View view = LayoutInflater.from(this).inflate(R.layout.item, null);
md.addViewContont(view);
}