本文介绍了如何从Recycler View onClick缓存WebView活动的多个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个 WebViewActivity ,它从意图中获取URL.我已经为我的 Recyclerview 设置了 onClickListener ,它会打开 WebViewActivity 并加载URL.但是, WebView 不会在打开的每个页面中都缓存每个.我应该对代码进行哪些更改?

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

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

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();
    }
}

推荐答案

您可以将URL写入共享的首选项或文件(使用附加模式),然后每当需要历史记录时,只需从该位置读取内容即可.

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

如果您要缓存Web URL,则可以使用 WebView

If you want cache web URLs then there is an option for WebView

Java代码

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

这篇关于如何从Recycler View onClick缓存WebView活动的多个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:02