自定义行中的Animate(闪烁)ListView

时间:2014-02-20 14:31:27

标签: android android-layout listview android-listview android-animation

我正在尝试使listView自定义行闪烁,虽然我正在寻找Fade in淡出listview行的动画,但在下面的代码我只是在一秒钟之后更改图像并且在设置和访问listView getViews之外的背景时遇到问题。我正在使用Activity,listView是该活动中的一个组件。

请在自定义方法

中建议如何访问BaseAdapter外部列表视图的项目
public class MyView extends Activity implements  {

       private Timer activeCallBlinker;
        MyListAdapter  mActiveAdapter;

      private class MyListAdapter extends BaseAdapter {

             private Activity activity;
        private ArrayList<HashMap<String, String>> data;
        private LayoutInflater inflater = null;

        ViewHolder viewHolder = null;

        public MyListAdapter(Activity act,
                ArrayList<HashMap<String, String>> dta) {
            activity = act;
            data = dta;
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

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

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub

            View view = null;

            if (convertView == null) {

                view = inflater.inflate(R.layout.my_listitem, null);

                viewHolder = new ViewHolder();

                viewHolder.cellBgImg = (ImageView) view.findViewById(R.id.cellBgImage);   ///  Works fine and setting

                view.setTag(viewHolder);

            } else {
                view = convertView;
            }

            viewHolder = (ViewHolder) view.getTag();

            viewHolder.cellBgImg.setImageResource(R.drawable.stripe_flash);

            return view;
        }

}

public void populateAndBlink() {

        mActiveAdapter = new MyListAdapter(ActiveCallView.this, calledPartyArr);

        AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(mActiveAdapter);
                        animAdapter.setAbsListView(getListView());
                        lvActiveCall.setAdapter(animAdapter);


                    ////  After successful population with animation 

                    I am looking to blink it with Fade in Fade out but I can't find a good way so just changing images of background for  my listadapter items

                    activeCallBlinker = new Timer();
                    startActiveTimer();     

}

public void startActiveTimer (){

        final int wantedPosition = globalRowIndex; 

        final Handler handler = new Handler ();
        activeCallBlinker.scheduleAtFixedRate (new TimerTask (){
            public void run (){

                handler.post (new Runnable (){
                    public void run (){

                        runOnUiThread(new Runnable() // start actions in UI thread
                        {

                            @Override
                            public void run()
                            {
                                View cellView = mActiveAdapter.getView(wantedPosition, null,getListView());
                                //ViewHolder viewHolder = (ViewHolder) cellView.getTag();
                                ViewHolder viewHolder = new ViewHolder();

                                viewHolder.cellBgImg = (ImageView)cellView.findViewById(R.id.cellBgImage);
                                viewHolder.timerLbl = (TextView)cellView.findViewById(R.id.timerLbl);

                                if(viewHolder.timerLbl.getVisibility() == View.VISIBLE)
                                    viewHolder.timerLbl.setVisibility(View.INVISIBLE);      ////  Unable to set InVisibility 
                                else
                                    viewHolder.timerLbl.setVisibility(View.VISIBLE);        ////  Unable to set Visibility 

                                String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.stripe);
                                Log.d("Resource Name", resName);

                                final Drawable d1 = getResources().getDrawable(R.drawable.stripe); 
                                final Drawable d2 = getResources().getDrawable(R.drawable.strip_header);




                                if (viewHolder.cellBgImg.getDrawable().equals(d1)) {        //// Unable to check Drawable image for setting up background only else is called

                                    Log.d("Resource Name", d1.toString());
                                    viewHolder.cellBgImg.setImageResource(R.drawable.stripe_flash);   //// Unable to change image of backkground
                                } else {

                                    Log.d("Resource Name", d2.toString());
                                    viewHolder.cellBgImg.setImageResource(R.drawable.stripe);    //// Unable to change image of backkground
                                }


                            }
                        });
                    }
                });

            }
        }, 0, 1000);
    }

    public ListView getListView() {
        return lvActiveCall;
    }

}

1 个答案:

答案 0 :(得分:0)

我很欣赏pskink注释,但是需要在稍微延迟之后调用对listView采取操作的方法,直到除非listView动态行添加动画完成

因此我创建一个单独的Thread runnable并在其中调用我的方法

worker =  Executors.newSingleThreadScheduledExecutor();

                    Runnable myRunnable = createRunnable(i);

                    worker.schedule(myRunnable, 2, TimeUnit.SECONDS);



    private Runnable createRunnable(final int wantedPosition){



     Runnable aRunnable = new Runnable(){
            public void run(){



           runOnUiThread(new Runnable() // start actions in UI
                // thread
                {
                @Override
                public void run() {

                ApplyActionOnActiveCallsAfterDelay(wantedPosition);

              }
            }
        };

        return aRunnable;

    }