首先编写SlideMene类,可直接复制进行使用,来自小小小白冷博主的文章-b站材料1-代码之侧滑界面的java文件
package com.example.myapplication2; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.frameLayout; import android.widget.Scroller; public class SlideMenu extends frameLayout { private View menuView,mainView; private int menuWidth; private Scroller scroller; public SlideMenu(Context context) { super(context); init(); } public SlideMenu(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init(){ scroller = new Scroller(getContext()); } @Override protected void onFinishInflate() { super.onFinishInflate(); menuView = getChildAt(0); mainView = getChildAt(1); menuWidth = menuView.getLayoutParams().width; } //使Menu也具有滑动功能 public boolean onInterceptTouchEvent(MotionEvent ev){ switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downX = (int) ev.getX(); break; case MotionEvent.ACTION_MOVE: int deltaX = (int) (ev.getX() - downX); if (Math.abs(deltaX) > 8){ return true; } break; } return super.onInterceptTouchEvent(ev); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { menuView.layout(-menuWidth, 0, 0, b); mainView.layout(0, 0, r, b); } private int downX; public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = (int) event.getX(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) event.getX(); int deltaX = moveX - downX; int newScrollX = getScrollX() - deltaX; if (newScrollX < -menuWidth) newScrollX = -menuWidth; if (newScrollX > 0) newScrollX = 0; scrollTo(newScrollX, 0); downX = moveX; break; case MotionEvent.ACTION_UP: //当滑动距离小于Menu宽度的一半时,平滑滑动到主页面 if(getScrollX()>-menuWidth/2){ closeMenu(); }else { //当滑动距离大于Menu宽度的一半时,平滑滑动到Menu页面 openMenu(); } break; } return true; } //关闭menu private void closeMenu(){ scroller.startScroll(getScrollX(),0,0-getScrollX(),0,400); invalidate(); } //打开menu private void openMenu(){ scroller.startScroll(getScrollX(),0,-menuWidth-getScrollX(),0,400); invalidate(); } public void computeScroll(){ super.computeScroll(); if(scroller.computeScrollOffset()){ //返回true,表示动画没结束 scrollTo(scroller.getCurrX(),0); invalidate(); } } public void switchMenu(){ if(getScrollX()==0){ openMenu(); }else { closeMenu(); } } }
2.界面布局中,引用SlideMenu然后向其中添加include,注意,要侧滑出现的界面需要先一步载入
3.Java类代码
声明控件
private SlideMenu slideMenu; private ImageView imageView; private Button bustudy; private Button buread; private Button buwatch; private Button buexec;
找到相关控件
imageView = findViewById(R.id.iv_head); bustudy=findViewById(R.id.labu_1); buread=findViewById(R.id.labu_2); buwatch=findViewById(R.id.labu_3); buexec=findViewById(R.id.labu_4);
创建类继承监听事件,实现Button按钮的点击事件
public class onclick implements View.OnClickListener{ @Override public void onClick(View view) { Intent intent =null; switch (view.getId()){ case R.id.labu_1: intent = new Intent(SlideActivity.this, StudyActivity.class); break; case R.id.labu_2: intent = new Intent(SlideActivity.this,ReadActivity.class); break; case R.id.labu_3: intent = new Intent(SlideActivity.this,WatchActivity.class); break; case R.id.labu_4: intent = new Intent(SlideActivity.this,ExecActivity.class); break; } startActivity(intent); } }
封装方法实现Button的点击事件
private void setListener(){ onclick onclick = new onclick(); //实现每一个按钮的点击 bustudy.setOnClickListener(onclick); buread.setOnClickListener(onclick); buwatch.setOnClickListener(onclick); buexec.setOnClickListener(onclick); }
实现相关功能
imageView.setOnClickListener(view -> { //调用方法实现侧滑效果 slideMenu.switchMenu(); ToastUtil.show(SlideActivity.this,"ok"); }); //调用Button按钮的实现方法 setListener();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)