侧滑菜单关联Fragment的应用

这篇博客介绍了如何在Android应用中实现侧滑菜单功能,通过关联Fragment和使用DrawerLayout来创建。博主展示了简单的布局代码和MainActivity的实现细节,强调了使用DrawerLayout的重要性,并分享了实现效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今日,小强为大家带来带来了一个小功能,这个小功能就是侧滑菜单关联了Fragment,知识就是从小功能累计的,项目就是从小功能拼接起来的,工资就是从这些小功能叠加的。

这是鄙人的效果图:

在这里插入图片描述
如上图所示,我仅仅只用了一个简单的布局为大家带来了侧滑菜单的功能。

activity.xml主布局里的代码:
其实只要写完主布局里的代码就能实现侧滑菜单了,但千万一定要把最大的布局改成DrawerLayout,否则实现不了哦。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/Drawer_Layout">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/frg">
        </FrameLayout>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:background="@color/colorPrimary">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="15dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="李某人"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个事件"

            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/activity_tv1"/>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个事件"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/activity_tv2"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个事件"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/activity_tv3"/>
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

这里是我的MainActivity里的代码:

package com.example.mychdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView activity_tv1;
    private TextView activity_tv2;
    private TextView activity_tv3;
    private AFragment frg1;
    private BFragment frg2;
    private CFragment frg3;
    private FragmentManager manager;
    private DrawerLayout Drawer_Layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//查找控件
        //默认进入第一个Fragment
        manager.beginTransaction().replace(R.id.frg,frg1).commit();
    }

    private void initView() {
        activity_tv1 = (TextView) findViewById(R.id.activity_tv1);
        activity_tv2 = (TextView) findViewById(R.id.activity_tv2);
        activity_tv3 = (TextView) findViewById(R.id.activity_tv3);
        activity_tv1.setOnClickListener(this);
        activity_tv2.setOnClickListener(this);
        activity_tv3.setOnClickListener(this);
        Drawer_Layout=(DrawerLayout)findViewById(R.id.Drawer_Layout);
        //获取Fragment管理类
        manager=getSupportFragmentManager();
        frg1=new AFragment();
        frg2=new BFragment();
        frg3=new CFragment();
    }

//侧滑菜单点击切换Fragment
    @Override
    public void onClick(View v) {
       switch (v.getId())
       {
           case R.id.activity_tv1:
                manager.beginTransaction().replace(R.id.frg,frg1).commit();
                //切换Fragment后关闭侧滑菜单
                    Drawer_Layout.closeDrawers();
                break;

           case R.id.activity_tv2:
               manager.beginTransaction().replace(R.id.frg,frg2).commit();
               //切换Fragment后关闭侧滑菜单
                    Drawer_Layout.closeDrawers();
               break;

           case R.id.activity_tv3:
               manager.beginTransaction().replace(R.id.frg,frg3).commit();
               //切换Fragment后关闭侧滑菜单
                    Drawer_Layout.closeDrawers();
               break;
       }
    }
}

望大家能喜欢,多多支持,在这里小强希望大家技术越来越高,身体健康

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值