0

I'm trying to do what I thought it was a simple task: implementing a fragment that display different views based on internet availability. To be clear: try to start youtube app without connectivity, you will see a beautiful error screen with a 'Retry' button. When you click the button the fragment (of just the view, or something else) reloads and, if internet is now available, display the correct items. I have two fragments inside a ViewPager. the one fragment have recyclerview and it retrieve data from firebase.I want the first fragment to display an error message and a 'Retry' button. When the button is pressed, just like the youtube app does, I want to reload the content of the fragments and display the correct stuff, if the network is available. I don't know how can I do this.enter image description here Please help!

Fragment_1.java

public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle b)
{
    View view=inflater.inflate(R.layout.fragment_home,group,false);
    recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
    myref= FirebaseDatabase.getInstance().getReference().child("/Blog");
    FirebaseRecyclerAdapter<Blog,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Blog,BlogViewHolder>(
            Blog.class,
            R.layout.individual_row,
            BlogViewHolder.class,
            myref
    ) {
        @Override
        protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
            viewHolder.setTitle(model.getTitle());
            viewHolder.setDescription(model.getDescription());
            viewHolder.setImage(model.getImage());
        }
    };
    recyclerView.setAdapter(recyclerAdapter);
    return view;
}

}

2 Answers2

1

Instead of replacing fragments on network change just create a view same as screen provided by you add it fragment which has recyclerView. and on network change just show or hide this view.

prashant17
  • 1,520
  • 3
  • 14
  • 23
1

Youtube uses a separate view like retry, if internet is not available it hides recylerview. On the other hand, if internet is available in the device then it hides Retry view then loads recyclervbiew and automatically loads the list with Swiprefresh view. Ofcourse everything is happenning in same fragment not in separate fragment like you are asking. Suppose retry is TextView, if internet is not available setVisibilty Visible.and Recyclerview setVisibility GONE. If internet is available then setVisibility for retry Textview is GONE and for recyclerview setvisibility Visible.

 if(internet){
        getDataFromserver();
        tvRetryView.setVisibilty(GONE);
        recylerview.setVisibility(VISIBLE);
    }else{
        tvRetryView.setVisibilty(VISIBLE);
        recylerview.setVisibility(GONE);
    }
esQmo_
  • 1,464
  • 3
  • 18
  • 43
Babul
  • 1,076
  • 7
  • 9