简体   繁体   English

在Viewpager片段的自定义操作栏布局中显示/隐藏按钮

[英]Show/hide button in custom action bar layout for viewpager fragments

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. 我有一个TabsActivity.class ,它具有一个加载四个片段的ViewPager

In my TabsActivity and all four fragment classes i have a method setUpCustomActionBar() that i call. 在我的TabsActivity和所有四个片段类中,我都有一个方法setUpCustomActionBar()调用。

I call this method in onCreate() for TabsActivity and for the 4 fragments i call them in setMenuVisibility() 我在onCreate()TabsActivity调用此方法,并在setMenuVisibility()为这4个片段调用它们

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 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: 我有两个问题:

  1. I have to copy and repeat this method in every fragment. 我必须在每个片段中复制并重复此方法。 Is there a way to avoid repetition of code? 有办法避免代码重复吗?
  2. In 2nd fragment i set menupin to be visible ONLY. 在第二个片段中,我将menupin设置为仅可见。 In 3rd fragment i set menufilter to be visible ONLY. 在第三个片段中,我将menufilter设置为仅可见。 In 1st and 4th fragment no layout SHOULD be visible BUT when i load the TabsActivity i can still see menufilter layout on fragment1. 在第一个和第四个片段中,当我加载TabsActivity时,没有布局应该是可见的,但是我仍然可以在fragment1上看到menufilter布局。 I can make it disappear by going to fragment2 and the switching to fragment1. 我可以通过转到fragment2并切换到fragment1使其消失。 Seems that setMenuVisiblity() is not called for the 1st default fragment when activity is loaded. 似乎在加载活动时未为第一个默认片段调用setMenuVisiblity()
  1. You can create class BaseFragment (and write setMenuVisibility here) -> then 4 fragments will extends from this class. 您可以创建BaseFragment类(并在此处编写setMenuVisibility)->然后从该类扩展4个片段。

  2. When creating fragment, it doesn't call setMenuVisibility method. 创建片段时,它不会调用setMenuVisibility方法。 Only when viewPager changes fragment (when we scroll viewPager), It calls setMenuVisibility. 仅当viewPager更改片段时(当我们滚动viewPager时),它才调用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. 因此,从活动开始,我第一次调用第一个Fragment来隐藏操作栏上的那些视图,然后它起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM