How to set click listener to LinearLayout using data binding

后端 未结 3 598
独厮守ぢ
独厮守ぢ 2021-02-15 12:39

I am currently trying to set a click listener to a LinearLayout view in the .xml layout file using data binding.

I have managed to get it to wo

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-15 13:18

    You can handle any view click event like below. Hope it helps!

    1) Create interface for handle click event like below.

    interface OnClickHandlerInterface {
        void onClick(View view)
    }
    

    2) Implement that click listener in action class as shown below

    class MainActivity implements OnClickHanderInterface{
        @Override
        void OnClick(View view){
    
        }
    }
    

    3) Now bind this interface in XML file.

    
    
        
    
    

    4) Now register this interface in action class with use of binding object

    mActivityMainBinding.clickHandler = this
    

    5) Now set onClick on any which you want to set click listener. For you it's LinearLayout

    
    
    

    6) Now handle click when your linearLayout clicked you can get click on interface which is implemented in action class.

    @Override
    void OnClick(View view){
        switch(view.getId()){
        case R.id.linearLayout:
            // Handler click and do some actions
            break;
        }
    }
    

    6) As mentioned above you can get layout click by data binding. Hope it will work for you.

    Thanks

    Happy Coding!!

提交回复
热议问题