片段中findViewById片段、findViewById

2023-09-05 23:16:11 作者:.斬殺.

有没有办法找到一个片段的范围内的视图通过ID?我使用了一系列片段来呈现一个专门的列表。该片段是从布局装,所以他们的部件都具有相同的ID。

我想我可以想出一个方法,让每个插件(或右后)创建过程中自定义的ID。然而,这将是一个要好很多,如果我能以某种方式限制findViewById的片段的范围。

解决方案

 私人查看myFragmentView;

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
    myFragmentView = inflater.inflate(R.layout.myLayoutId,集装箱,假);
    MyView的= myFragmentView.findViewById(R.id.myIdTag)

    返回myFragmentView;
}
 

Is there any way to find a view by id within the scope of a fragment? I'm using a series of fragments to render a specialized list. The fragments are loaded from a layout, so their widgets all have the same ids.

肖战 青游记 片段曝光,节目中遭冷待,唯有贾玲对他释放温暖

I suppose I can figure out a way to give each widget a custom id during (or right after) creation. However, it would be a lot nicer if I could somehow limit the findViewById to the scope of the fragment.

解决方案

private View myFragmentView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    myFragmentView = inflater.inflate(R.layout.myLayoutId, container, false);
    myView = myFragmentView.findViewById(R.id.myIdTag)

    return myFragmentView;
}

 
精彩推荐
图片推荐