setUserVisibleHint with Fragments for android

你。 提交于 2019-12-11 03:02:12

问题


Im using 3 fragments in my ViewPager adapter. I will be loading data from Parse(parse.com) and displaying them in recycler views. The following code is causing my app to crash. What my understanding is that when my MainActivity loads, this is my first fragment ie, it gets viewed by user immediately so the setUserVisibleHint function gets called immediately and in that v.findViewById code causes a null pointer exception since setContentView hasnt/ may not been called. My proof for this is if i add a 1sec delay to setUserVisisbleHint then my code works properly.

Now I want to add server PULL requests using Parse, add data in a list to an adapter and populate recyclerview AFTER user views the page so

1) Should i add all the code in setUserVisisbleHint and just add a 0.5secc delay so it gets executed after setContentView is called ensuring I dont get a null pointer exception error OR

2) Is there a better way/ other functions I can use to achieve the same?

public class NewsFeed extends Fragment {
        LinearLayoutManager mLayoutManager;
        boolean _areLecturesLoaded=false;
        View v;
        ProgressBar bar;

        public NewsFeed() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            v=inflater.inflate(R.layout.fragment_news_feed, container, false);

            RecyclerView mRecyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(v.getContext());
            mRecyclerView.setLayoutManager(mLayoutManager);    
            MyStickyAdapter mAdapter = new MyStickyAdapter(v.getContext());    
            mRecyclerView.setAdapter(mAdapter);
           // mRecyclerView.addItemDecoration(new StickyRecyclerHeadersDecoration(mAdapter));
            return v;
        }

        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            if (isVisibleToUser && !_areLecturesLoaded ) {
                _areLecturesLoaded = true;
                v.findViewById(R.id.asd).setVisibility(View.GONE);
            }
        }


    }//Closes Fragment

Im using this library for my recycler view StickyHeaderRecyclerView


回答1:


I would highly recommend avoiding adding delays especially if you're running this on the main thread. To avoid the NPE, try moving the findViewById(R.id.asd) into your onCreateView method after you inflate the view:

@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        v=inflater.inflate(R.layout.fragment_news_feed, container, false);
        v.findViewById(R.id.asd).setVisibility(View.GONE);

This is assuming that R.id.asd is in R.layout.fragment_news_feed

This post could also provide some insight: setUserVisibleHint called before onCreateView in Fragment



来源:https://stackoverflow.com/questions/32037356/setuservisiblehint-with-fragments-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!