(4) Android Webview (II) and androidwebview
This article mainly introduces how to add webview to listview and implement pull-down refresh and effect
The direct process is as follows:
1. mainActivity layout file:
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.happy.demo.xlistview.XListView android:id="@+id/xlv_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f5f5f5" android:divider="#00000000" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:listSelector="#00000000" > </com.happy.demo.xlistview.XListView></RelativeLayout></span>
2. mainActivity main interface java code:
<span style="font-size:14px;">package com.happy.demo;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import com.happy.demo.adapter.MyAdapter;import com.happy.demo.xlistview.XListView;import com.happy.demo.xlistview.XListView.IXListViewListener;public class MainActivity extends Activity implements IXListViewListener {private XListView xlv_list;private List<String> lists = new ArrayList<String>();private MyAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);xlv_list = (XListView) this.findViewById(R.id.xlv_list);lists.add("http://www.qq.com");adapter = new MyAdapter(lists, MainActivity.this);xlv_list.setAdapter(adapter);xlv_list.setXListViewListener(this, 0);xlv_list.setPullLoadEnable(false);xlv_list.setRefreshTime();}@Overridepublic void onRefresh(int id) {new Handler().postDelayed(new Runnable() {@Overridepublic void run() {xlv_list.setRefreshTime();xlv_list.stopRefresh();}}, 3000);}@Overridepublic void onLoadMore(int id) {// TODO Auto-generated method stub}}</span>
3. adapter item layout xml code:
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/wb_webView" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout></span>
4. adapter java code:
<Span style = "font-size: 14px;"> package com. happy. demo. adapter; import java. util. list; import com. happy. demo. r; import android. content. context; import android. view. view; import android. view. viewGroup; import android. webkit. webView; import android. widget. baseAdapter;/*** @ author zhonglq * @ version V1.0 * @ Date 2014-10-25 10:53:37 */public class MyAdapter extends BaseAdapter {private List <String> lists; private Context context; public MyAdapter (List <String> lists, Context context) {this. lists = lists; this. context = context;} @ Overridepublic int getCount () {return lists. size () ;}@ Overridepublic Object getItem (int position) {return lists. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {if (convertView = null) {convertView = View. inflate (context, R. layout. list_item, null);} WebView wb_webView = (WebView) convertView. findViewById (R. id. wb_webView); setWebView (lists. get (position), wb_webView); return convertView;} private void setWebView (String url, WebView webView) {webView. loadUrl (url) ;}</span>
In this way, the webView can be embedded in the listview and the pull-down refresh effect can be implemented. There are not many codes and it is easy to understand, so I will not explain them much.
WebView usage in android
You cannot find the object. Find myWebview in the oncreate function and save the object as a global variable.
I mean, just take the findWindowbyid sentence to mHandler = new Handler ....
WebView myWebView get static Handler mHandler = null; The following is a global variable ....
Android WebView Problems
Import android. app. Activity;
Import android. OS. Bundle;
Import android. webkit. WebView;
Public class MainActivity extends Activity {
@ Override
Public void onCreate (Bundle icicle ){
Super. onCreate (icicle );
WebView wv = new WebView (this );
SetContentView (wv );
Wv. loadUrl ("www.baidu.com ");
}
}
Try this