片段工具栏中的后退按钮
back button in toolbar of fragments
我有一个 activity,在选项卡式布局下有片段。这是我的应用层次结构 => MainActivity.class > FragmentExplore.class > FragmentOne.class 或 FragmentTwo.class 或 FragmentTheree.class。
我想在主activity的工具栏中设置后退按钮。我试图在这些 activity 和片段的所有 xml 和 java 文件中插入工具栏的 java 和 xml 代码。但是,后退按钮不起作用。
这是该工具栏后退按钮的 java 和 xml 代码:
<com.google.android.material.appbar.AppBarLayout
...>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
/>
</com.google.android.material.appbar.AppBarLayout>
Toolbar toolbar = view.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_rating);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "back pressed", Toast.LENGTH_SHORT).show();
}
});
同样,这是MainActivity.class:
public class MainActivity extends AppCompatActivity {
private final static String COLLAPSING_TOOLBAR_FRAGMENT_TAG = "collapsing_toolbar";
private final static String SELECTED_TAG = "selected_index";
private final static int COLLAPSING_TOOLBAR = 0;
private static int selectedIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedIndex = COLLAPSING_TOOLBAR;
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
new FragmentExplore(), COLLAPSING_TOOLBAR_FRAGMENT_TAG).commit();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SELECTED_TAG, selectedIndex);
}
}
}
片段探索:
public class FragmentExplore extends Fragment {
public FragmentExplore() {
}
@Override
public void onAttach(Context activity) {
super.onAttach(activity);
mainActivity = (MainActivity) activity;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_layout, container, false);
AppBarLayout appBarLayout = view.findViewById(R.id.tab_appbar_layout);
((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).setBehavior(new AppBarLayoutBehavior());
tabLayout = view.findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
viewPager = view.findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(tab_count);
toolbar = view.findViewById(R.id.toolbar);
setupToolbar();
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
}
});
return view;
}
public class MyAdapter extends FragmentPagerAdapter {
private MyAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@Override
public Fragment getItem(int position) {...}
@Override
public int getCount() {
return tab_count;
}
@Override
public CharSequence getPageTitle(int position) {...}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void setupToolbar() {
toolbar.setTitle(getString(R.string.app_name));
mainActivity.setSupportActionBar(toolbar);
}
}
和FragmentOne.class:
public class FragmentOne extends Fragment {
View view;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_frag_one, container, false);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menuo, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
}
我试着把那些工具栏的 xml 和 java 代码放在不同的地方,但无法解决这个问题。提前谢谢你。
你应该在调用后设置 setSupportActionBar
试试这个
private void setupToolbar() {
mainActivity.setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_rating);
mainActivity. getSupportActionBar().setTitle(getString(R.string.app_name));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "back pressed", Toast.LENGTH_SHORT).show();
}
});
}
将其添加到 tab_layout
并在 onCreateView
中初始化
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
/>
我有一个 activity,在选项卡式布局下有片段。这是我的应用层次结构 => MainActivity.class > FragmentExplore.class > FragmentOne.class 或 FragmentTwo.class 或 FragmentTheree.class。
我想在主activity的工具栏中设置后退按钮。我试图在这些 activity 和片段的所有 xml 和 java 文件中插入工具栏的 java 和 xml 代码。但是,后退按钮不起作用。
这是该工具栏后退按钮的 java 和 xml 代码:
<com.google.android.material.appbar.AppBarLayout
...>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
/>
</com.google.android.material.appbar.AppBarLayout>
Toolbar toolbar = view.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_rating);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "back pressed", Toast.LENGTH_SHORT).show();
}
});
同样,这是MainActivity.class:
public class MainActivity extends AppCompatActivity {
private final static String COLLAPSING_TOOLBAR_FRAGMENT_TAG = "collapsing_toolbar";
private final static String SELECTED_TAG = "selected_index";
private final static int COLLAPSING_TOOLBAR = 0;
private static int selectedIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedIndex = COLLAPSING_TOOLBAR;
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
new FragmentExplore(), COLLAPSING_TOOLBAR_FRAGMENT_TAG).commit();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SELECTED_TAG, selectedIndex);
}
}
}
片段探索:
public class FragmentExplore extends Fragment {
public FragmentExplore() {
}
@Override
public void onAttach(Context activity) {
super.onAttach(activity);
mainActivity = (MainActivity) activity;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_layout, container, false);
AppBarLayout appBarLayout = view.findViewById(R.id.tab_appbar_layout);
((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).setBehavior(new AppBarLayoutBehavior());
tabLayout = view.findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
viewPager = view.findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(tab_count);
toolbar = view.findViewById(R.id.toolbar);
setupToolbar();
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
}
});
return view;
}
public class MyAdapter extends FragmentPagerAdapter {
private MyAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@Override
public Fragment getItem(int position) {...}
@Override
public int getCount() {
return tab_count;
}
@Override
public CharSequence getPageTitle(int position) {...}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void setupToolbar() {
toolbar.setTitle(getString(R.string.app_name));
mainActivity.setSupportActionBar(toolbar);
}
}
和FragmentOne.class:
public class FragmentOne extends Fragment {
View view;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_frag_one, container, false);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menuo, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
}
我试着把那些工具栏的 xml 和 java 代码放在不同的地方,但无法解决这个问题。提前谢谢你。
你应该在调用后设置 setSupportActionBar
试试这个
private void setupToolbar() {
mainActivity.setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_rating);
mainActivity. getSupportActionBar().setTitle(getString(R.string.app_name));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "back pressed", Toast.LENGTH_SHORT).show();
}
});
}
将其添加到 tab_layout
并在 onCreateView
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
/>