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

时间:2014-11-07 08:25:35

标签: android listview android-webview

我使用自定义适配器为其中一个ListView行加载不同的布局。我使用的自定义布局包含Webview。但是当我给它充气并检查webview的高度和宽度时,它会变为零,同样也不会在Listview中看到webview。 谁能告诉我我做错了什么? 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请求,然后将我从服务器获得的响应加载到我的webview中。 硬编码的HTML字符串可以工作,但是在请求方法的情况下,我确实使用loadurl()函数加载到webview中的HTML字符串,但没有任何内容出现,该行根本不会显示。

2 个答案:

答案 0 :(得分:1)

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

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 =)

答案 1 :(得分:0)

您在每个行中为新XML充气,这对性能不利。最好将static holder与单XML一起使用。您只需根据textview变量隐藏/显示 webview / type

如果您坚持使用您的代码,请尝试删除此if

if (v == null)