在ListView中新增item為WebView
阿新 • • 發佈:2019-01-27
今天在做一個demo,是要把一個WebView當做ListView的item加入到ListView中去,查找了點資料,然後自己實現了一個,發現是可以做出來的,只是在ListView中加入的這個WebView的上下滾動有點問題,好像是WebView的滾動條和ListView的滾動條衝突了,導致上下滑動不能響應WebView的滾動條,暫時還不知道怎麼去解決,只是把這個效果做出來了
首先是佈局檔案的程式碼:
main.xml
web_item.xml ,這個是含有webView的的那個item的佈局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
<?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/webView" android:layout_width="match_parent" android:layout_height="300dp" /> </LinearLayout>
item.xml,這個是正常的item的佈局
<?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" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp"/> </LinearLayout>
MainActivity.java
package com.slfly.webviewtest;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ListView;
public class MainActivity extends Activity {
private WebView webView;
private List<String> lists;
private ListView listView;
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
listView = (ListView) findViewById(R.id.listView);
mAdapter = new MyAdapter(MainActivity.this,lists);
listView.setAdapter(mAdapter);
}
public void initDatas(){
lists = new ArrayList<String>();
for(int i = 0 ; i< 20;i++){
String s = "item----" + i;
lists.add(s);
}
}
}
MainActivity初始化了ListView等元件,然後把ListView要顯示的資料來源都新增到容器中,傳給adapter
然後是MyAdapter的程式碼
package com.slfly.webviewtest;
import java.util.List;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<String> lists;
public MyAdapter(Context context, List<String> lists) {
this.context = context;
this.lists = lists;
}
@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) {
convertView = bindView(position, convertView, parent);
return convertView;
}
public View bindView(int position, View convertView, ViewGroup parent) {
int itemType = getItemViewType(position);
switch (itemType) {
case 0:
convertView = convertViewToWeb(convertView, parent);
break;
case 1:
convertView = convertViewToItem(position, convertView, parent);
break;
}
return convertView;
}
public View convertViewToWeb(View convertView, ViewGroup parent) {
final WebHolder webHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.web_item, null);
webHolder = new WebHolder();
webHolder.webView = (WebView) convertView
.findViewById(R.id.webView);
convertView.setTag(webHolder);
} else {
webHolder = (WebHolder) convertView.getTag();
}
webHolder.webView.getSettings().setJavaScriptEnabled(true);
webHolder.webView.requestFocus();
webHolder.webView.loadUrl("http://sina.cn/?from=www&ref=http%3A%2F%2Fwww.sina.com%2F");
/** 使webview自己處理開啟網頁事件,不呼叫系統瀏覽器開啟*/
webHolder.webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
/**使webview能夠響應back按鍵,點選back按鍵回退網頁,不會退出整個Activity*/
webHolder.webView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && webHolder.webView.canGoBack()){
webHolder.webView.goBack();
return true;
}
return false;
}
});
return convertView;
}
public View convertViewToItem(int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null
|| !(convertView.getTag() instanceof ViewHolder)) {
convertView = LayoutInflater.from(context).inflate(R.layout.item,
null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
} else if (convertView.getTag() instanceof ViewHolder) {
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
holder.tv.setText(lists.get(position) + "");
return convertView;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return 0;
} else {
return 1;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
public class ViewHolder {
public TextView tv;
}
public class WebHolder {
public WebView webView;
}
}
上一下最後執行的效果圖