双工具栏显示在片段上 [英] Double Toolbar Is Showing on Fragment

查看:74
本文介绍了双工具栏显示在片段上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的问题与双重工具栏有关,是在给片段充气(或说替换)时出现在我的应用程序中. 我有一个活动类,其中包含带有工具栏和工具栏图标的导航抽屉.

My problem is related to double toolbar is coming in my app while inflating(or say replacing) a fragment. I have an activity class which contains navigation drawer with it's toolbar with navigation toolbar icon.

有些片段我不想显示活动工具栏,我只需要显示片段工具栏:

There is some fragment i don't want to show the activity toolbar i need to show only fragment toolbar :

但是在替换它时,它会显示活动以及片段工具栏,任何对我有帮助的建议.

But while replacing it's showing activity as well as fragment toolbar , any suggestion helpful to me.

我已经尝试了:

我的片段:

public class FoodDetailsFragment extends Fragment{
    View view;
    private Toolbar toolbar;
    private ImageView imageView,tabBg;
    private CollapsingToolbarLayout collapsingToolbar;
    private TabPagerAdapter tabPagerAdapter;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    CoordinatorLayout.Behavior behavior;
    SendMessage listener;

     public  interface SendMessage{
        void sendData(String s);
}

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
            listener= (SendMessage) activity;

        }catch (ClassCastException exp){
            throw new ClassCastException("Class Cast Exp");
        }


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       view=inflater.inflate(R.layout.food_details,container,false);
        setHasOptionsMenu(true);
        listener.sendData("ds");

        imageView= (ImageView) view.findViewById(R.id.backdrop);
        tabBg= (ImageView) view.findViewById(R.id.tabBg);
        collapsingToolbar=(CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);

        setToolbar(view);
        setImage();

        mViewPager= (ViewPager) view.findViewById(R.id.viewpager);
        mTabLayout= (TabLayout) view.findViewById(R.id.detail_tabs);
        tabPagerAdapter=new TabPagerAdapter(getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(tabPagerAdapter);
        mTabLayout.setTabsFromPagerAdapter(tabPagerAdapter);
        mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position==0){
                    Glide.with(getActivity()).load(R.drawable.onet).into(imageView);
                    Glide.with(getActivity()).load(R.drawable.oneb).into(tabBg);
                } else {
                    Glide.with(getActivity()).load(R.drawable.twot).into(imageView);
                    Glide.with(getActivity()).load(R.drawable.twob).into(tabBg);
                }
                imageView.invalidate();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        return view;


    }
    private void setToolbar(View view) {
        toolbar= (Toolbar) view.findViewById(R.id.toolbarone);

        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

       ((AppCompatActivity) getActivity()).getSupportActionBar();
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true);




        if (toolbar != null) {
            ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        }


    }
    private void setImage() {
        Glide.with(this).load(R.drawable.resfor).into(imageView);
        Glide.with(getActivity()).load(R.drawable.resone).into(tabBg);
    }
    class TabPagerAdapter extends FragmentStatePagerAdapter {

        public TabPagerAdapter(FragmentManager fm){
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {

            switch (position){
                case 0:
                    FoodDetailedInfoFragment tab1 = new FoodDetailedInfoFragment();
                    return tab1;
                case 1:
                    FoodReviewFragment tab2 = new FoodReviewFragment();
                    return tab2;

                default:
                    return null;
            }




        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if(position==0){
                return "INFO";
            }else if(position==1){
                return "REVIEW";
            }


            return "INFO" ;
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.my_custom_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

}

片段xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"

        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView

                android:id="@+id/backdrop"

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="fitXY"
                app:layout_collapseMode="none" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbarone"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


            </android.support.v7.widget.Toolbar>


        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"

        app:layout_anchor="@+id/appbar"
        app:layout_anchorGravity="bottom"
        app:layout_collapseMode="none">

        <ImageView
            android:id="@+id/tabBg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:scaleType="fitXY" />

        <android.support.design.widget.TabLayout
            android:id="@+id/detail_tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:tabMaxWidth="0dp"
            android:layout_gravity="bottom"
            android:background="#00000000"
            app:layout_anchor="@+id/appbar"
            app:layout_anchorGravity="bottom"
            app:layout_collapseMode="none"
            app:tabGravity="fill"



            app:tabIndicatorColor="#fff"
            app:tabIndicatorHeight="2dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="#fff"
            app:tabTextColor="#fff" />

    </FrameLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

飞溅活动xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/container_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/rl"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:elevation="4dp"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.AppBarLayout>




            <FrameLayout
                android:layout_below="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:background="@color/faintwhite"
                android:id="@+id/container">

            </FrameLayout>





        </RelativeLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/spacing"
            android:src="@drawable/ic_share_grey_600_24dp"
            app:backgroundTint="@color/colorAccent" />

    </android.support.design.widget.CoordinatorLayout>


    <!-- Navigation Drawer-->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#000000">

    </android.support.v7.widget.RecyclerView>

</android.support.v4.widget.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"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp">


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:background="@color/faintwhite"
    android:id="@+id/container">
    <include
        android:id="@+id/tool_bar"
        layout="@layout/app_bar"/>
&lt;!&ndash;<RelativeLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <include
        android:id="@+id/tool_bar"
        layout="@layout/app_bar"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"

        android:layout_below="@+id/tool_bar">
    <LinearLayout
        android:id="@+id/lin_lay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />
    </LinearLayout>
    </LinearLayout>

</RelativeLayout>&ndash;&gt;

</FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        app:headerLayout="@layout/drawer_header"
        app:itemIconTint="@drawable/drawer_icon_selector"
        app:itemTextColor="@drawable/drawer_text_selector"
        app:menu="@menu/drawer" />

    &lt;!&ndash;<android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"

        android:background="#ffffff"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>&ndash;&gt;
    </android.support.v4.widget.DrawerLayout>-->

推荐答案

使用

((AppCompatActivity)getActivity()).getSupportActionBar().hide();

以编程方式隐藏操作栏

使用主题:

<style name="AppCompat" parent="Theme.AppCompat.NoActionBar">

</style>

这篇关于双工具栏显示在片段上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆