1

我希望在我的列表视图中插入额外的元素。例如,我有无限的图像列表视图,在每 20 张图像之后,我希望添加带有源页面链接的 web 视图。

根据sugested Unfortunatly receive NPE onWebView sourse = (WebView) vi.findViewById(R.id.webViewSourse);

public class MediaItemAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    public MediaItemAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    @Override
    public int getViewTypeCount() {
        return 2; // any number what you need.
    }

    @Override
    public int getItemViewType(int position) {
        // view type is managed as zero-based index.
        if (position % 11 != 0)
            return 0;
        else
            return 1;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;

        if (convertView == null) {
            // View Recycling is managed separately based on its view type,
            // so you don't need to worry about view corruptions.

            int type = getItemViewType(position);
            switch (type) {
            case 0:
                // inflate imageview here.
                convertView = inflater.inflate(R.layout.item_composer, null);
            case 1:
                // inflate webview here.
                convertView = inflater.inflate(R.layout.banner_row, null);
            }
        }
        int type = getItemViewType(position);
        switch (type) {
        case 0:
            // inflate imageview here.

            ImageView thumb_image = (ImageView) vi.findViewById(R.id.imagePrev); // thumb


            String value = add.get("first_photo_url");
            if (value == null || value.trim().length() <= 0
                    || value.equalsIgnoreCase("null")
                    || value.trim().equalsIgnoreCase("")) {
                thumb_image.setImageResource(R.drawable.stub);
            } else {
                imageLoader.DisplayImage(add.get("first_photo_url"),
                        thumb_image);
                // // do nothing
            }
        case 1:
            // inflate webview here.
            WebView sourse = (WebView) vi.findViewById(R.id.webViewSourse);

            WebSettings webSettings = sourse.getSettings();
            webSettings.setJavaScriptEnabled(true);
            // banner.loadUrl("http://www.google.com");
            sourse.loadUrl(url);
        }

        return vi;
    }

}
4

2 回答 2

2

ListView 支持多个 ViewType。您可以在 ListAdapter 中覆盖 getViewTypeCount 和 getItemViewType 方法。在您的情况下,您可以准备两种视图类型,一种用于图像,另一种用于 webvie,并根据项目的位置分别使用它。例如...

private class MyCustomAdapter extends ArrayAdapter<Object> {

    public MyCustomAdapter(Context context) {
        super(context, 0);
    }

    @Override
    public int getViewTypeCount() {
        return 2;   //any number what you need.
    }

    @Override
    public int getItemViewType(int position) {
        //view type is managed as zero-based index.
        if(position % 20 != 0)
            return 0;
        else
            return 1;
    }

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

        if( convertView == null ){
            // View Recycling is managed separately based on its view type,
            // so you don't need to worry about view corruptions.

            LayoutInflater inflater = getLayoutInflater();
            int type = getItemViewType(position);
            switch (type){
                case 0:
                    //inflate imageview here.
                    convertView = inflater.inflate(R.layout.my_imageivew);
                break;
                case 1:
                    //inflate webview here.
                    convertView = inflater.inflate(R.layout.my_webview);
            }
            //do some data binding job here...
        }
        return convertView;
    }
}//end of inner class 

我发现了一篇有用的博客文章——“ handling-listviews-with-multiple-row-types

于 2013-08-21T08:48:59.880 回答
0

在 getView 方法中的列表适配器中,您可以检查以下条件:

if(position%20==0){
  //return your webview here instead of imageView or list row etc.
}
于 2013-08-21T08:53:47.803 回答