从 PopupMenu 调用 Fragment 方法
Call Fragment method from PopupMenu
所以我试图从 activity class 中的片段 class 调用一个方法。在我的 activity 中,我有底部导航视图(和其中的 PopupMenu),我试图从中调用片段中的方法。
这是我的 activity 解决方案,我在谷歌搜索中找到了解决方案,但它对我不起作用:
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;
}
};
}
还有我的片段(我在这个 class 中还有其他几个用于地图的方法,但这是我正在测试的方法):
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()。
您正在对从未添加到片段管理器的片段实例调用 test
。
// You initialize this field
MapFragment map = new MapFragment();
protected void onCreate(Bundle savedInstanceState) {
...
// Then you initialize a new MapFragment you add to the fragment manager
Fragment selectedFragment = null;
selectedFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
}
...
// Then you call `test` on the instance that's not attached to anything
map.test();
解决方案是使用一个片段并引用它。并且您还需要小心处理正确保存和恢复片段的状态。
MapFragment map; // Declare but don't initialize here
protected void onCreate(Bundle savedInstanceState) {
...
// Initialize fragment instead of creating a new one if there is no state already
if (savedInstanceState == null) {
map = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, map).commit();
}
else {
// If there is state, the fragment will be restored by the system, so just
// get a reference to it
map = getSupportFragmentManager().findFragmentById(R.id.fragment_containter)
}
}
...
// Now calling test on the correctly attached fragment should work
map.test();
希望对您有所帮助!
所以我试图从 activity class 中的片段 class 调用一个方法。在我的 activity 中,我有底部导航视图(和其中的 PopupMenu),我试图从中调用片段中的方法。
这是我的 activity 解决方案,我在谷歌搜索中找到了解决方案,但它对我不起作用:
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;
}
};
}
还有我的片段(我在这个 class 中还有其他几个用于地图的方法,但这是我正在测试的方法):
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()。
您正在对从未添加到片段管理器的片段实例调用 test
。
// You initialize this field
MapFragment map = new MapFragment();
protected void onCreate(Bundle savedInstanceState) {
...
// Then you initialize a new MapFragment you add to the fragment manager
Fragment selectedFragment = null;
selectedFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
}
...
// Then you call `test` on the instance that's not attached to anything
map.test();
解决方案是使用一个片段并引用它。并且您还需要小心处理正确保存和恢复片段的状态。
MapFragment map; // Declare but don't initialize here
protected void onCreate(Bundle savedInstanceState) {
...
// Initialize fragment instead of creating a new one if there is no state already
if (savedInstanceState == null) {
map = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, map).commit();
}
else {
// If there is state, the fragment will be restored by the system, so just
// get a reference to it
map = getSupportFragmentManager().findFragmentById(R.id.fragment_containter)
}
}
...
// Now calling test on the correctly attached fragment should work
map.test();
希望对您有所帮助!