今日,小强为大家带来带来了一个小功能,这个小功能就是侧滑菜单关联了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;
}
}
}
望大家能喜欢,多多支持,在这里小强希望大家技术越来越高,身体健康