我正在为我的联系人应用程序使用选项卡式布局。使用搜索视图时,我需要隐藏选项卡布局(需要在选项卡的位置显示搜索视图)。选项卡布局是在MainActivity中设置的。我正在尝试在名为“联系人片段”的片段中隐藏标签布局。
MainActivity.java -
public class MainActivity extends AppCompatActivity {
TabLayout tab;
ViewPager viewPager;
RecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab = findViewById(R.id.tab);
viewPager = findViewById(R.id.viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(2);
tab.setupWithViewPager(viewPager);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/colorPrimary"
app:tabTextColor="@color/offWhite"
app:tabIndicatorColor="@android:color/white"
app:tabSelectedTextColor="@android:color/white"
app:tabIndicatorHeight="2dp"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
ContactsFragment.java
public class ContactsFragment extends Fragment {
BottomNavigationView bottomNavigation;
DBHelper dbHelper;
public ContactsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for getActivity() fragment
final View view = inflater.inflate(R.layout.fragment_contacts, container, false);
AddFrag(new ContactsHomeFragment(), 0);
bottomNavigation = view.findViewById(R.id.bottomNav);
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.navNew) {
AddFrag(new NewContactFragment(), 1);
} else if (id == R.id.navDelete) {
AddFrag(new DeleteContactFragment(), 1);
} else if (id == R.id.navHome) {
AddFrag(new ContactsHomeFragment(), 1);
} else if (id == R.id.navSearch) {
AddFrag(new SearchContactFragment(), 1);
view.findViewById(R.id.tab).setVisibility(View.INVISIBLE);
}
return true;
}
});
dbHelper = DBHelper.getDB(getActivity());
if (!dbHelper.checkDB()) {
dbHelper.createDB(getActivity());
}
dbHelper.openDB();
if (dbHelper.getCount() == 0) {
bottomNavigation.setSelectedItemId(R.id.navNew);
} else {
bottomNavigation.setSelectedItemId(R.id.navHome);
}
return view;
}
public void AddFrag(Fragment fragment, int flag) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (flag == 0) {
ft.add(R.id.container, fragment);
} else {
ft.replace(R.id.container, fragment);
}
ft.addToBackStack(null);
ft.commit();
}
}
fragment_contacts.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ContactsFragment"
android:orientation="vertical">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/nav_items"
android:layout_alignParentBottom="true">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
答案 0 :(得分:0)
在MainActivity中,设置一个公共功能以设置选项卡布局的可见性,请使用View.GONE将其隐藏。然后在片段中,只要您想隐藏TabLayout,请使用
((MainActivty)getActivity()).yourMethodToHideTabBar();