片段中findViewById [英] findViewById within fragment

查看:147
本文介绍了片段中findViewById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找到一个片段的范围内的视图通过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;
}

这篇关于片段中findViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆