Skip to content

Instantly share code, notes, and snippets.

@darwind
Created March 20, 2017 22:30
Show Gist options
  • Save darwind/ed153ff267413d21f74203d6f5e1d200 to your computer and use it in GitHub Desktop.
Save darwind/ed153ff267413d21f74203d6f5e1d200 to your computer and use it in GitHub Desktop.
ViewPager with WebViews that shows individual ProgressBars for each WebView when it's loading
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.loadingview.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loadingview">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.loadingview;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<String> pages = new ArrayList<>();
pages.add("https://google.com");
pages.add("https://stackoverflow.com");
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomPagerAdapter(pages));
}
static class CustomPagerAdapter extends PagerAdapter {
private final List<String> pages;
CustomPagerAdapter(List<String> pages) {
this.pages = pages;
}
@Override
public int getCount() {
return pages.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final View viewItem = LayoutInflater.from(container.getContext()).inflate(R.layout.view_pager_item, container, false);
final WebView webView = (WebView) viewItem.findViewById(R.id.webView);
webView.setVisibility(View.INVISIBLE);
final ProgressBar progressBar = (ProgressBar) viewItem.findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
});
webView.loadUrl(pages.get(position));
container.addView(viewItem);
return viewItem;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment