Image GridView Inside Fragment

前端 未结 2 899
长发绾君心
长发绾君心 2020-12-31 17:51

I have just started to develop on the android platform after developing on iOS. I have looked around and I can\'t seem to figure it out. I am trying to have a grid view appe

相关标签:
2条回答
  • 2020-12-31 18:25

    It turns out a few simple modifications to the original code and it works.

    After debugging and setting breakpoints I was able to find that the context in the PhotoImageAdapter was a null pointer and therefore, causing the app to crash. It was the way I was initialising the adapter in my PhotoFragment and also the method I was initialising it in. Below is the code that works correctly for anyone else who is struggling with this.

        @Override
        public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.photos_layout,container,false);
            GridView gridView = (GridView) view.findViewById(R.id.photogridview);
            gridView.setAdapter(new MyAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
            return view;
        }
    

    Again this might not be the best or the only of doing it but this way worked for me. (PhotoImageAdapter.java had a name change to MyAdapter.java)

    0 讨论(0)
  • 2020-12-31 18:47

    hi chek the code which i just shared, i made the fragment with gridview so hopefully it will helpful for you check this link, there you will find full detail about gridview implementation with fragment for both device and tablet all the best

    check this files mainly in my answer it will sure helpful for u MasterFragment,MasterGridActivity,MyAdapter,gridview.xml

    How to Show Different Layouts inside Fragments

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