findViewById in Fragment

后端 未结 30 2315
终归单人心
终归单人心 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:31

    According to the documentation on API level 11

    Reference, in Back Stack http://developer.android.com/reference/android/app/Fragment.html

    short code

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

    You need to inflate the Fragment's view and call findViewById() on the View it returns.

    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)
  • 2020-11-21 07:32

    Try This:

    View v = inflater.inflate(R.layout.testclassfragment, container, false);
    ImageView img = (ImageView) v.findViewById(R.id.my_image);
    
    return v;
    
    0 讨论(0)
  • 2020-11-21 07:33

    Use getView() or the View parameter from implementing the onViewCreated method. It returns the root view for the fragment (the one returned by onCreateView() method). With this you can call 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); 
    

    As getView() works only after onCreateView(), you can't use it inside onCreate() or onCreateView() methods of the fragment .

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

    I realise this is an old question, but the prevailing answer leaves something to be desired.

    The question is not clear what is required of imageView - are we passing it back as the view, or merely saving a reference for later?

    Either way, if the ImageView is coming from the inflated layout, the correct way to do this would be:

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

    try

    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;
    }
    
    0 讨论(0)
提交回复
热议问题