安卓简易开发笔记条例
一、
首先编写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;
}
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:
if(getScrollX()>-menuWidth/2){
closeMenu();
}else {
openMenu();
}
break;
}
return true;
}
private void closeMenu(){
scroller.startScroll(getScrollX(),0,0-getScrollX(),0,400);
invalidate();
}
private void openMenu(){
scroller.startScroll(getScrollX(),0,-menuWidth-getScrollX(),0,400);
invalidate();
}
public void computeScroll(){
super.computeScroll();
if(scroller.computeScrollOffset()){
scrollTo(scroller.getCurrX(),0);
invalidate();
}
}
public void switchMenu(){
if(getScrollX()==0){
openMenu();
}else {
closeMenu();
}
}
}
2.界面布局中,引用SlideMenu然后向其中添加include,注意,要侧滑出现的界面需要先一步载入
<com.example.myapplication2.SlideMenu
android:id="@+id/SlideMenu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 侧滑界面的布局-->
<include layout="@layout/layout_menu"/>
<!-- 主界面的布局-->
<include layout="@layout/layout_main"/>
</com.example.myapplication2.SlideMenu>
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");
});
setListener();
|