自定义行中的Animate(闪烁)ListView(Animate(Blink) ListView from Custom Rows)

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

请建议如何在自定义方法中访问BaseAdapter外部的listview项目

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

}

I am trying to blink listView Custom rows although I am looking for Fade in Fade out animation for listview rows, but in below code I am just changing image after a second and having problem in setting up and accessing background outside listView getViews. I am using Activity and listView is a component in that activity.

Please advice how to access items of listview outside my BaseAdapter in custom method

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

}

最满意答案

我很欣赏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; }

I appreciate pskink comments but Method for taking action on listView need to be called after a minor delay until an unless listView dynamic row addition with animation is completed

Therefore I create a seperate Thread runnable and called my method in it

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

更多推荐