設(shè)置listview的點擊事件
ListView newslistView=(ListView) findViewById(R.id.show_news);
newslistView.setAdapter(new NewsAdapter(newss,NewsActivity.this,newslistView));
newslistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(NewsActivity.this,ArticleActivity.class);
News news = newss.get(position);
String AtUrl = "http://news-at.zhihu.com/api/4/news/"+news.getId();
intent.putExtra("AtUrl",AtUrl);
startActivity(intent);
}
});
新建WebView焦影,通過url獲取html內(nèi)容,問題是加載css的過程封断。
webview.load()
這個方法會出現(xiàn)亂碼斯辰,查過之后發(fā)現(xiàn)要手動設(shè)置編碼方式為utf8
WebView wv = (WebView)findViewById(R.id.webview);
String content = getUnicodeContent();
wv.getSettings().setDefaultTextEncodingName(“UTF -8”);
wv.loadData(content, “text/html”, “UTF-8”) ;
但仍沒有解決亂碼,所以改用loadDataWithBaseURL()
* @param baseUrl the URL to use as the page's base URL. If null defaults to
* 'about:blank'.
* @param data a String of data in the given encoding
* @param mimeType the MIMEType of the data, e.g. 'text/html'. If null,
* defaults to 'text/html'.
* @param encoding the encoding of the data
* @param historyUrl the URL to use as the history entry. If null defaults
* to 'about:blank'. If non-null, this must be a valid URL.
*/
public void loadDataWithBaseURL(String baseUrl, String data,
String mimeType, String encoding, String historyUrl) {
checkThread();
mProvider.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
}
加載css的方法貌似有很多坡疼,比如用js彬呻,但最后選擇了在以下方案。
要注意的是獲取到的html代碼不一樣的情況下面的代碼是有問題的柄瑰,所以這個方法是偷懶的權(quán)宜之計
WebView webview = (WebView) findViewById(R.id.ArticleView);
String html = "<html><header><style type='text/css'>"+css+"</style></header>"
+"<body>"+body+"</body></html>";
webview.loadDataWithBaseURL(null,html, "text/html", "utf-8", null);