BaseActivity中的工具栏

时间:2017-01-25 21:38:21

标签: android android-fragments android-activity toolbar

我有多个活动和片段。我想在BaseActivity中设置工具栏(所以只设置一次)。但需要从片段访问工具栏(方法如 - show,hide,changeTitle等) 有什么建议吗?

我在下面尝试过解决方案,但是当我想隐藏片段时,会显示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();
}

4 个答案:

答案 0 :(得分:4)

我在项目中加入了类似你的东西。这是样本。你可以参考它。

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();

答案 1 :(得分:0)

来自片段

on(map.infoWindow, "show", function () {
            on(dom.byId("submit"), "click", function () {
                alert("I should be saving");
            });
            on(dom.byId("delete"), "click", function () {
                alert("I should be deleting");
            });
        })

答案 2 :(得分:0)

使用:

public class Main {

    public static void main(String[] args) {
        System.out.println(
                IntStream.range(2, 1000000)
                         .map(e->factor(e))
                         .filter(e -> isPrime(e))
                         .boxed()
                         .collect(Collectors.toCollection(ArrayList::new))
                );

    }

    public static boolean isPrime(int input) {
        for (int i=2;i<input;i++) {
            if (input%i==0) {
                return false;
            }
        }
        return true;
    }

    public static int factor(int input) {
        return ( (input * (input + 1)) / 2);

    }
}

答案 3 :(得分:0)

要从ActionBar访问Fragment,请尝试以下方法

ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();

您现在可以访问ActionBar

的所有方法