Android使用DrawLayout,ToolBar和ActionBarDrawerToggle实现抽屉

效果:

可进行侧滑,点击可切换不同页面

Android使用DrawLayout,ToolBar和ActionBarDrawerToggle实现抽屉_第1张图片
Android使用DrawLayout,ToolBar和ActionBarDrawerToggle实现抽屉_第2张图片

对Toolbar的设置,添加三个菜单选项,并给菜单选项设置点击事件

public class DrawerLayoutUsing extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    ArrayList<String> menuList;
    private ArrayAdapter<String> adapter;
    private Toolbar toolbar;
    private ActionBarDrawerToggle mToggle;
    private FragmentManager fragmentManager;
    private ListView mylist;
    private LinearLayout ll_drawer;
    private int currentFragmentId = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("讨论专区");
//        设置菜单内容
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.lv_drawer);
        menuList = new ArrayList<>();
        menuList.add("热门帖子");
        menuList.add("品牌讨论");
        menuList.add("我要发贴");
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menuList);
        mDrawerList.setAdapter(adapter);
        ll_drawer = (LinearLayout) findViewById(R.id.ll_drawer);
//        设置点击事件
        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                switchFragement(position);
            }
        });
//        设置toolbar
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
//        设置左上标(箭头)
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToggle.syncState();
        mDrawerLayout.addDrawerListener(mToggle);
//        设置默认fragment
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.ll_content,new Fragment1()).commit();
    }

//switchFragment方法切换界面
    private void switchFragement(int fragmentId) {
        mDrawerLayout.closeDrawer(ll_drawer);
//        if(currentFragmentId==fragmentId&&fragmentId!=2){
//            return;
//        }
//        currentFragmentId=fragmentId;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (fragmentId){
            case 0:
                Intent intentFragment1 = new Intent(this,BannerActivity.class);
                startActivity(intentFragment1);
                fragmentTransaction.replace(R.id.ll_content,new Fragment1());
                toolbar.setTitle("讨论专区");
                break;
            case 1:
                Intent intentFragment2 = new Intent(this,ListViewActivity.class);
                startActivity(intentFragment2);
                fragmentTransaction.replace(R.id.ll_content, new Fragment1());
                toolbar.setTitle("讨论专区");
                break;
            case 2:

                Intent intentFragment3 = new Intent(this,AddArticles.class);
                startActivity(intentFragment3);
                fragmentTransaction.replace(R.id.ll_content, new Fragment1());
                toolbar.setTitle("讨论专区");
                break;
        }
        fragmentTransaction.commit();

    }

三个Fragment切换三个不同区的界面:

三个类似只展示其中一个

public class Fragment1 extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment1,container,false);
    }
}

activity_main.xml:

添加两个布局包含在其中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/mytoolbar"/>
    <include layout="@layout/drawlayout1"/>
</LinearLayout>

mytoolbar.xml:

设置ToorBar

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >
</androidx.appcompat.widget.Toolbar>

drawlayout1.xml:

设置菜单项样式

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- 主界面-->
    <LinearLayout
        android:id="@+id/ll_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start">
    <!-- 导航视图-->
    <ListView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:id="@+id/lv_drawer"

        android:background="#FFFFFF"
        android:choiceMode="singleChoice"
        android:divider="@color/transparent"
        android:dividerHeight="0dp"
        android:clickable="true"/>

    </LinearLayout>

</androidx.drawerlayout.widget.DrawerLayout>

对ActionBar进行修改

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Android自定义ActionBar报错

https://blog.csdn.net/weixin_44223180/article/details/107372143


文章部分技术参考:https://blog.csdn.net/miyuexingchen/article/details/52232751?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-1

你可能感兴趣的:(Android)