首页 文章

片段中的findViewById

提问于
浏览
882

我试图在片段中创建一个ImageView,它将引用我在片段的XML中创建的ImageView元素 . 但是, findViewById 方法仅在扩展Activity类时才有效 . 无论如何,我还可以在片段中使用它吗?

public class TestClass extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ImageView imageView = (ImageView)findViewById(R.id.my_image);
        return inflater.inflate(R.layout.testclassfragment, container, false);
    }
}

findViewById 方法有一个错误,表明该方法未定义 .

30 回答

  • 1

    您可以覆盖在所有视图被夸大后立即调用的onViewCreated() . 它's the right place to fill in your Fragment'的成员 View 变量 . 这是一个例子:

    class GalleryFragment extends Fragment {
        private Gallery gallery;
    
        (...)
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            gallery = (Gallery) view.findViewById(R.id.gallery);
            gallery.setAdapter(adapter);
            super.onViewCreated(view, savedInstanceState);
        }
    }
    
  • 5

    您可以使用 Activity Object 调用 findViewById() ,进入片段内的 public void onAttach(Activity activity) 方法 .

    将Activity保存到变量中,例如:

    Fragment classprivate Activity mainActivity;onAttach() 方法中: this.mainActivity=activity;

    最后通过vairable执行每个findViewById: mainActivity.findViewById(R.id.TextView);

  • 17

    还有一个名为onViewCreated的方法 .

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview1);
    }
    
  • 64

    你需要给Fragment的视图充气并在它返回的 View 上调用 findViewById() .

    public View onCreateView(LayoutInflater inflater, 
                             ViewGroup container, 
                             Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.testclassfragment, container, false);
         ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
         return view;
    }
    
  • 4

    实现此目的的最佳方法如下:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
    rootView = inflater.inflate(R.layout.testclassfragment, container, false);
            ImageView imageView = (ImageView) rootView.findViewById(R.id.my_image);
            return rootView
    }
    

    通过这种方式,rootView可用于xml布局中定义的每个控件,并且代码更加清晰 .

    希望这可以帮助 :)

  • 1

    Fragment 类中,您将获得onViewCreated()覆盖方法,您应始终初始化视图,因为在此方法中您可以使用视图对象来查找您的视图,例如:

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.yourId).setOnClickListener(this);
    
        // or
        getActivity().findViewById(R.id.yourId).setOnClickListener(this);
    }
    

    永远记住在Fragment的情况下,如果从 onCreateView() 方法返回null或 super.onCreateView() ,则不会自动调用 onViewCreated() 方法 . 默认情况下,如果 ListFragmentListFragment 返回 FrameLayout ,则默认情况下会调用它 .

    注意:一旦 onCreateView() 已成功执行,您可以使用 getView() 获取类中任何位置的片段视图 . 即

    getView().findViewById("your view id");
    
  • 18
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.testclassfragment, container, false);
         ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
         return view;
    }
    

    请注意,如果使用 getView() 方法,它可能会导致 nullPointerException ,因为它返回根视图,它将是 onCreateView() 方法之后的某个视图 .

  • 1277

    使用这些东西的最简单方法是使用butterknife这样你可以通过@OnClick()添加尽可能多的Onclciklistener,如下所述:

    public class TestClass extends Fragment {
        @BindView(R.id.my_image) ImageView imageView;
        @OnClick(R.id.my_image)
        public void my_image_click(){
            yourMethod();
        }
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.testclassfragment, container, false);
            ButterKnife.bind(getActivity,view);
            return view;
        }
    }
    
  • 7

    使用gradle skeleton plugin,它会自动生成视图持有者类,并引用您的布局 .

    public class TestClass extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            MyLayout myLayout = new MyLayout(inflater, container, false);
            myLayout.myImage.setImageResource(R.drawable.myImage);
            return myLayout.view;
        }
    }
    

    现在假设您在 my_layout.xml 文件中声明了 ImageView ,它会自动为您生成myLayout类 .

  • 0

    方法 getView() 不适用于OnCreate和类似方法之外的片段 .

    您有两种方法,将视图传递给oncreate上的函数(这意味着您只能在创建视图时运行函数)或将视图设置为变量:

    private View rootView;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_contatos, container, false);
    }
    
    public void doSomething () {
        ImageView thumbnail = (ImageView) rootView.findViewById(R.id.someId);
    }
    
  • 2

    getView() 将给出根视图

    View v = getView().findViewByID(R.id.x);
    
  • 121

    1)首先膨胀片段的布局,然后你可以使用findviewbyId .

    View view = inflater.inflate(R.layout.testclassfragment, container, false);
                 ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
                 return view;
    
  • 3

    注意 :

    From API Level 26, you also don't need to specifically cast the result of findViewById as it uses inference for its return type.

    所以现在你可以做到,

    public View onCreateView(LayoutInflater inflater, 
                             ViewGroup container, 
                             Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.testclassfragment, container, false);
         ImageView imageView =  view.findViewById(R.id.my_image); //without casting the return type
         return view;
    }
    
  • 574

    尝试

    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;
    }
    
  • 8

    在片段中,我们需要该窗口的视图,以便我们创建此片段的onCreateView .

    然后获取视图并使用它来访问该视图元素的每个视图ID .

    class Demo extends Fragment
        {
            @Override
            public View onCreateView(final LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
            {
                View view =inflater.inflate(R.layout.demo_fragment, container,false);
                ImageView imageview=(ImageView)view.findViewById(R.id.imageview1);
    
                return view;
            }
        }
    
  • 8

    使用getView()或View参数来实现 onViewCreated 方法 . 它返回片段 (the one returned by onCreateView() method) 的根视图 . 有了这个你可以调用 findViewById() .

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        ImageView imageView = (ImageView) getView().findViewById(R.id.foo);
        // or  (ImageView) view.findViewById(R.id.foo);
    

    由于 getView() 仅在 onCreateView() 之后起作用,因此你的片段是 can't use it inside onCreate() or onCreateView() methods .

  • 8

    试试这个:

    View v = inflater.inflate(R.layout.testclassfragment, container, false);
    ImageView img = (ImageView) v.findViewById(R.id.my_image);
    
    return v;
    
  • 0

    我意识到这是一个古老的问题,但是流行的答案还有待改进 .

    问题不明确 imageView 需要什么 - 我们将它作为视图传回,还是仅仅为以后保存参考?

    无论哪种方式,如果 ImageView 来自膨胀的布局,正确的方法是:

    public class TestClass extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.testclassfragment, container, false);
            ImageView imageView = (ImageView)v.findViewById(R.id.my_image);
            return v;
        }
    }
    
  • 4

    根据API级别11的文档

    参考,在Back Stack中http://developer.android.com/reference/android/app/Fragment.html

    短代码

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hello_world, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Fragment #" + mNum);
        tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
        return v;
    }
    
  • 20

    同意在View上调用 findViewById() .

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View V = inflater.inflate(R.layout.testclassfragment, container, false);
        ImageView imageView = (ImageView) V.findViewById(R.id.my_image);
    
        return V;
    }
    
  • 0

    你必须给视图充气

    public class TestClass extends Fragment {
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.testclassfragment, container, false);
        ImageView imageView = (ImageView)v.findViewById(R.id.my_image);
        return v
    }}
    
  • 8

    1)声明您的布局文件 .

    public View onCreateView(LayoutInflater inflater,ViewGroup container, 
                                     Bundle savedInstanceState) {
        return inflate(R.layout.myfragment, container, false);
    }
    

    2)然后,获取您的视图的ID

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView nameView = (TextView) view.findViewById(R.id.textview1);
    }
    
  • 14

    首先获取片段视图,然后从此视图中获取ImageView .

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.testclassfragment, container, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
        return view;
    }
    
  • 10

    试试这个对我有用

    public class TestClass extends Fragment {
        private ImageView imageView;
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.testclassfragment, container, false);
            findViews(view);
            return view;
        }
    
        private void findViews(View view) {
            imageView = (ImageView) view.findViewById(R.id.my_image);
        }
    }
    
  • 9

    使用

    imagebutton = (ImageButton) getActivity().findViewById(R.id.imagebutton1);
    
    imageview = (ImageView) getActivity().findViewById(R.id.imageview1);
    

    它会工作

  • 8
    EditText name = (EditText) getView().findViewById(R.id.editText1);
    EditText add = (EditText) getView().findViewById(R.id.editText2);
    
  • 5

    您也可以在 onActivityCreated 方法中执行此操作 .

    public void onActivityCreated(Bundle savedInstanceState) { 
          super.onActivityCreated(savedInstanceState);
    }
    

    就像他们在这里一样:http://developer.android.com/reference/android/app/Fragment.html (deprecated in API level 28)

    getView().findViewById(R.id.foo);
    

    getActivity().findViewById(R.id.foo);
    

    是可能的 .

  • 4

    使用 getView() 返回片段的视图,然后可以调用 findViewById() 来访问片段视图中的任何视图元素 .

  • 9

    在onCreateView方法里面

    1)首先你必须膨胀你想要添加的布局/视图,例如 . 的LinearLayout

    LinearLayout ll = inflater.inflate(R.layout.testclassfragment, container, false);
    

    2)然后你可以从布局中找到你的imageView id

    ImageView imageView = (ImageView)ll.findViewById(R.id.my_image);
    

    3)返回膨胀的布局

    return ll;
    
  • 42

    布局inflater在这里进入图片 . 布局inflater是一个让我们能够使用的类java代码中的XML视图 . 因此,您可以使用以下代码在变量v中扩充根xml视图 . 然后使用v,您可以找到根视图v的子视图 .

    public class TestClass extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.testclassfragment, container, false);
        ImageView imageView = (ImageView)v.findViewById(R.id.my_image);
        return v;
        }
    }
    

相关问题