【问题标题】:Is there a way to have a floating action button do something dependant on which fragment is currently in nav host?有没有办法让浮动操作按钮根据导航主机中当前的片段来执行某些操作?
【发布时间】:2021-05-29 18:03:45
【问题描述】:

我在单个活动项目上遇到了一些问题。 MainActivity 中有一个浮动操作按钮,我想在几个片段中使用它。因此,如果 HomeFragment 在 nav_host_fragment 中,我希望浮动操作按钮执行操作 1,如果 SecondFragment 在 nav_host_fragment 中,我希望浮动操作按钮执行操作 2。

如果您在 android studio 中创建一个新项目并选择基本活动,您将获得我正在使用的代码。 有一个 MainActivity、两个片段(FirstFragment 和 SecondFragment)和一个导航控制器。首先显示 FirstFragment,这是我希望操作按钮调用方法 doThing1() 的位置。如果您单击此片段中的按钮,它会将您带到 SecondFragment。如果您现在单击浮动操作按钮,我希望工厂调用 doThing2() 我该怎么办?

我试过了

在片段中


   public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
           super.onViewCreated(view, savedInstanceState);`
   
   view.findViewById(R.id.button_first).setOnClickListener(v -> {
   doThing1();
    });

但这不起作用,因为findViewById 没有找到工厂,我猜是因为它在片段自己的布局中搜索,而不是在主要活动中。

我解决这个问题的唯一方法是在片段中获取对 MainActivity 的引用,从 MainActivity 调用公共方法并设置一个字段(设置为“first”或类似的东西),然后在 fab onClick 方法中检查这个变量是什么设置为并根据此变量执行不同的方法。

所以在onViewCreated:

MainActivity reference = (MainActivity) getActivity();
assert reference != null;
reference.changeFabActionTo("thing2");

然后在点击监听中

fab.setOnClickListener(view -> {
                if(whichFragmentClick.equals("thing1")) {
                    // doThing1()}
                if(whichFragmentClick.equals("thing2")) {
                    // doThing2()}
            });

然而,这似乎不是解决这个问题的方法,我觉得这不是正确的方法。 有什么想法吗?

【问题讨论】:

  • 不,你的方式完全没问题。这就是你的做法。 (我不是讽刺)
  • @paladin 肯定有更好的方法吗?我已经开始研究使用 ViewModel,它们不是我想要的吗?

标签: java android android-fragments


【解决方案1】:

许多可能的解决方案之一:

科特林:

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)
    }
  }
}

Java:

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

【讨论】:

  • 感谢您的回复!这看起来和我想要的完全一样!但是,我不懂kotlin,请问有没有办法将其转换为java?
  • 您可以使用 Android Studio 将 Kotlin 转换为 Java。工具 -> Kotlin -> 显示 Kotlin 的字节码,然后按“反编译”。结果代码将不理想,但可以理解。 @Davidtagt
  • 我不是 Java 专家,但在发布之前我已经更新了我的帖子和测试代码。 @Davidtagt
  • 非常感谢,这完全回答了我的问题!
【解决方案2】:

您可以使用简单的 when(switch) 并检查最新的片段实例类型,i

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多