I have four fragments in my app and I intend to use a custom action bar. Two of these fragments have buttons on the action bar. I have a TabsActivity.class
that has a ViewPager
loading the four fragments.
In my TabsActivity
and all four fragment classes i have a method setUpCustomActionBar()
that i call.
I call this method in onCreate()
for TabsActivity
and for the 4 fragments i call them in setMenuVisibility()
private void setUpCustomActionBar(Activity activity){
ViewGroup actionBarLayout = (ViewGroup) activity.getLayoutInflater().inflate(R.layout.actionbar_custom, null);
RelativeLayout menupin = (RelativeLayout) actionBarLayout.findViewById(R.id.rl_menu_map);
RelativeLayout menufilter = (RelativeLayout) actionBarLayout.findViewById(R.id.rl_menu_filter);
ActionBar actionBar = activity.getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
menufilter.setVisibility(View.GONE);
menupin.setVisibility(View.GONE);
}
}
For fragments i call it like this:
@Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (menuVisible){
if (getActivity() != null) {
this.setUpCustomActionBar(this.getActivity());
}
}
}
actionbar_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:background="@color/theme_light_blue">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:id="@+id/it_regionname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Region_defined"
android:textColor="@color/white"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/faq_arrow_down" />
</LinearLayout>
<RelativeLayout
android:id="@+id/rl_menu_map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:visibility="gone"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/menu_icon_pin"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_menu_filter"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/menu_icon_filter"/>
</RelativeLayout>
</LinearLayout>
I have TWO questions:
menupin
to be visible ONLY. In 3rd fragment i set menufilter
to be visible ONLY. In 1st and 4th fragment no layout SHOULD be visible BUT when i load the TabsActivity
i can still see menufilter
layout on fragment1. I can make it disappear by going to fragment2 and the switching to fragment1. Seems that setMenuVisiblity()
is not called for the 1st default fragment when activity is loaded. You can create class BaseFragment (and write setMenuVisibility here) -> then 4 fragments will extends from this class.
When creating fragment, it doesn't call setMenuVisibility method. Only when viewPager changes fragment (when we scroll viewPager), It calls setMenuVisibility. At first, the actionbar is shown with full view. So at the first time, from activity, I call to the first Fragment to hide those view on the actionbar and It work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.