Унаследованный WebView из библиотеки Android:
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
initDefaultSetting();
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
initDefaultSetting();
}
public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initDefaultSetting();
}
public MyWebView(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initDefaultSetting();
}
private void initDefaultSetting() {
WebSettings webSettings = this.getSettings();
webSettings.setJavaScriptEnabled(true);
setWebChromeClient(new WebChromeClient());
setWebViewClient(new MyWebViewClient());
}
/**
* Load Web View with url
*/
public void load(String url) {
this.loadUrl(url);
}
}
Унаследованный и переопределенный метод из WebViewClient из библиотеки
Класс: MyWebViewClient
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
}
Макет: row_my_web.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">
<com.silicus.fluidforms.MyWebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Класс: родительский адаптер
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.silicus.fluidforms.MyWebView;
import com.silicus.fluidforms.R;
import com.silicus.fluidforms.expandable.ParentAdapter.MyViewHolder;
public class ParentAdapter extends RecyclerView.Adapter<MyViewHolder> {
private String urlList[] = {
"/1725876/v-chem-raznitsa-mezhdu-setwebviewclient-i-setwebchromeclient",
"https://developer.android.com/guide/webapps/webview",
"https://www.google.com",
"https://www.bling.com"};
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_my_web, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.myWebView.load(urlList[position]);
}
@Override
public int getItemCount() {
return urlList.length;
}
class MyViewHolder extends RecyclerView.ViewHolder {
MyWebView myWebView;
MyViewHolder(View itemView) {
super(itemView);
myWebView = itemView.findViewById(R.id.myWebView);
}
}
}