Toolbar+DrawerLayout+Fragment结合使用

来源:互联网 发布:淘宝网汽车用品配件 编辑:程序博客网 时间:2024/06/10 13:58

需求是抽屉中有两个按钮,点击切换不同的Fragment,在第一个Fragment中调用系统相机

第一个Fragment的布局fragment_a

<LinearLayout 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"
              android:orientation="vertical"
             android:background="@android:color/holo_green_light"
             tools:context="alice.bw.com.day06toolbardrawerlayoutfragment.A_Fragment">


    <!-- TODO: Update blank fragment layout -->
    <Button
        android:id="@+id/xiangji_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击打开相机"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/img"/>


</LinearLayout>


第二个随便写一个布局即可fragment_b

主布局中Activity的布局activity_main中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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"
    android:id="@+id/drawer_layout"
    tools:context="alice.bw.com.day06toolbardrawerlayoutfragment.MainActivity">
    <LinearLayout
        android:id="@+id/center_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        />


    <FrameLayout
        android:id="@+id/layout"
        android:layout_below="@id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </LinearLayout>


    <LinearLayout
           android:id="@+id/left_layout"
            android:layout_width="300dp"
           android:background="@android:color/holo_blue_light"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:orientation="vertical">
            <Button
                android:id="@+id/fragment1_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onClick"
                android:text="点击展示第一个fragment"/>
            <Button
                android:id="@+id/fragment2_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onClick"
                android:text="点击展示第二个fragment"/>
        </LinearLayout>


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

创建两个Fragment  第一个Fragment

public class A_Fragment extends Fragment implements View.OnClickListener {
   private Button xiangji_btn;
    private ImageView img;


    public A_Fragment() {
        // Required empty public constructor
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container, false);
        xiangji_btn = (Button) view.findViewById(R.id.xiangji_btn);
        xiangji_btn.setOnClickListener(this);
        img = (ImageView) view.findViewById(R.id.img);
        return view;
    }


    @Override
    public void onClick(View view) {
        //跳转到相机页面
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,1);
    }
    //requestCode是请求码   resultCode是返回码
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode== getActivity().RESULT_OK){
            Bundle bundle = data.getExtras();
            Bitmap  bitmap = (Bitmap) bundle.get("data");
            img.setImageBitmap(bitmap);
        }
    }
}


主MainActivity页面



public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private Fragment[] mFragments ;
    private FragmentManager fm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除原有的导航栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //实例化ToolBar
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mFragments = new Fragment[]{new A_Fragment(),new B_Fragment()};
        fm = getSupportFragmentManager();
        //导航图标
        mToolbar.setNavigationIcon(R.mipmap.ic_launcher);
        //为导航  加一个监听
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDrawerLayout.openDrawer(Gravity.LEFT);//打开抽屉


            }
        });


    }




    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fragment1_btn:
                fm.beginTransaction().replace(R.id.layout,mFragments[0]).commit();
                break;
            case R.id.fragment2_btn:
                fm.beginTransaction().replace(R.id.layout,mFragments[1]).commit();
                break;
        }


    }
}


原创粉丝点击