设置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内容,问题是加载css的过程。
webview.load()
这个方法会出现乱码,查过之后发现要手动设置编码方式为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代码不一样的情况下面的代码是有问题的,所以这个方法是偷懒的权宜之计
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);