>

프래그먼트 내부의 버튼에서 onClick 이벤트를 잡으려고하지만 작동하지 않습니다.

팁이 있습니까?

이 주요 활동이 있으며 bottomNavigation을 통해 조각을 호출합니다. MainActivity.kt :

   class MainActivity : FragmentActivity()  {
    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                showFragmentSetup()
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }
    fun showFragmentSetup(){
        val setupFragment = SetupFragment()
        val manager = supportFragmentManager
        val transaction = manager.beginTransaction()
        transaction.replace(R.id.setupFragment, setupFragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    }
}

activity_main.xml은 프래그먼트를 구성 할 linearLayout의 컨테이너입니다.

activity_main.xml

   <LinearLayout
        android:id="@+id/setupFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

내 조각은 간단합니다. 단추가 있고이 버튼에서 onClickEvent를 잡으려고합니다.

   class SetupFragment : Fragment(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_setup, container, false)
        val view: View = inflater!!.inflate(R.layout.fragment_setup, container, false)
        btnSetup.setOnClickListener { view ->
            Log.d("btnSetup", "Selected")
        }
        // Return the fragment view/layout
        return view
    }
    companion object {
        fun newInstance(): SetupFragment {
            return SetupFragment()
        }
    }
}


  • 답변 # 1

    여기서 리스너를 설정하기 전에 돌아 왔습니다 :

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                                  savedInstanceState: Bundle?): View? {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_setup, container, false)
            val view: View = inflater!!.inflate(R.layout.fragment_setup, container, false)
            btnSetup.setOnClickListener { view ->
                Log.d("btnSetup", "Selected")
            }
            // Return the fragment view/layout
            return view
        }
    
    

    다음과 같이 해보십시오 :

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                                  savedInstanceState: Bundle?): View? {
    
            val view: View = inflater!!.inflate(R.layout.fragment_setup, container, false)
            view.btnSetup.setOnClickListener { view ->
                Log.d("btnSetup", "Selected")
            }
            // Return the fragment view/layout
            return view
        }
    
    

  • 답변 # 2

    해당 버튼에보기를 제공하지 않습니다

    lateinit var mView: View
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mView=inflater.inflate(R.layout.product_list,container,false)
        mView.addProduct.setOnClickListener {
            val intent=Intent(activity,ProductAddActivity::class.java)
            startActivity(intent)
        }
        return mView
    }
    
    

  • 답변 # 3

    "SetupFragment"에서 "onViewCreated"기능을 사용해야한다고 생각합니다

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        btnSetup.setOnClickListener { view ->
            Log.d("btnSetup", "Selected")
        }
    }
    
    

관련 자료

  • 이전 winsock - Doevents 또는 서브 클래스없는 VB6 TCP IP 통신
  • 다음 python - 연속 문자열에서 정규식 일치 만 선택