ToorBar结合DraweLayout

Activity

    private FragmentTransaction fragmentTransaction;
    private Fragment_one fragment_one;
    private Fragment_two fragmentTwo;
    private LinearLayout drawLayout;
    private DrawerLayout mDraw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取布局管理器
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        //fragment
        fragment_one = new Fragment_one();
        fragmentTwo = new Fragment_two();
        //加入管理器
        fragmentTransaction.add(R.id.frame, fragment_one);
        fragmentTransaction.add(R.id.frame, fragmentTwo);
        //默认展示
        fragmentTransaction.hide(fragmentTwo);
        //提交
        fragmentTransaction.commit();
        //初始化DrawLayout
        mDraw = findViewById(R.id.myDraw);
        //初始化侧拉布局
        drawLayout = findViewById(R.id.drawlayout);
        //list集合
        ListView listView = findViewById(R.id.list);
        List<String> list = new ArrayList<>();
        list.add("Fragment one");
        list.add("Fragment two");
        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1, list));



        //条目点击联动fragment
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.hide(fragment_one);
                fragmentTransaction.hide(fragmentTwo);
                switch (position) {
                    case 0:
                        fragmentTransaction.show(fragment_one);
                        break;
                    case 1:
                        fragmentTransaction.show(fragmentTwo);
                        break;
                }
                fragmentTransaction.commit();
                mDraw.closeDrawer(drawLayout);
            }
        });
        //点击图标打开侧拉篮
        ImageView mMenuList = findViewById(R.id.img_icon);
        mMenuList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDraw.openDrawer(drawLayout);
            }
        });
    }

layout

<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:id="@+id/myDraw"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary">

            <ImageView
                android:id="@+id/img_icon"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/menu" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="八维电商"
                android:textSize="20sp" />
        </android.support.v7.widget.Toolbar>

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"></FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/drawlayout"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/ic_launcher_round" />

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp">

        </ListView>

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值