(四) Android Webview 深入 (下)
本文主要介绍一下,如何在listview中加入webview,并且实现下拉刷新,效果如图:
废话不多说,直接过程:
1.mainActivity布局文件:
<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主界面java代码:
<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; @Override protected 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(); } @Override public void onRefresh(int id) { new Handler().postDelayed(new Runnable() { @Override public void run() { xlv_list.setRefreshTime(); xlv_list.stopRefresh(); } }, 3000); } @Override public void onLoadMore(int id) { // TODO Auto-generated method stub } }</span>
3.adapter适配器item布局xml代码:
<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代码:
<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; } @Override public int getCount() { return lists.size(); } @Override public Object getItem(int position) { return lists.get(position); } @Override public long getItemId(int position) { return position; } @Override public 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>
这样就可以实现在listview中嵌入webView并且实现下拉刷新的效果了,代码并不多,并且比较好懂,就不多作解释了。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。