山东大学项目实训之安卓slideActivity

安卓端侧滑抽屉界面
侧滑前
在这里插入图片描述
在这里插入图片描述
侧滑activity包括两个菜单,一个主菜单,一个侧滑菜单

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SlideActivity">

    <com.example.adverseevents.SlideMenu
        android:id="@+id/slideMenu1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/layout_menu" />

        <include layout="@layout/activity_main" />


    </com.example.adverseevents.SlideMenu>
</RelativeLayout>

主菜单

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="410dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@color/colorWhiteblue"
    tools:context=".MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/image_head"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher_round" />


    <androidx.cardview.widget.CardView
        android:id="@+id/cardview1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        app:cardCornerRadius="15dp"
        app:cardBackgroundColor="@color/colorLightblue"

        >

        <Button
            android:id="@+id/main_button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:text="不良事件上报"
            android:textSize="25sp"
            android:background="@color/colorWhiteblue"/>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardview2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        app:cardBackgroundColor="@color/colorLightblue"
        app:cardCornerRadius="15dp"

        >
        <Button
            android:id="@+id/main_button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:text="不良事件查询"
            android:textSize="25sp"
            android:background="@color/colorWhiteblue"/>


    </androidx.cardview.widget.CardView>
        <androidx.cardview.widget.CardView
            android:id="@+id/cardview3"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginTop="10dp"
            app:cardBackgroundColor="@color/colorLightblue"
            app:cardCornerRadius="15dp"

            >
            <Button
                android:id="@+id/main_button3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="待处理不良事件(质控办)"
                android:textSize="25sp"
                android:background="@color/colorWhiteblue"/>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:id="@+id/cardview4"

            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginTop="10dp"
            android:visibility="gone"
            app:cardBackgroundColor="@color/colorLightblue"
            app:cardElevation="5dp"
            app:cardCornerRadius="15dp">
            <Button
                android:id="@+id/main_button4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="待处理不良事件(职能科室)"
                android:textSize="25sp"
                android:background="@color/colorWhiteblue"/>

        </androidx.cardview.widget.CardView>
    <androidx.cardview.widget.CardView
        android:id="@+id/cardview5"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:visibility="gone"
        app:cardBackgroundColor="@color/colorLightblue"
        app:cardElevation="5dp"
        app:cardCornerRadius="15dp">
        <Button
            android:id="@+id/main_button5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="待处理不良事件(非职能科室)"
            android:textSize="25sp"
            android:background="@color/colorWhiteblue"/>

    </androidx.cardview.widget.CardView>
    <androidx.cardview.widget.CardView
    android:id="@+id/cardview6"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="10dp"
    android:visibility="gone"
    app:cardBackgroundColor="@color/colorLightblue"

    app:cardCornerRadius="15dp">
    <Button
        android:id="@+id/main_button6"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="查询统计"
        android:textSize="25sp"
        android:background="@color/colorWhiteblue"/>

</androidx.cardview.widget.CardView>
    <androidx.cardview.widget.CardView
        android:id="@+id/cardview7"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:visibility="gone"
        app:cardBackgroundColor="@color/colorLightblue"

        app:cardCornerRadius="15dp">
        <Button
            android:id="@+id/main_button7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="查询统计(科室主任)"
            android:textSize="25sp"
            android:background="@color/colorWhiteblue"/>

    </androidx.cardview.widget.CardView>
</LinearLayout>
    </ScrollView>
</LinearLayout>

侧滑菜单

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="270dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorWhiteblue"
    tools:context=".SlideMenu">
   <TextView
       android:id="@+id/text1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="个人中心"
       android:textStyle="bold"
       android:textColor="@color/colorLightblue"
       android:gravity="center_horizontal"
       android:paddingTop="100dp"
       android:textSize="40sp"/>
   <Button

       android:onClick="Logout"
       android:layout_width="300dp"
       android:text="退出登录"
       android:layout_gravity="center"
       android:textSize="20sp"
       android:layout_marginTop="10dp"
       android:gravity="center"
       android:background="@color/colorLightblue"
       android:layout_height="50dp"
       ></Button>
   <Button

       android:onClick="changePasswordBox"
       android:layout_width="300dp"
       android:text="修改密码"
       android:layout_gravity="center"
       android:textSize="20sp"
       android:layout_marginTop="10dp"
       android:gravity="center"
       android:background="@color/colorLightblue"
       android:layout_height="50dp"
       ></Button>
<LinearLayout
    android:id="@+id/change_password"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   android:orientation="vertical"
   >
   <EditText
       android:id="@+id/nickname"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:textSize="20sp"
       android:layout_marginTop="10dp"
       android:background="@drawable/edittext_shape"
       android:paddingLeft="10dp"
       android:hint="用户名"
       />
   <EditText
       android:id="@+id/oldPass"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:textSize="20sp"
       android:layout_marginTop="10dp"
       android:background="@drawable/edittext_shape"
       android:paddingLeft="10dp"
       android:inputType="textPassword"
       android:hint="旧密码"
       />
   <EditText
       android:id="@+id/newPass"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:textSize="20sp"
       android:layout_marginTop="10dp"
       android:background="@drawable/edittext_shape"
       android:inputType="textPassword"
       android:paddingLeft="10dp"
       android:hint="新密码"
       />
   <EditText
       android:id="@+id/newPass2"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:textSize="20sp"
       android:layout_marginTop="10dp"
       android:inputType="textPassword"
       android:background="@drawable/edittext_shape"
       android:paddingLeft="10dp"
       android:hint="确认新密码"
       />
   <Button

       android:onClick="changePassword"
       android:layout_width="300dp"
       android:text="修改密码"
       android:layout_gravity="center"
       android:textSize="20sp"

       android:layout_marginTop="10dp"
       android:gravity="center"
       android:background="@color/colorLightblue"
       android:layout_height="50dp"
       ></Button>
</LinearLayout>
</LinearLayout>

SlideActivity


public class SlideActivity extends AppCompatActivity {
    private ImageView btn_back;
    private SlideMenu slideMenu;
    private List<eventtype> typeslist;
    private CardView report_btn;
    private CardView search_btn;
    private CardView receive_btn;
    private CardView analysis_btn;
    private CardView analysis2_btn;
    private CardView statistic;
    private CardView statistic2;
    private Button report_btn1;
    private Button search_btn1;
    private Button receive_btn1;
    private Button analysis_btn1;
    private Button analysis2_btn1;
    private Button statistic1;
    private Button statistic21;
    private LinearLayout passBox;
    private EditText nickname;
    private EditText oldPass;
    private EditText newPass;
    private EditText newPass2;
    private SharedPreferences sharedPreferences;//token1
    private String name2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide);
        btn_back = (ImageView) findViewById(R.id.image_head);
        slideMenu = (SlideMenu) findViewById(R.id.slideMenu1);
        //点击返回键打开或关闭Menu
        passBox = findViewById(R.id.change_password);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slideMenu.switchMenu();

                Toast.makeText(getBaseContext(), "ok", Toast.LENGTH_SHORT).show();
            }
        });

        sharedPreferences = getSharedPreferences("tokenInfo", MODE_PRIVATE);//token2
        name2 = sharedPreferences.getString("token", "");
        nickname = findViewById(R.id.nickname);
        oldPass = findViewById(R.id.oldPass);
        newPass = findViewById(R.id.newPass);
        newPass2 = findViewById(R.id.newPass2);
        report_btn = findViewById(R.id.cardview1);
        search_btn = findViewById(R.id.cardview2);
        receive_btn = findViewById(R.id.cardview3);
        analysis_btn = findViewById(R.id.cardview4);
        analysis2_btn = findViewById(R.id.cardview5);
        statistic = findViewById(R.id.cardview6);
        statistic2 = findViewById(R.id.cardview7);
        report_btn1 = findViewById(R.id.main_button1);
        search_btn1 = findViewById(R.id.main_button2);
        receive_btn1 = findViewById(R.id.main_button3);
        analysis_btn1= findViewById(R.id.main_button4);
        analysis2_btn1 = findViewById(R.id.main_button5);
        statistic1 = findViewById(R.id.main_button6);
        statistic21 = findViewById(R.id.main_button7);
        report_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SlideActivity.this, ReportActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "上报事件", Toast.LENGTH_SHORT).show();
            }
        });
        search_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SlideActivity.this, ReportListActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "查询事件", Toast.LENGTH_SHORT).show();
            }
        });
        receive_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SlideActivity.this, ExamineListActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "我的受理", Toast.LENGTH_SHORT).show();
            }
        });
        analysis_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SlideActivity.this, RectifyListActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "我的受理", Toast.LENGTH_SHORT).show();
            }
        });
        analysis2_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SlideActivity.this, InvolvedRectifyListActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "我的受理", Toast.LENGTH_SHORT).show();
            }
        });
        statistic1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SlideActivity.this, EchatsActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "统计分析", Toast.LENGTH_SHORT).show();
            }
        });
        statistic21.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SlideActivity.this, DepartmentEchartsActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "统计分析(科室主任)", Toast.LENGTH_SHORT).show();
            }
        });

        OkHttpClient mOkHttpClient = new OkHttpClient();

        //使用Gson将对象转换为json字符串

        Request request = new Request.Builder()
                .header("token", name2)
                .url("http://43.138.24.19/user/getMenuTree")
                .method("GET",null).build();
//3,创建call对象并将请求对象添加到调度中
        Log.i("request",request.toString());
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("测试", response.code()+"");

                Gson gson = new Gson();
                String msg = response.body().string();
                Log.v("body", "body=" + msg);

                Type listType = new TypeToken<ResponseTypes<List<eventtype>>>() {
                }.getType();
                final ResponseTypes data2 = gson.fromJson(msg, listType);
                //ResponseData data2 = gson.fromJson(msg,ResponseData.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            typeslist = data2.getData2();
                            ;

                            for (int i = 0; i < typeslist.size(); i++) {
                                int temp = typeslist.get(i).getId();
                                Log.v("body", "body=" + temp);
                                if (temp == 3) {
                                    receive_btn.setVisibility(View.VISIBLE);
                                }
                                if (temp == 4) {
                                    analysis2_btn.setVisibility(View.VISIBLE);
                                }
                                if (temp == 5) {
                                    analysis_btn.setVisibility(View.VISIBLE);
                                }
                                if (temp == 6) {
                                    statistic.setVisibility(View.VISIBLE);
                                }
                                if (temp == 7) {
                                    statistic2.setVisibility(View.VISIBLE);
                                }
                            }
                        }catch (Exception e){
                            Toast.makeText(getApplicationContext(), "请求未成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

        });

    }

SlideMenu


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());
    }
    /**
     * 当1级子view全部加载完调用,可以用初始化子view引用
     * 注意这里无法获取子view的宽高
     */
    @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);
    }

    /**
     * s设置两个子view在页面上的布局
     * @param l:当前子view的左边在父view的坐标系的x坐标
     * @param t:当前子view的顶边在父view的坐标系的y坐标
     * @param r:当前子view的宽
     * @param b:当前子view的高
     */
    @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();
    }
    /**
     * Scroller不主动去调用这个方法
     * 而invalidate()可以调用这个方法
     * invalidate->draw->computeScroll
     */
    public void computeScroll(){
        super.computeScroll();
        if(scroller.computeScrollOffset()){
            //返回true,表示动画没结束
            scrollTo(scroller.getCurrX(),0);
            invalidate();
        }
    }
    /**
     * 切换菜单的开和关
     */
    public void switchMenu(){
        if(getScrollX()==0){
            openMenu();
        }else {
            closeMenu();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值