Android Data Binding using include tag

前端 未结 5 884
迷失自我
迷失自我 2020-11-28 04:11

Update note:

The above example works properly, because release 1.0-rc4 fixed the issue of needing the unnecessary variable.

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 04:54

    Easy Complete Example

    Just set id to included layout, and use binding.includedLayout.anyView.

    This example helps passing a value to & accessing included views in code.

    Step 1

    You have layout_common.xml, want to pass String to included layout.

    You will create String variable in layout and refer this String to TextView.

    
        // declare fields
        
    
    
     //set field to your view.
    

    Step 2

    Include this layout to parent layout. Give an id to included layout, so that we can use that in binding class. Now you can pass String passedText to your tag.

    activity_main.xml

    
    
    
        
    
            
    
        
    
    
    • You can use now binding.includedLayout.textView in your class.
    • You can pass any variables to included layout like above.

      ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
      binding.includedLayout.textView.setText("text");
      

    Note Both layouts (parent & included) should be binding layout, wrapped with

提交回复
热议问题