【问题标题】:Custom Adapter of ListViewListView 的自定义适配器
【发布时间】:2013-12-17 16:17:31
【问题描述】:

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

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;
    }          
}

【问题讨论】:

  • 你可以为此使用动画绘制。
  • 如何在自定义适配器中实现动画绘制?可能吗?先生,您能解释一下吗?
  • 看我的回答。如果您需要有关如何将图像放入动画中的更多详细信息,请发布一些代码以显示您的图像在适配器中的使用方式
  • 我在整个适配器上方添加了先生。看看吧。
  • 不太清楚Categary 是什么,Categary 中的left 和rightPicture 是什么(我假设是url?其他的在哪里?),以及什么ImageLoader,我假设它是universalImageLoader?跨度>

标签: android multithreading android-listview android-imageview


【解决方案1】:

AnimationDrawable 是正确的组件:

在您的getView() 中,我假设您有一个名为imageViewImageView。我还假设您有一个代表您的图像的 Drawable 数组:

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

这可能会根据您的图像在您的应用程序中的可用方式而改变。

【讨论】:

  • 谢谢先生。我使用你的代码取决于我的应用程序。
【解决方案2】:

您可以在 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();

【讨论】:

    【解决方案3】:

    假设您将图像的 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多