ListView的自定义适配器

时间:2013-12-17 16:17:31

标签: android multithreading android-listview android-imageview

我的ListView有多个项目。每个项目都有TextView和两个ImageView。我已经使用自定义适配器实现了这一点。我ImageView中的每个ListView都有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;
    }          
}

3 个答案:

答案 0 :(得分:2)

AnimationDrawable是正确的组成部分:

getView()中,我假设您有一个名为ImageView的{​​{1}}。我还假设你有一个代表你的图像的Drawables数组:

imageView

根据应用程序中图像的可用方式,这可能会发生变化。

答案 1 :(得分: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();

答案 2 :(得分: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();