Reputation: 555
It's been all day, 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. 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've tried replacing the entire fragment from inside itself and from the main activity, changing the view programmatically and other stuff by I hadn't any success.
Please help!
EDIT:
I can display the error view, I just check the internet availability inside the OnCreateView method:
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View view;
if(CheckNetwork.isInternetAvailable(getActivity())){
view = inflater.inflate(R.layout.fragment_with_connection, container, false);
}
else{
view = inflater.inflate(R.layout.layout_no_connectivity, container, false);
Button button = (Button)view.findViewById(R.id.button_retry);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View mview) {
//DUNNO WAT TO DO
}
});
}
EDIT: I have used the answer by @daro2189 to solve my problem, who was slightly different: in this case I only need to change the view of ONE of two fragments inside a view pager.
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_with_connection, container, false);
initNoInternetView(view);
if (!CheckNetwork.isInternetAvailable(getActivity())) {
if (noInternetView != null) {
View child = view.findViewById(R.id.child );
child.setVisibility(View.GONE);
noInternetView.setVisibility(View.VISIBLE);
}
} else {
getInfoFromInternet();
}
return view;
}
private void initNoInternetView(final View view) {
if(view == null)
return;
RelativeLayout parentView = (RelativeLayout)view.findViewById(R.id.layout_no_connectivity);
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
noInternetView = inflater.inflate(R.layout.layout_no_connectivity, null);
noInternetView.setVisibility(View.GONE);
parentView.addView(noInternetView);
final Button button = (Button)noInternetView.findViewById(R.id.button_retry);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View mview) {
if(!CheckNetwork.isInternetAvailable(getActivity())){
if(noInternetView != null){
noInternetView.setVisibility(View.VISIBLE);
noInternetView.bringToFront();
}
} else {
if(noInternetView != null){
noInternetView.setVisibility(View.GONE);
View child = view.findViewById(R.id.child);
child.setVisibility(View.VISIBLE);
getInfoFromInternet();
}
}
}
});
}
Upvotes: 1
Views: 9657
Reputation: 531
Don't waste time on that, use this library: https://github.com/medyo/dynamicbox
Upvotes: 1
Reputation: 587
Please provide some code.
My tips: In main layout you should have FragmentLayout with id=main_fragment In activity create method "noInternetView()" which show on id=main_fragment a fragment with button "no internet" When is no internet run function noInternetView() other shows normal view
Some code:
//This code put into noInterneView() - because it opens fragment with no internet layout
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
NoInternetFragment bdf = new NoInternetFragment();
ft.replace(R.id.main_fragment, bdf);
ft.addToBackStack(null);
ft.commit();
By changing Fragment use code above to open normal fragment
And:
If(noInternetConnection){
//open fragment with no internet laout - code above
} else {
//open fragment with normal layout
}
I hope I help :)
--- UPDATE -----
private View noInternetView = null;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_with_connection, container, false);
initNoInternetView(view);
if(!CheckNetwork.isInternetAvailable(getActivity())){
if(noInternetView != null)
noInternetView.setVisibility(View.VISIBLE);
}
}
private void initNoInternetView(View view){
if(view == null)
return;
RelativeLayout parentView = (RelativeLayout)view.findViewById(R.id.parent_view);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
noInternetView = inflater.inflate(R.layout.layout_no_connectivity, null);
parentView.addView(noInternetView);
final Button button = (Button)noInternetView.findViewById(R.id.button_retry);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View mview) {
if(!CheckNetwork.isInternetAvailable(getActivity())){
if(noInternetView != null){
noInternetView.setVisibility(View.VISIBLE);
noInternetView.bringToFront();
}
} else {
if(noInternetView != null)
noInternetView.setVisibility(View.GONE);
}
}
});
}
//XML layout
//Remember to in xml layout (fragment_with_connection) your main layout was a //RelativeLayout with id parent_view
//something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
//here put your view pager
</RelativeLayout>
Upvotes: 1