I have a listview with a custom adapter.Each row of the listview contains two textviews and a viewflipper.
When I click a button, I want to animate/flip each row with a random text. For animation, I used view flipper,but the animation is not working one after the other , rather it works simultaneously for all the rows.
My button click code is as follows.
public void onButtonClick(View view) {
customAdapter.setFlipRow(!(customAdapter.isFlipRow()));
listView.invalidateViews();
}
The custom adapter:
public class CustomAdapter extends BaseAdapter {
Context context;
String countryList[];
boolean flag[]= null;
int flags[];
LayoutInflater inflter;
Random r = null;
private static String TAG = "CustomAdapter";
public ViewHolder getViewHolder() {
return viewHolder;
}
ViewHolder viewHolder;
public boolean isFlipRow() {
return flipRow;
}
public void setFlipRow(boolean flipRow) {
this.flipRow = flipRow;
}
private boolean flipRow = false;
public CustomAdapter(Context applicationContext, String[] countryList) {
this.context = context;
this.countryList = countryList;
flag = new boolean[countryList.length];
for(int f = 0;f<flag.length;f++)
{
flag[f] = false;
}
inflter = (LayoutInflater.from(applicationContext));
r = new Random(5);
}
private static class ViewHolder {
TextView country;
TextView country1;
ViewFlipper viewFlipper;
ListView lv;
}
@Override
public View getView(int pos, View view, ViewGroup viewGroup) {
View v = view;
if(view == null)
{
viewHolder = new ViewHolder();
view = inflter.inflate(R.layout.listadapter, null);
viewHolder.country = (TextView) view.findViewById(R.id.textView);
viewHolder.country1 = (TextView) view.findViewById(R.id.bottomtextView);
viewHolder.viewFlipper = (ViewFlipper) view.findViewById(R.id.flipper);
viewHolder.lv = (ListView)view.findViewById(R.id.listView);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.country.setText(countryList[pos]);
int rInt = r.nextInt(5);
if(isFlipRow()){
viewHolder.country1.setText("Random No: "+rInt);
viewHolder.viewFlipper.setFlipInterval(1000);
if(pos <= countryList.length-1)
{
flag[pos] = true;
if(pos == 0 && flag[pos]){
viewHolder.viewFlipper.startFlipping();
}
if(pos != 0 && flag[pos-1] == true){
viewHolder.viewFlipper.startFlipping();
flag[pos-1] = false;
/*viewHolder.viewFlipper.stopFlipping();
I tried to have a boolean flag for each row,
setting it before startFlipping() and resetting it after
stopFlipping().But adding this stopFlipping() here
stops all rows except first row and all rows flip at the
same time.Moreover getView is called only during setAdapter()
and invalidateviews().*/
}
if(pos == countryList.length-1){
Arrays.fill(flag,false);
}
}
}
else{
viewHolder.viewFlipper.stopFlipping();
Arrays.fill(flag,false);
}
return view;
}
}
All I need is to animate each row one at a time.But now all the rows are animating at the same time.Any insights or inputs will be really helpful.
Try this:
public void onButtonClick(View view) {
customAdapter.flipRow(view);
}
and adapter:
public class CustomAdapter extends BaseAdapter {
Context context;
String countryList[];
int rnds[];
LayoutInflater inflater;
Random r;
private boolean flipRow = false;
private int flipRowNum = 0;
Button btn;
final private static String TAG = "CustomAdapter";
final private static int FLIP_DELAY = 1000;
final private static int FLIP_COUNT = 2;
public boolean isFlipRow() {
return flipRow;
}
public void flipRow(View view) {
btn = (Button)view;
btn.setEnabled(false);
flipRow = true;
notifyDataSetChanged();
}
public CustomAdapter(Context context, String[] countryList) {
this.context = context;
this.countryList = countryList;
inflater = (LayoutInflater.from(context));
r = new Random(5);
rnds = new int[countryList.length];
for(int f = 0;f<rnds.length; f++)
{
rnds[f] = r.nextInt(5);
}
}
private class ViewHolder {
TextView country;
TextView country1;
ViewFlipper viewFlipper;
ListView lv;
}
@Override
public int getCount() {
return countryList.length;
}
@Override
public String getItem(int i) {
return countryList[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int pos, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;
if(view == null)
{
viewHolder = new ViewHolder();
view = inflater.inflate(R.layout.listadapter, null);
viewHolder.country = (TextView) view.findViewById(R.id.textView);
viewHolder.country1 = (TextView) view.findViewById(R.id.bottomtextView);
viewHolder.viewFlipper = (ViewFlipper) view.findViewById(R.id.flipper);
//viewHolder.lv = (ListView)view.findViewById(R.id.listView);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.country.setText(countryList[pos]);
viewHolder.country1.setText("Random No: " + rnds[pos]);
viewHolder.viewFlipper.setFlipInterval(0);
if(isFlipRow() && (pos == flipRowNum)){
viewHolder.viewFlipper.startFlipping();
viewHolder.viewFlipper.setFlipInterval(FLIP_DELAY);
viewHolder.viewFlipper.postDelayed(new Runnable() { //Stop flipping
@Override
public void run() {
viewHolder.viewFlipper.stopFlipping();
flipRow = !flipRow;
flipRowNum++;
if(flipRowNum == countryList.length) flipRowNum = 0;
btn.setEnabled(true);
}
}, (2*FLIP_COUNT - 1)*FLIP_DELAY + 50);
}
return view;
}
}
Hope that helps!