AndroidViewflipper in Action

1 min read

Android View Flipper animates between two or more views that have been added to it. On request by the app it can flip between each child and shows only one child at a time.

Lets have a look at below pseudo code

mFlipper = (AdapterViewFlipper) inflater.inflate(R.layout.adapterviewfilpper, container, false);
mFlipper.setAutoStart(true);
mFlipper.setFlipInterval (3000);

setFlipInterval Method will be used to flip mFlipper with another child view after x seconds automatically.
setAutoStart , setFlipInterval methods can be called as per need of application developer. Many application developers may like to perform flipping after right / left swipe motion.

Just Like other Multiview containers like ListView , AdapterViewFlipper also takes a view adapter.

public class MyAdapterViewFlipper extends BaseAdapter{
private List mList;
private Context mContext;
public MyAdapterViewFlipper(List mList,Context context) {
this.mList = mList;
this.mContext = context;
}
@Override
public int getCount() {
return this.mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
holder.imgView = imageView;
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.imgView.setImageBitmap(mList.get(position));
Animation animation = null;
animation = AnimationUtils.loadAnimation(mContext,getAnimationFile(position));
holder.imgView.setAnimation(animation);
return (View)imageView;
}
static class ViewHolder {
ImageView imgView;
}
int getAnimationFile(int position) {
int result=-1;
switch(position) {
case 0:
result = R.anim.from_left;
default:
result = R.anim.fade_in;
}
return result;
}

Now at last but not the least override getView() method and start flipping child views.


Note : Dear Copy-Paste Community :).
Hope you are doing good.
If you are a software engineer study the material before copy pasting.

Else knowledge gets increased when it is shared.