findViewById in Fragment

后端 未结 30 2251
终归单人心
终归单人心 2020-11-21 06:39

I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById<

相关标签:
30条回答
  • 2020-11-21 07:39

    The method getView() wont work on fragments outside OnCreate and similar methods.

    You have two ways, pass the view to the function on the oncreate (what means you can only run your functions when the view is being created) or set the view as a variable:

    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);
    }
    
    0 讨论(0)
  • 2020-11-21 07:40

    You can override onViewCreated() which is called right after all views had been inflated. It's the right place to fill in your Fragment's member View variables. Here's an example:

    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);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:40

    1) Declare your layout file.

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

    2)Then, get the id of your view

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView nameView = (TextView) view.findViewById(R.id.textview1);
    }
    
    0 讨论(0)
  • 2020-11-21 07:40

    Use gradle skeleton plugin, it will automatically generate the view holder classes with the reference to your layout.

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

    Now assuming you had an ImageView declared in your my_layout.xml file, it will automatically generate myLayout class for you.

    0 讨论(0)
  • 2020-11-21 07:41

    Inside Fragment class you will get onViewCreated() override method where you should always initialize your views as in this method you get view object using which you can find your views like :

    @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);
    }
    

    Always remember in case of Fragment that onViewCreated() method will not called automatically if you are returning null or super.onCreateView() from onCreateView() method. It will be called by default in case of ListFragment as ListFragment return FrameLayout by default.

    Note: you can get the fragment view anywhere in the class by using getView() once onCreateView() has been executed successfully. i.e.

    getView().findViewById("your view id");
    
    0 讨论(0)
  • 2020-11-21 07:43

    Get first the fragment view and then get from this view your 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;
    }
    
    0 讨论(0)
提交回复
热议问题