Android - 动态更改自定义列表视图的项目,如幻灯片效果

时间:2013-06-12 12:25:52

标签: android multithreading listview slideshow custom-lists

我有一个自定义列表视图,其中使用了四个项目,一个imageview和三个textview。我为列表的每个图像视图都有五个图像uri,我希望在特定的imageview项目的某个持续时间内动态更改图像。我应用线程动态更改图像,但它一次只处理一个列表视图项。我想动态更改listview的每个imageview项。我所做的。我是朝着正确的方向前进,还是我应该尝试其他任何事情。请任何人建议我。帮助将非常感谢。谢谢你提前:)

这是我的getView代码 -

final AQuery recycle = aq.recycle(view);

final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {

           animate_slide(recycle , currentimageindex);
           currentimageindex++;

        if(currentimageindex > 4){
            currentimageindex = 0;
                    }
            }
        };
        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {

                   mHandler.post(mUpdateResults);

                  }

            }, delay, period);

和我调用动画的方法 -

private void animate_slide(AQuery recycle ,int ci) {

                            if(ci == 0){
                                Log.d("00000000000000","----------"+ci);
                                recycle.id(R.id.avatar).progress(R.id.progress).image(ApplicationConstants.IMG_URL_SUFFIX+picture.get(currentimageindex), true, true, 100, 0, null, AQuery.FADE_IN);
                            }else if(ci == 1){
                                Log.d("11111111111111","----------"+ci);
                                recycle.id(R.id.avatar).progress(R.id.progress).image(ApplicationConstants.IMG_URL_SUFFIX+picture1.get(currentimageindex), true, true, 100, 0, null, AQuery.FADE_IN);
                            }else if(ci == 2){
                                Log.d("2222222222222222","----------"+ci);
                                recycle.id(R.id.avatar).progress(R.id.progress).image(ApplicationConstants.IMG_URL_SUFFIX+picture2.get(currentimageindex), true, true, 100, 0, null, AQuery.FADE_IN);
                            }else if(ci == 3){
                                Log.d("3333333333333333","----------"+ci);
                                recycle.id(R.id.avatar).progress(R.id.progress).image(ApplicationConstants.IMG_URL_SUFFIX+picture3.get(currentimageindex), true, true, 100, 0, null, AQuery.FADE_IN);
                            }else if(ci == 4){
                                Log.d("00000000000000","----------"+ci);
                                recycle.id(R.id.avatar).progress(R.id.progress).image(ApplicationConstants.IMG_URL_SUFFIX+picture4.get(currentimageindex), true, true, 100, 0, null, AQuery.FADE_IN);

                            }

                        }

2 个答案:

答案 0 :(得分:0)

您可以在getView xml文件中使用Gallary 你可以在那里做一些工作

答案 1 :(得分:0)

你不需要那么复杂。我认为只有列表中的第一项动画,我是对的吗?

我建议不要使用新的runnable,并像这样更改getView()(我假设代码中的currentimageindexListView中项目的位置:

// The view holder for better ListView
private class ViewHolder {
    TextView text1, text2, text3;
    ImageView image;
}

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

    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        /* Initialize views and add them to  the ViewHolder
         * 
         * ....
         * 
        */
        // Setting the position as a tag, so we can get it in the runnable
        holder.image.setTag(position);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                animate_slide(aq.recycle(holder.image), (int) holder.image.getTag());
            }

        }, when, period);
        view.setTag(holder);
    } else holder = (ViewHolder) view.getTag();

    /* do what you want with the texts...
     * 
     * ....
     * 
    */  

    return view;

}

编辑:我找到了一个更简单的解决方案。 :) 我们不需要任何其他线程。 在getView()内放置此代码,它将自动触发动画:

    // At first we load the animation 
    Animation slideAnimation = AnimationUtils.loadAnimation(context , R.anim.fade_in);
    slideAnimation.setRepeatMode(Animation.INFINITE);

    slideAnimation.setAnimationListener(new AnimationListener(){

        @Override
        public void onAnimationStart(Animation arg0) {/* Nothing*/}

        @Override
        public void onAnimationEnd(Animation animation) {
            yourImageView.setImageResource(R.drawable.newResource);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // Setting a delay after each animation.
            // This is the time between two animations.
            animation.setStartOffset(period);
        }

    });

    yourImageView.startAnimation(slideAnimation);

最后,不要忘记取消动画,因为我们说slideAnimation.setRepeatMode(Animation.INFINITE);。我建议在你的适配器类中添加这个方法:

public void onDestroy() {
    slideAnimation.cancel();
}

这在您的活动中:

    @Override
    public void onDestroy() {
        super.onDestroy();
        yourListAdapter.onDestroy();
    }

希望有所帮助:)

相关问题