我有很多活动和片段。我想在baseactivity中设置工具栏(所以只设置一次)。但需要从fragment访问工具栏(如-show、hide、changetile等方法)
有什么建议吗?
我已经尝试了下面的解决方案,但是当我想隐藏片段时,会显示npexception

public abstract class BaseActivity extends AppCompatActivity {

  Toolbar toolbar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResource());
    configureToolbar();
  }

  protected abstract int getLayoutResource();

  private void configureToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
  }

 public void hideToolbar() {
   toolbar.setVisibility(View.GONE);
 }

我的活动
public class MyActivity extends BaseActivity() {
}

我把HideToolbar叫做碎片状的:
public class MyFragment extends Fragment() {

onCreate() {

((Myactivity)getActivity).hideToolbar();
}

最佳答案

我在我的项目中加入了类似你的东西。这是样品。你可以参考一下。
BaseActivity.java等级:

public abstract class BaseActivity extends AppCompatActivity {

   @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(getLayoutResource());
    }


    protected abstract int getLayoutResource();
}

ToolBarActivity.java等级:
public abstract class ToolbarActivity extends BaseActivity {
protected Toolbar toolbar;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View contentView = findViewById(R.id.flToolbarContentContainer);
    if (contentView instanceof ViewGroup) {
        ((ViewGroup) contentView)
                .addView(LayoutInflater.from(this)
                        .inflate(getToolbarLayoutResource()
                                , (ViewGroup) contentView, false));
    }
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
 }

 @Override
 protected int getLayoutResource() {
     return R.layout.activity_toolbar;
 }

 protected abstract int getToolbarLayoutResource();

 public void showToolbar() {
     toolbar.setVisibility(View.VISIBLE);
 }

 public void hideToolbar() {
     toolbar.setVisibility(View.GONE);
 }
}

activity_toolbar.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlToolbarContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_toLeftOf="@+id/pbToolbarActivity"
    android:background="@color/blue_panel_day_background"
    android:theme="@style/ToolbarTheme" />

<FrameLayout
    android:id="@+id/flToolbarContentContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar" />
 </RelativeLayout>

MainActivity.java等级:
public class MainActivity extends ToolbarActivity {

@Override
protected int getToolbarLayoutResource() {
    return R.layout.activity_main;
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    homeFragment = HomeFragment.newInstance();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.llMainActivityContainer, homeFragment)
            .commit();
}
}

activity_main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/llMainActivityContainer"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

</LinearLayout>

现在,在Fragment类中,应用:
 ((ToolbarActivity) getActivity()).showToolbar();
 ((ToolbarActivity) getActivity()).hideToolbar();

关于android - BaseActivity中的工具栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41862109/

10-11 19:56