1

我的ListView有多个项目。每个商品有TextView和两个ImageView。我通过使用自定义适配器实现了这一点。我的ListView中的每个ImageView有5个图像。如何在自定义适配器中的ImageView中反复显示5张图像?我想在15秒的时间间隔内重复所有ImageView中的图像。ListView的自定义适配器

public class CategoryAdapter extends BaseAdapter { 
    public Activity adapterActivity; 
    LayoutInflater adapterInflater; 
    ImageLoader compImageLoader; 
    List<Categary> list; 

    ArrayList<String> category = new ArrayList<String>(); 
    ArrayList<String> leftImage = new ArrayList<String>(); 
    ArrayList<String> rightImage = new ArrayList<String>(); 

    public CategoryAdapter(Activity activity, List<Categary> listData) { 
     list = listData; 
     adapterActivity = activity; 
     adapterInflater = (LayoutInflater) adapterActivity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     compImageLoader = new ImageLoader(
       adapterActivity.getApplicationContext()); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return list.size(); 
    } 

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

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

    public class ViewHolder { 
     public ImageView lPic, rPic; 
     public TextView category; 
    } 

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

     View vi = convertView; 
     final ViewHolder holder; 
     if (vi == null) { 

      vi = adapterInflater.inflate(R.layout.categoryadapter, null); 
      holder = new ViewHolder(); 
      holder.lPic = (ImageView) vi.findViewById(R.id.imageView1); 
      holder.rPic = (ImageView) vi.findViewById(R.id.imageView2); 
      holder.category = (TextView) vi 
        .findViewById(R.id.textView_category); 
      vi.setTag(holder); 
     } else { 
      holder = (ViewHolder) vi.getTag(); 
     } 
     final Categary assingValue = list.get(position); 
     holder.lPic.setTag(assingValue.leftPicture); 
     holder.rPic.setTag(assingValue.rightPicture); 
     holder.category.setText(assingValue.id); 
     compImageLoader.DisplayImage(assingValue.leftPicture, holder.lPic); 
     compImageLoader.DisplayImage(assingValue.rightPicture, holder.rPic); 
     return vi; 
    }   
} 
+0

你可以使用一个animationdrawable。 – njzk2

+0

我如何在自定义适配器中实现animationdrawable?可能吗?你能解释我先生吗? –

+0

看到我的答案。如果您需要更多关于如何将图像置于动画中的细节,请发布一些代码,说明您的图像在我的整个适配器上方添加的适配器 – njzk2

回答

2

AnimationDrawable是,正确的组件:

在你getView(),我假设你有一个名为ImageViewimageView。我还假设您有一系列代表图像的Drawables:

AnimationDrawable animation = new AnimationDrawable(); 
for (Drawable image : images) { 
    animation.addFrame(image, 15 * 1000L); 
} 
imageView.setImageDrawable(animation); 
animation.start(); 

这可以根据您的图像在应用程序中的可用方式进行更改。

+0

谢谢先生。我用你的代码取决于我的应用程序。 –

0

可以在getView功能为每个ListView项目产卵线程和图像之间把15秒的延迟。

这样的:

final ImageView iv = (ImageView) v.findViewById(R.id.myImageView); 
    new Thread(new Runnable() { 

     @Override 
     public void run() { 
      while(true){ 
       iv.setImageResource(iconID); 
       Thread.sleep(15000); 
      } 
     } 
    }).start(); 
0

让我们说,你有你的图片的ID在数组中。

int i=0; 
new CountDownTimer(15000, 1000) { 

    @Override 
    public void onTick(long millisUntilFinished) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onFinish() { 
     imgView.setImageResource(arrayIds[i]); 
    i++; 
     if(i==arrayIds.length) 
      i=0; 
     start();//restart the count down timer for another 15 seconds again 
    } 
}.start();