问题
I have setup a WebViewActivity
which gets the URL from the intent. I have setup an onClickListener
for my Recyclerview
which opens the WebViewActivity
and loads the URL. However the WebView
does not cache each an every page it opens. What changes should i make in the code?
WebViewActivity.java
public class WebViewActivity extends AppCompatActivity {
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Search");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
Intent intent = getIntent();
String url = intent.getStringExtra("url");
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
@Override
public void onBackPressed() {
if(myWebView.canGoBack()){
myWebView.goBack();
}else{
super.onBackPressed();
finish();
}
}
}
ArticleAdapter.java
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ArticleViewHolder> {
private static final String TAG = "ArticleAdapter";
private Context mContext;
private ArrayList<String> mlist;
private String url;
public ArticleAdapter(Context context, ArrayList<String> list) {
mContext = context;
mlist = list;
}
@Override
public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: called");
Log.d(TAG, "onCreateViewHolder: GOT REQUEST CALLED===================");
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.article_list, parent, false);
return new ArticleViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ArticleViewHolder articleViewHolder, int i) {
if(mlist.size()!=0){
url = mlist.get(articleViewHolder.getAdapterPosition());
articleViewHolder.name.setText(url);
articleViewHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, WebViewActivity.class);
intent.putExtra("url", articleViewHolder.name.getText());
mContext.startActivity(intent);
}
});
}
}
class ArticleViewHolder extends RecyclerView.ViewHolder {
TextView name;
public ArticleViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.tv_title);
}
}
@Override
public int getItemCount() {
return mlist.size();
}
}
回答1:
You can write URL to the shared preference or file (with append mode), then whenever you need history just read the contents from the location
If you want cache web URLs then there is an option for WebView
Java code
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
websettings.setAppCachePath(getContext().getCacheDir().toString());
websettings.setAppCacheEnabled(true);
websettings.setCacheMode(WebSettings.LOAD_DEFAULT);
this and this I have found useful, please refer these
来源:https://stackoverflow.com/questions/56562641/how-to-cache-multiple-urls-for-a-webview-activity-from-recycler-view-onclick