adsl9002 2023-02-20 00:25 采纳率: 46.2%
浏览 44
已结题

MainActivity关联fragment中的点击事件

想要达到的效果,MainActivity中,点击左边的LeftFragment的button,右边的RightFragment动态加载为AnotherFragment.
不设置button监听的情况下,能正常显示还没动态加载前的画面

img


val btn: Button = findViewById(R.id.button)
设置button监听,到这步提示@layout/activimain不包含id按钮。
如果分别以findViewById和ViewBinding来监听点击事件,该怎么处理。

MainActivity.kt

package com.example.fragmenttest

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val btn: Button = findViewById(R.id.button)
        replaceFragment(RightFragment())
        btn.setOnClickListener {
            replaceFragment(AnotherRightFragment())
        }
    }

    private fun replaceFragment(fragment: Fragment) {
        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.rightLayout, fragment)
        transaction.commit()
    }
}

LeftFragment.kt

package com.example.fragmenttest

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import com.example.fragmenttest.databinding.LeftFragmentBinding

class LeftFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.left_fragment, container, false)
    }
}

RightFragment.kt

package com.example.fragmenttest

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class RightFragment : Fragment(){
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.right_fragment, container, false)
    }
}

AnotherRightFragment.kt

package com.example.fragmenttest

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class AnotherRightFragment: Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.another_right_fragment, container, false)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/leftFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/rightLayout"
        tools:layout="@layout/left_fragment"
        android:name="com.example.fragmenttest.LeftFragment"/>

    <FrameLayout
        android:id="@+id/rightLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/leftFrag"
        app:layout_constraintRight_toRightOf="parent">
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

left_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"/>

</LinearLayout>

right_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"
        android:text="@string/right_fragment"
        android:textSize="24sp" />

</LinearLayout>

another_right_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/another_fragment"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"
        android:textSize="24sp"/>

</LinearLayout>
  • 写回答

2条回答 默认 最新

  • 不咕鸟会咕咕 2023-02-20 00:39
    关注

    在 LeftFragment 中使用 ViewBinding 来监听 Button 的点击事件可以像这样:
    LeftFragment.kt

    package com.example.fragmenttest
    
    import android.os.Bundle
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import androidx.fragment.app.Fragment
    import com.example.fragmenttest.databinding.LeftFragmentBinding
    
    class LeftFragment : Fragment() {
        private var _binding: LeftFragmentBinding? = null
        private val binding get() = _binding!!
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View {
            _binding = LeftFragmentBinding.inflate(inflater, container, false)
            return binding.root
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            binding.button.setOnClickListener {
                (activity as MainActivity).replaceFragment(AnotherRightFragment())
            }
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            _binding = null
        }
    }
    

    在 LeftFragment 中获取 Button 的引用不需要使用 findViewById,可以通过 ViewBinding 来实现。首先在 LeftFragment 中添加 ViewBinding 的支持,在 build.gradle 文件中添加以下代码

    buildFeatures {
        viewBinding true
    }
    

    然后在 LeftFragment 中添加 _binding 和 binding 属性。在 onCreateView() 方法中使用 LeftFragmentBinding.inflate() 方法来获取 ViewBinding 实例。在 onViewCreated() 方法中设置 Button 的点击事件。

    为了调用 MainActivity 中的 replaceFragment() 方法,需要将 Fragment 中的 activity 强制转换为 MainActivity 类型。为了避免转换失败导致应用崩溃,最好在转换前加上 if (activity is MainActivity) 的判断。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月20日
  • 已采纳回答 2月20日
  • 创建了问题 2月20日

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备