【发布时间】:2019-09-29 11:25:08
【问题描述】:
所以我试图从活动类中的片段类中调用一个方法。在我的活动中,我有底部导航视图(以及其中的 PopupMenu),我试图从中调用片段中的方法。
这是我在谷歌上找到的解决方案的活动,但它对我不起作用:
public class MenuActivity extends AppCompatActivity {
MapFragment map = new MapFragment();
TextView textView;
Toolbar toolbar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
toolbar = (Toolbar) findViewById(R.id.toolbar);
((TextView) toolbar.findViewById(R.id.text_main)).setText(MainActivity.selectedButton);
Fragment selectedFragment = null;
selectedFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav_map);
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.nav_map:
selectedFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
break;
case R.id.nav_sensors:
selectedFragment = new SensorsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
break;
case R.id.nav_apps:
PopupMenu popup = new PopupMenu(MenuActivity.this, findViewById(R.id.nav_apps));
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.bottom_nav_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.nav_floor:
map.test();
//Toast.makeText(getApplicationContext(),"Floor",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_room:
Toast.makeText(getApplicationContext(),"Room",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_search:
Toast.makeText(getApplicationContext(),"Search",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
};
}
还有我的片段(我在这个类中有几个其他的 Maps 方法,但这是我正在测试的方法):
public class MapFragment extends Fragment{
View mView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_map, container, false);
return mView;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
public void test(){
Toast.makeText(getActivity(),"Test",Toast.LENGTH_SHORT).show();
}
}
还有,XML 文件:
activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:id="@+id/toolbar">
<TextView
android:id="@+id/text_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_centerHorizontal="true"/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_nav_map"
android:layout_below="@+id/toolbar"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav"
app:itemIconTint="@drawable/bottom_nav_color"
app:itemTextColor="@drawable/bottom_nav_color"
android:background="@color/colorPrimary"
app:labelVisibilityMode="labeled"
/>
</RelativeLayout>
fragment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/location_button"
android:text="LOC"
android:layout_gravity="bottom"
/>
</FrameLayout>
总而言之,当我在弹出菜单中选择第一个选项时,我试图调用方法 test()。
【问题讨论】:
标签: android android-activity methods fragment call