Java 是否有办法让浮动操作按钮根据当前在导航主机中的片段执行某些操作?
我在一个活动项目上遇到了一些麻烦。MainActivity中有一个浮动操作按钮,我想在几个片段中使用它。因此,如果HomeFragment在nav_host_片段中,我希望浮动操作按钮执行操作1,如果SecondFragment在nav_host_片段中,我希望浮动操作按钮执行操作2 如果你在androidstudio中创建一个新项目并选择basicactivity,你将得到我正在使用的代码。 有一个MainActivity、两个片段(FirstFragment和SecondFragment)和一个导航控制器。FirstFragment首先显示,我希望操作按钮在这里调用方法Java 是否有办法让浮动操作按钮根据当前在导航主机中的片段执行某些操作?,java,android,android-fragments,Java,Android,Android Fragments,我在一个活动项目上遇到了一些麻烦。MainActivity中有一个浮动操作按钮,我想在几个片段中使用它。因此,如果HomeFragment在nav_host_片段中,我希望浮动操作按钮执行操作1,如果SecondFragment在nav_host_片段中,我希望浮动操作按钮执行操作2 如果你在androidstudio中创建一个新项目并选择basicactivity,你将得到我正在使用的代码。 有一个MainActivity、两个片段(FirstFragment和SecondFragment)和
doThing1()
。如果单击此片段中的按钮,则会转到SecondFragment。如果现在单击浮动操作按钮,我希望fab调用doThing2()
我该怎么办
我试过了
在碎片中
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);`
view.findViewById(R.id.button_first).setOnClickListener(v -> {
doThing1();
});
但这不起作用,因为findviewbyd
找不到fab,我猜是因为它在片段自己的布局中搜索,而不是在主活动中
我解决这个问题的唯一方法是在片段中获取对MainActivity的引用,从MainActivity调用一个公共方法并设置一个字段(设置为“first”或类似的值),然后在fab onClick方法中检查这个变量设置为什么,并根据这个变量执行不同的方法
因此,在创建的视图中:
MainActivity reference = (MainActivity) getActivity();
assert reference != null;
reference.changeFabActionTo("thing2");
然后在侦听器中单击
fab.setOnClickListener(view -> {
if(whichFragmentClick.equals("thing1")) {
// doThing1()}
if(whichFragmentClick.equals("thing2")) {
// doThing2()}
});
然而,这似乎不是解决这个问题的方法,我觉得这不是正确的方法。
有什么想法吗?许多可能的解决方案之一: Kotlin:
class MainActivity : AppCompatActivity() {
private lateinit var floatingButton: FloatingActionButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
floatingButton = findViewById(R.id.fab)
floatingButton.setOnClickListener { _ ->
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val visibleFragment = navHostFragment?.childFragmentManager?.fragments?.first()
when (visibleFragment != null) {
true -> {
when (visibleFragment::class.java) {
FirstFragment::class.java -> doStuff1()
SecondFragment::class.java -> doStuff2()
else -> doSomethingElse()
}
}
false -> {
println("<<<DEV>>> visibleFragment is not defined")
}
}
}
}
private fun doStuff1() {
println("<<<DEV>>> Fragment 1 is visible")
}
private fun doStuff2() {
println("<<<DEV>>> Fragment 2 is visible")
}
private fun doSomethingElse() {
println("<<<DEV>>> Fragment is undefined")
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
if (navHostFragment instanceof NavHostFragment) {
Fragment visibleFragment = navHostFragment.getChildFragmentManager().getFragments().get(0);
if (visibleFragment instanceof FirstFragment) {
doStuff1();
} else if (visibleFragment instanceof SecondFragment) {
doStuff2();
} else {
doSomethingElse();
}
}
}
});
}
public static void doStuff1() {
System.out.println("<<<DEV>> Fragment 1 is visible");
}
public static void doStuff2() {
System.out.println("<<<DEV>> Fragment 2 is visible");
}
public static void doSomethingElse() {
System.out.println("<<<DEV>>> Fragment is undefined");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
class MainActivity:AppCompatActivity(){
私有lateinit var floatingButton:FloatingActionButton
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
设置支持操作栏(findViewById(R.id.toolbar))
浮动按钮=findViewById(R.id.fab)
floatingButton.setOnClickListener{{{u->
val navHostFragment=supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val visibleFragment=navHostFragment?.childFragmentManager?.fragments?.first()
何时(visibleFragment!=null){
正确->{
何时(visibleFragment::class.java){
FirstFragment::class.java->doStuff1()
SecondFragment::class.java->doStuff2()
else->doSomethingElse()
}
}
错误->{
println(“未定义visibleFragment”)
}
}
}
}
私人娱乐场所1(){
println(“片段1可见”)
}
私人娱乐场所2(){
println(“片段2可见”)
}
私人娱乐doSomethingElse(){
println(“片段未定义”)
}
重写创建选项菜单(菜单:菜单):布尔值{
//为菜单充气;这会将项目添加到操作栏(如果存在)。
菜单充气器(右菜单菜单主菜单)
返回真值
}
覆盖选项ItemSelected(项:菜单项):布尔值{
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
返回时间(item.itemId){
R.id.action\u设置->真
else->super.onOptionsItemSelected(项目)
}
}
}
Java:
class MainActivity : AppCompatActivity() {
private lateinit var floatingButton: FloatingActionButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
floatingButton = findViewById(R.id.fab)
floatingButton.setOnClickListener { _ ->
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val visibleFragment = navHostFragment?.childFragmentManager?.fragments?.first()
when (visibleFragment != null) {
true -> {
when (visibleFragment::class.java) {
FirstFragment::class.java -> doStuff1()
SecondFragment::class.java -> doStuff2()
else -> doSomethingElse()
}
}
false -> {
println("<<<DEV>>> visibleFragment is not defined")
}
}
}
}
private fun doStuff1() {
println("<<<DEV>>> Fragment 1 is visible")
}
private fun doStuff2() {
println("<<<DEV>>> Fragment 2 is visible")
}
private fun doSomethingElse() {
println("<<<DEV>>> Fragment is undefined")
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
if (navHostFragment instanceof NavHostFragment) {
Fragment visibleFragment = navHostFragment.getChildFragmentManager().getFragments().get(0);
if (visibleFragment instanceof FirstFragment) {
doStuff1();
} else if (visibleFragment instanceof SecondFragment) {
doStuff2();
} else {
doSomethingElse();
}
}
}
});
}
public static void doStuff1() {
System.out.println("<<<DEV>> Fragment 1 is visible");
}
public static void doStuff2() {
System.out.println("<<<DEV>> Fragment 2 is visible");
}
public static void doSomethingElse() {
System.out.println("<<<DEV>>> Fragment is undefined");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar Toolbar=findviewbyd(R.id.Toolbar);
设置支持操作栏(工具栏);
FloatingActionButton fab=findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
片段navHostFragment=getSupportFragmentManager().findFragmentById(R.id.nav_主机_片段);
if(navHostFragment的navHostFragment实例){
片段visibleFragment=navHostFragment.getChildFragmentManager().getFragments().get(0);
if(FirstFragment的visibleFragment实例){
doStuff1();
}else if(SecondFragment的visibleFragment实例){
doStuff2();
}否则{
doSomethingElse();
}
}
}
});
}
公共静态无效doStuff1(){
System.out.println(“您可以使用simple when(switch)并检查最新的片段实例类型,我不,您的方式完全正确。这就是您的方式。(我不是讽刺)@圣骑士肯定有更好的方法吗?我已经开始研究使用ViewModels了,它们不是我想要的吗?谢谢你的回答!这看起来和我想要的完全一样!但是,我不明白kotlin,有没有办法将它转换成java?你可以使用Android Studio将kotlin转换成java。工具->kotlin->显示kotlin's字节码,然后按“反编译”。结果代码并不理想,但可以理解。@DavidtagtI不是Java专家,但我在发布之前更新了我的帖子和测试代码。@Davidtagt非常感谢你,这完全回答了我的问题!