【问题标题】:How to change between fragments with buttons inside the fragments in Kotlin?如何在 Kotlin 片段内的按钮之间切换片段?
【发布时间】:2020-04-18 07:48:09
【问题描述】:

我希望我不是在问一个重复的问题,而是只在 java 中找到了这个答案,我正在使用 Kotlin。我正在尝试使用按钮在片段之间进行交换。这是我的代码。首先,谢谢。

对于主要的

       class MainActivity : AppCompatActivity (){
        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
}

及其xml文件

           <fragment
            android:id="@+id/main"
            android:name="com.example.stackoverflow.fragment1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_fragment1"/>
</LinearLayout>

对于 Fragment1

      class fragment1 : Fragment(){
       override fun onCreateView(Layoutinflater: LayoutInflater,
                              container: ViewGroup?, savedInstantState: Bundle?): View? {
        return Layoutinflater.inflate(R.layout.fragment_fragment1, container, false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        button1.setBackgroundColor(Color.parseColor("#faaf45"))
     button1.setOnClickListener(){
           //Not sure what to put here
        }
}

还有片段2

       class fragment2 : Fragment(){
        override fun onCreateView(Layoutinflater: LayoutInflater,
                              container: ViewGroup?, savedInstantState: Bundle?): View? {
        return Layoutinflater.inflate(R.layout.fragment_fragment2, container, false)
    }
         override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
         button2.setBackgroundColor(Color.parseColor("#aaaf45"))
       button2.setOnClickListener(){
         //Not sure what to put here
        }
}

【问题讨论】:

  • 如果你想在片段之间导航,你应该使用导航组件developer.android.com/guide/navigation/…
  • 或者你可以使用FragmentTransactions,这和FragmentNavigator在Navigation组件中使用的机制是一样的

标签: android android-fragments button kotlin fragment


【解决方案1】:

在 MainActivity 中:

class MainActivity : AppCompatActivity (){
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    }

    fun changeFragment(fragmnet:Fragment){
      getFragmentManager()
                .beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }
}

在 MainActivity laout 中:

     <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 </LinearLayout>

在片段1中:

class fragment1 : Fragment(){
   override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View?  {
    return Layoutinflater.inflate(R.layout.fragment_fragment1, container,false)
    }
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    button1.setBackgroundColor(Color.parseColor("#faaf45"))
    button1.setOnClickListener(){
       (context as MainActivity).changeFragment(fragmnet2.newInstance())
    }
  companion object {
    fun newInstance(): fragment1 {
        return fragment1()
     }
    }
 }

在片段2中:

class fragment2 : Fragment(){
    override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View? {
    return Layoutinflater.inflate(R.layout.fragment_fragment2, container, false)
   }
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     super.onViewCreated(view, savedInstanceState)
     button2.setBackgroundColor(Color.parseColor("#aaaf45"))
   button2.setOnClickListener(){
     (context as MainActivity).changeFragment(fragmnet1.newInstance())
    }

  companion object {
      fun newInstance(): fragment2 {
        return fragment2()
       }
     }
 }

【讨论】:

  • 抱歉,失败了。我正在使用 kotlin,这看起来像 java。
  • 在 kotlin 中更改让我知道是否需要更多帮助。您也可以使用此链接了解更多详细信息:medium.com/thoughts-overflow/…
  • 这是科蒂林!我只是添加了一个简单的代码。但是解决方法是正确的。
  • 你应该用 改变主布局中的
  • 我编辑了答案并编写了完整的代码。请重试。
【解决方案2】:
 class fragment1 : Fragment(){
   override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View? {
    return Layoutinflater.inflate(R.layout.fragment_fragment1, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    button1.setBackgroundColor(Color.parseColor("#faaf45"))
 button1.setOnClickListener(){
    supportFragmentManager.beginTransaction()
    .replace(R.id.container, fragment2()).commit()        }
 }

还有片段2

class fragment2 : Fragment(){
    override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View? {
    return Layoutinflater.inflate(R.layout.fragment_fragment2, container, false)
}
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     super.onViewCreated(view, savedInstanceState)
     button2.setBackgroundColor(Color.parseColor("#aaaf45"))
   button2.setOnClickListener(){
        supportFragmentManager.beginTransaction()
    .replace(R.id.container, fragment1()).commit() 
    }
 }

【讨论】:

  • 使用&lt;fragment&gt;标签时不能使用replace()
  • 感谢重写,我尝试了该代码,结果如下:(29, 13): Unresolved reference: supportFragmentManager 我需要特定的导入吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 2019-03-07
  • 1970-01-01
  • 2018-01-24
相关资源
最近更新 更多