gpt4 book ai didi

android - 如何在 ListView 中将 webview 作为一行加载?

转载 作者:行者123 更新时间:2023-11-30 02:34:29 25 4
gpt4 key购买 nike

我使用自定义适配器为 ListView 行之一加载不同的布局。我使用的自定义布局包含一个 Webview。但是当我给它充气并检查 webview 的高度和宽度时,结果为零,而且 webview 在 Listview 中也不可见。谁能告诉我我做错了什么?MyCustomListAdapter getView函数如下

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
int type = getItemViewType(position);
if (v == null) {
// Inflate the layout according to the view type
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (type == 0) {
v = inflater.inflate(R.layout.list_row_web, parent, false);
WebView wv = (WebView) v.findViewById(R.id.webview1);
String html="<html><body>Hello World</body></html>";
wv.loadData(html, "text/html", "utf-8");
System.out.println(wv.getWidth());
System.out.println(wv.getHeight());
} else {
v = inflater.inflate(R.layout.list_row_default, parent, false);
TextView title = (TextView) v.findViewById(R.id.textView1);
TextView desc = (TextView) v.findViewById(R.id.textView2);
title.setText("Title");
desc.setText("Description");
}
}
return v;
}

我的 list_row_web.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res/co.silverpush.app_native_ads_silverpush"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:layerType="software" />
</LinearLayout>

我想提一下,我实际上是先发出 HTTP POST 请求,然后将我从服务器获得的响应加载到我的 Web View 中。硬编码的 HTML 字符串有效,但在请求方法的情况下,我确实有使用 loadurl() 函数加载到 webview 中的 HTML 字符串,但没有出现,该行根本不会显示。

最佳答案

你正在以一种糟糕的方式回收你的行。使用这样的东西:

LISTITEM_ROW.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:background="@color/visualizer_background"
android:paddingBottom="20dp"
android:paddingTop="20dp" >

...

<WebView
android:id="@+id/newsListContent"
android:background="@color/transparent"
android:layerType="software"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="@+id/newsListTitle"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:layout_toRightOf="@+id/newsListImage" />

...

</RelativeLayout>

你的适配器:

public class FragmentNewsListAdapter extends BaseAdapter implements OnClickListener {

private final ArrayList<RadioMarcaNew> noticias;
private static LayoutInflater inflater=null;
private Activity act;

public FragmentNewsListAdapter(Activity act, ArrayList<RadioMarcaNew> d){

this.act=act;
this.noticias = d;
inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View vi = convertView;
if(act!=null) {

final NewsViewHolder holder;
if (convertView == null) {

vi = inflater.inflate(R.layout.listitem_news, null);
holder = new NewsViewHolder();

...
holder.content = (WebView) vi.findViewById(R.id.newsListContent);
...

vi.setTag(holder);
} else holder = (NewsViewHolder)vi.getTag();

final RadioMarcaNew noticia = noticias.get(position);

...
if(noticia.getHTMLContent()!=null && noticia.getHTMLContent().length()>0) {

holder.content.setWebViewClient(new MyWebViewClient());
holder.content.loadDataWithBaseURL(null,cadenaHTML, "text/html","utf-8", null);
holder.content.setBackgroundColor(act.getResources().getColor(R.color.transparent));
holder.content.setEnabled(false);
}
...

}
return vi;
}

class MyWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

return true;
}

@Override
public void onPageFinished(WebView view, String url) {

super.onPageFinished(view, url);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {

super.onPageStarted(view, url, favicon);
}

@Override
public void onLoadResource(WebView view, String url) {

super.onLoadResource(view, url);

}
}

@Override
public int getCount() {

return this.noticias.size();
}

@Override
public Object getItem(int position) {

return position;
}

@Override
public long getItemId(int position) {

return position;
}
}

class NewsViewHolder {

...
public WebView content;
...
}

最后,您应该将 ListView 的高度更改为 match_parent =)

关于android - 如何在 ListView 中将 webview 作为一行加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com