我已经设置了WebViewActivity
,它从意图中获取URL。我为onClickListener
设置了Recyclerview
,打开了WebViewActivity
并加载了URL。但是WebView
不会缓存打开的每个页面。我应该对代码进行哪些更改?
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();
}
}
答案 0 :(得分:0)
您可以将URL写入共享的首选项或文件(带有附加模式),然后每当需要历史记录时,只需从该位置读取内容即可。
如果要缓存Web网址,则可以选择WebView
Java代码
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
websettings.setAppCachePath(getContext().getCacheDir().toString());
websettings.setAppCacheEnabled(true);
websettings.setCacheMode(WebSettings.LOAD_DEFAULT);
我发现