1

リスト ビューに余分な要素を挿入したいと考えています。たとえば、画像を含む無限のリストビューがあり、20 個の画像ごとにソースページへのリンクを含む webview を追加したいと考えています。

提案によると、 残念ながらNPEを受け取りますWebView 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 用の 2 つのビュー タイプを用意し、アイテムの位置に基づいて使い分けることができます。例えば...

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 

そして、これに関する有用なブログ投稿を 1 つ見つけました - 「handling-listviews-with-multiple-row-types

于 2013-08-21T08:48:59.880 に答える