findViewById within fragment

前端 未结 5 1510
轮回少年
轮回少年 2020-12-13 19:14

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

相关标签:
5条回答
  • 2020-12-13 19:51

    You can do it by getView().findViewById()

    0 讨论(0)
  • 2020-12-13 19:55
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootview=null;
    
                 rootview=inflater.inflate(R.layout.fragment_web_view, container, false);
               ListView lv = (ListView)rootview.findViewById(android.R.id.list);
    
    return rootview;
            // Inflate the layout for this fragment
            //return inflater.inflate(R.layout.fragment_web_view, container, false);
        }
    
    0 讨论(0)
  • 2020-12-13 19:57
    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)
  • 2020-12-13 19:59

    From inside the Fragment:

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

    From the enclosing Activity:

    getFragmentManager().findFragmentByTag("YourFragmentTag").getView().findViewById(R.id.your_view);
    

    or

    getFragmentManager().findFragmentById(R.id.your_fragment).getView().findViewById(R.id.your_view);
    
    0 讨论(0)
  • 2020-12-13 20:00

    Yes, there is a way, you can find it through rootView. First find the rootView of your fragment rootView=getView(); and then use rootView.findViewById(...);

    0 讨论(0)
提交回复
热议问题