android 视图绑定-如何获得包含的布局的绑定?

qojgxg4l  于 2023-05-27  发布在  Android
关注(0)|答案(9)|浏览(190)

在使用视图绑定时,我遇到了几个未记录的案例。
第一:如何获得包含的视图布局部件的绑定?主绑定只能看到主布局中定义的项。
第二:如何获得合并布局部件的绑定。同样,主绑定只能看到主布局中的项目?

tyg4sfes

tyg4sfes1#

如果:
1.包含与通用布局(不合并节点),我们需要分配ID的一部分,这种方式在绑定,我们将有机会包括子部分

<include
    android:id="@+id/your_id"
    layout="@layout/some_layout" />

在您的活动代码中这样做:

private lateinit var exampleBinding: ActivityExampleBinding  //activity_example.xml layout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    exampleBinding = ActivityExampleBinding.inflate(layoutInflater)
    setContentView(exampleBinding.root)
    //we will be able to access included layouts view like this
    val includedView: View = exampleBinding.yourId.idOfIncludedView
//[...]
}

1.在外部布局中包含合并块。我们不能向它添加ID,因为合并块不是视图。假设我们有这样的永恒合并布局(merge_layout.xm):

<?xml version="1.0" encoding="utf-8"?>
<merge 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"
    tools:showIn="@layout/activity_example">

    <TextView
        android:id="@+id/some_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />
</merge>

要正确绑定这样的合并布局,我们需要:
在您的活动代码中:

private lateinit var exampleBinding: ActivityExampleBinding  //activity_example.xml layout
private lateinit var mergeBinding: MergeLayoutBinding  //merge_layout.xml layout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    exampleBinding = ActivityExampleBinding.inflate(layoutInflater)
    //we need to bind the root layout with our binder for external layout
    mergeBinding = MergeLayoutBinding.bind(exampleBinding.root)
    setContentView(exampleBinding.root)
    //we will be able to access included in merge layout views like this
    val mergedView: View = mergeBinding.someView
//[...]
}
fivyi3re

fivyi3re2#

关于您的第一个问题,您可以获得包含的布局的视图绑定。
下面是一个示例main_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:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

</LinearLayout>

MainFragment.java可以这样:

public class MainFragment extends Fragment {

    private MainFragmentBinding binding;
    private ToolbarBinding toolbarBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        binding = MainFragmentBinding.inflate(inflater, container, false);
        toolbarBinding = binding.toolbar;

        return binding.getRoot();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        toolbarBinding = null;
        binding = null;
    }
}

现在您有两个绑定:一个对应于主布局,另一个对应于包含的布局。

7uzetpgm

7uzetpgm3#

如果您想绑定包含的布局,

用于活动

YourMainLayoutBinding mainLayoutBinding = MainLayoutBinding.inflate(getLayoutInflater);

View view = mainLayoutBinding.getRoot();

YourIncludedLayoutBinding includedLayoutBinding = YourIncludedLayoutBinding.bind(View);

对于分片

YourMainLayoutBinding mainLayoutBinding = MainLayoutBinding.inflate(inflater,container,false);

View view = mainLayoutBinding.getRoot();

YourIncludedLayoutBinding includedLayoutBinding = YourIncludedLayoutBinding.bind(View);

确保如果您的主布局绑定父根是LinearLayout,则includedLayoutBinding父布局也是线性布局

vs91vp4v

vs91vp4v4#

假设我在activity_main.xml文件中包含一个布局,如下所示:

<include
    android:id="@+id/ll_layout1"
    layout="@layout/layout1"
    android:visibility="gone" />

假设我想改变它的可见性。我可以这样做:

activityMainBinding.llLayout1.root.visibility = View.VISIBLE
wgxvkvu9

wgxvkvu95#

在本例中,我忘记将id分配给include标记
现在,当你已经分配了id,你就可以得到绑定对象,

YourMainLayoutBinding.YourIncludeTagIDLayoutBinding
tuwxkamq

tuwxkamq6#

按照步骤:-

  1. private val binding : FragmentBinding通过viewBinding(FragmentBinding::bind)
    1.请确保在“onViewCreated(view:View,savedInstanceState:Bundle?)
val binding2 = binding.root.include_layout_id

例如瓦尔binding 2 = binding.root.tool_bar_layout
现在访问您的包含布局,视图在这里。例如

binding2.textView.text = "your text"
2lpgd968

2lpgd9687#

回答关于片段的第一个问题,假设您在MainFragment的xml文件中包含了一个“error_layout”。

<include
    layout="@layout/error_layout"
    android:id="@+id/layout_error"
    android:visibility="gone"/>

现在在“error_layout”中,你有一个id为的按钮:“btn_try_again”。您想为该按钮设置一个单击侦听器。
这就是如何使用fragment_main.xml文件的绑定对象获取对“btn_try_again”的引用。

binding.layoutError.btnTryAgain
nc1teljy

nc1teljy8#

使用数据绑定库。然后使用<layout>标记 Package XML布局

activity_main.xml

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

    ... 

    <include 
      android:id="@+id/toolbar"
      layout="@layout/toolbar" />
    
    ...

</LinearLayout>
</layout>

toolbar.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:orientation="vertical">

    <ImageView 
        android:id="@+id/ivImage"
        ... />

    <TextView 
        android:id="@+id/tvTitle"
        ... />

</LinearLayout>

MainActivity.kt

private lateinit var binding: ActivityMainBinding  

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

    // Access include layout views
    binding.toolbar.rootView.ivImage.setImageResource(R.drawable.ic_back_arrow)
    binding.toolbar.rootView.tvTitle.text = getString(R.string.home)
   
    ...
}
wkyowqbh

wkyowqbh9#

在include布局中,您必须创建一个Container布局,并在此处输入id。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/example"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent">
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

相关问题