Hide Tablayout Bar in android

时光怂恿深爱的人放手 提交于 2019-12-30 07:09:25

问题


I have a activity with toolbar ,Tablayout , viewpager with fragments like that

I want to implement toolbar material search on all the fragments

like that

but the problem is TAB Bar . I want to hide the tab bar(Call,Chat,Contact) on Search Open and unhide it when the search close

How can I hide the tabbar ?

I am using this library for material Search view implementation

layout code is below

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
   >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <FrameLayout
            android:id="@+id/toolbar_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"

            android:minHeight="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <com.miguelcatalan.materialsearchview.MaterialSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        </FrameLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabGravity="center"/>

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

    <include layout="@layout/content_dashboard" />


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

回答1:


here is your solution, I tried this in fragment.

1. Write two methods like below in your Main Activity which having tablayout for hiding and showing tablayout.

    public static void showTabLayout() {
                parentTabs.setVisibility(View.VISIBLE);
            }

    public static void hideTabLayout() {
                parentTabs.setVisibility(View.GONE);

            }

2.Create Interface

 public interface FragmentLifecycle {

        public void onPauseFragment();
        public void onResumeFragment();

    }

3. Implement interface and call hide/show tab-layout methods

CameraFragment.java

package demo.com.demo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * Created by rucha on 24/11/16.
 */
public class CameraFragment extends Fragment implements FragmentLifecycle {

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = null;
        rootView = inflater.inflate(R.layout.fragment_blue,
                container, false);
        setupUI(rootView);
        return rootView;
    }

    private void setupUI(View rootView) {
    }


    @Override
    public void onPauseFragment() {
        MainActivity.showTabLayout();
    }

    @Override
    public void onResumeFragment() {
        MainActivity.hideTabLayout();
    }
}

I hope this is useful. Happy Coding!!



来源:https://stackoverflow.com/questions/41121270/hide-tablayout-bar-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!