问题
i'm trying to create a ListView with a Webview inside, but the app shows nothing. Here my Code:
The MainActivity where I set the CustomAdapter
public class Web_in_list1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.listView2);
ListViewAdapter adapter = new ListViewAdapter(this);
lv.setAdapter(adapter);
}
}
getView of the CustomAdapter Here I get the Layout for the ListView and ste the URL for the Webview
public View getView(int arg0, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.listitem, null);
WebView wv = (WebView)convertView.findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("http://www.google.com");
convertView.setTag(wv);
return convertView;
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:id="@+id/listView2"
android:layout_width="fill_parent"/>
</LinearLayout>
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
CLOSED! PROBLEM SOLVED WITH A SCROLLVIEW
回答1:
i'm trying to create a ListView with a Webview inside
You cannot reliably put scrollable things inside of other scrollable things. Hence, you cannot reliably:
- Put a
WebViewin aListViewrow - Put a
ScrollViewin aListViewrow - Put a
WebViewin aScrollView - Put a
ListViewin aScrollView
Moreover, a WebView is a very heavyweight widget and is not designed to have lots of copies floating around. Please consider using TextView for lightweight HTML rendering.
回答2:
Try this code, it worked for me.
public View getView(final int position, View view, ViewGroup parent) {
String Vurl = "your URL";
ViewHolder holder;
mHandler = new Handler();
if (view == null) {
holder = new ViewHolder();
view = mInflater.inflate(R.layout.listitem, null);
holder.browser = (WebView) view.findViewById(R.id.webview);
view.setTag(holder);
} else
holder = (ViewHolder) view.getTag();
holder.browser.getSettings().setJavaScriptEnabled(true);
holder.browser.getSettings().setPluginState(WebSettings.PluginState.ON);
holder.browser.loadUrl(Vurl);
holder.browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
return view;
}
class ViewHolder {
WebView browser;
}
}
来源:https://stackoverflow.com/questions/8456554/listview-with-webview