0

当我将 listview 与适配器一起使用时,该适配器在视图中添加了一个画廊,并且画廊适配器加载了图像,并使用 asynctask 加载图像,画廊显示在不同的地方。

public View getView(int position, View convertView, ViewGroup parent) {
    final int myPosition = position;
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    boolean isConverted = false;
    switch(typeOfCell.get(position))
    {
    case 0:
        if(convertView == null)
        {
            rowView = inflater.inflate(R.layout.newstextcelladapter, parent, false);
        }
        else
        {
            rowView = convertView;
        }
        break;
    case 1:
            if(convertView == null)
            {
                rowView = inflater.inflate(R.layout.newsimagecelladapter, parent, false);
            }
            else
            {
                rowView = convertView;
                isConverted = true;

            }

        Gallery gallery = (Gallery) rowView.findViewById(R.id.newsImageGallery);
        try{
            gallery.setAdapter(this.adaptersForGallery.get(position));
            Log.w("Adapter used", "" + this.adaptersForGallery.get(position));
            Log.w("URL used", "" + this.adaptersForGallery.get(position).mImageIds.get(0));
            gallery.setHorizontalFadingEdgeEnabled(false);
            gallery.setVisibility(View.GONE);
            gallery.setVisibility(View.VISIBLE);
        }
        catch(Exception e)
        {
            @SuppressWarnings("unused")
            int a =1;
            Log.w("FATAL","ERROR");
        }


        rowView.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                View view2 =  new View(MainActivity.context);
                DetailsImageNews.setView(MainActivity.dataBase.getCommentsForNews(MainActivity.existingForNews.get(myPosition)), 
                                         MainActivity.dataBase.getAuthorForNews(MainActivity.existingForNews.get(myPosition)),
                                         MainActivity.dataBase.getDateForNews(MainActivity.existingForNews.get(myPosition)),
                                         MainActivity.dataBase.getTimeForNews(MainActivity.existingForNews.get(myPosition)),
                                         MainActivity.dataBase.getDetailedTextForNews(MainActivity.existingForNews.get(myPosition)),
                                         MainActivity.dataBase.getTitleForNews(MainActivity.existingForNews.get(myPosition)),
                                         MainActivity.detailImageNews,
                                         adaptersForGallery.get(myPosition)
                                         ,view2);
                while (MainActivity.currentViewPosition != 1) {
                    MainActivity.mContainer.showNext();
                    Log.w("position", "" + MainActivity.currentViewPosition);
                    if (MainActivity.currentViewPosition > 2) {
                        MainActivity.currentViewPosition = 0;
                    }
                    MainActivity.currentViewPosition += 1;
                }
            }

        });
        break;
    case 2:
        if(convertView == null)
        {
            rowView = inflater.inflate(R.layout.newsvideocelladapter, parent, false);
        }
        else
        {
            rowView = convertView;
        }

        Uri uri=Uri.parse(videoUrls.get(position));
        VideoView video = (VideoView) rowView.findViewById(R.id.newsVideo);
        video.setVideoURI(uri);
        break;
    default:
        if(convertView == null)
        {
            rowView = inflater.inflate(R.layout.newstextcelladapter, parent, false);
        }
        else
        {
            rowView = convertView;
        }
        break;
    }
    TextView titleView = (TextView) rowView.findViewById(R.id.newsTitleView);
    TextView dateView = (TextView) rowView.findViewById(R.id.newsDateView);
    TextView timeView = (TextView) rowView.findViewById(R.id.newsTimeView);
    TextView commentsView = (TextView) rowView.findViewById(R.id.newsCommentView);
    titleView.setText(this.title.get(position));
    dateView.setText(this.date.get(position));
    timeView.setText(this.time.get(position));
    commentsView.setText(Integer.toString(this.comments.get(position)));
    return rowView;
}
public View getView(int position, View convertView, ViewGroup parent) {
    View i ;
    ImageView iv;
    ProgressBar mProgressBar;
    calledGetView+=1;
    Log.i("Image View:URL", ""+mImageIds.get(position));

    if(convertView == null)
    {
        i = MainActivity.factory.inflate(R.layout.imagespinner, null);
    }
    else
    {
        i = (ImageView) convertView;
    }
    mProgressBar =(ProgressBar) i.findViewById(R.id.imageProgress);
    iv = (ImageView) i.findViewById(R.id.image);

                try {
                      AsycTask task =  new AsycTask();
                      task.url = new URL(mImageIds.get(position));
                      task.iv = iv;
                      task.execute(iv);
                      iv.setAdjustViewBounds(true);
                      iv.setScaleType(ImageView.ScaleType.FIT_XY);
                      iv.setLayoutParams(new RelativeLayout.LayoutParams((int) (MainActivity.screenWidth*0.7), (int) ((MainActivity.screenWidth*0.7)*0.6)));
                      task.globalPosition = currentPosition;
                      task.mProgressBar = mProgressBar;
                      if(task.bm == null)
                      {

                      }


                        }
                        catch (IOException e) {
                             Log.i("Asyc task", "FATAL ERROR");
                              e.printStackTrace();
                        }


    return i;
}


public class AsycTask extends AsyncTask<ImageView, Void, Bitmap> {
public Bitmap bm;
public Bitmap bm2;
public int globalPosition;
public ProgressBar mProgressBar;
public int imagePosition;
public ImageView iv;
public URL url;
public Canvas c;

@Override
protected Bitmap doInBackground(ImageView... arg0) {
    iv = arg0[0];
    //Log.w("Loading url:",""+ url.getPath());
    try {
        bm2 = BitmapFactory.decodeStream((InputStream) url.getContent());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inSampleSize = 2;
        o.inDither = false;
        o.inPurgeable = true;
        try {
            bm2 = BitmapFactory.decodeStream(
                    (InputStream) url.getContent(), null, o);
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (OutOfMemoryError e1) {
        }
    }
    return bm2;
}
private void updateView(int index){
    View v = MainActivity.newsListView.getChildAt(index - 
            MainActivity.newsListView.getFirstVisiblePosition());
}
@Override
protected void onPostExecute(Bitmap result) {
    mProgressBar.setVisibility(View.GONE);
    iv.setVisibility(View.GONE);
    iv.setVisibility(View.VISIBLE);
    BitmapDrawable dr = ((BitmapDrawable) iv.getDrawable());
    if (dr != null) {
        Bitmap bmForRecyl = dr.getBitmap();
        if (bmForRecyl != null) {
            bmForRecyl.recycle();
        }
    }
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setImageBitmap(result);
    Log.i("Set", "setted");
}

@Override
protected void onPreExecute() {
}

    @Override
    protected void onProgressUpdate(Void... values) {
        mProgressBar.incrementProgressBy(5);
    }

我知道这个问题,但我不知道如何用带有 asyctask 的画廊解决它。我认为这是因为画廊没有加载新的适配器。如何更改适配器?

4

1 回答 1

0

You need to override getViewItemType.

getViewTypeCount() - this methods returns information how many types of rows do you have in your list

getItemViewType(int position) - returns information which layout type you should use based on position

Then you inflate layout only if it's null and determine type using getItemViewType.

Example :

   private static final int TYPE_ITEM1 = 0;
   private static final int TYPE_ITEM2 = 1;
   private static final int TYPE_ITEM3 = 2;  
    @Override
    public int getItemViewType(int position) 

    {

    int type;

    if (position== 0){ // your condition
        type = TYPE_ITEM1;  //type 0 for image
    } else if(position == 1){
        type = TYPE_ITEM2; //type 1 for text
    }else {
        type = TYPE_ITEM3; //type 2 for videos
    }
    return type;
    }
@Override
public int getViewTypeCount() {
    return 3;    //three different layouts to be inflated
}

In getView

 int type= getItemViewType(arg0);
 switch (type) {
 case TYPE_ITEM1: 
        // inflate layout for text
 break;
 case TYPE_ITEM2:
        //   inflate layout for image
 break;
 case TYPE_ITEM3:
        //   inflate layout for video
 break;    
 .... 

You can check the tutorial below

http://android.amberfog.com/?p=296

于 2013-06-28T05:12:56.783 回答