package
com.mundane.gridanimationdemo;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.view.animation.Animation;
import
android.view.animation.TranslateAnimation;
import
android.widget.BaseAdapter;
import
android.widget.TextView;
import
java.util.List;
/**
* Created by Jackie on 2017/3/7 16:29
*/
public
class
GridAdapter
extends
BaseAdapter{
private
List<String> mList;
public
GridAdapter(List<String> list) {
mList = list;
}
@Override
public
int
getCount() {
return
mList.size();
}
@Override
public
Object getItem(
int
position) {
return
mList.get(position);
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
@Override
public
View getView(
final
int
position, View convertView, ViewGroup parent) {
String text = mList.get(position);
ViewHolder holder;
if
(convertView ==
null
) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_desk_grid_item, parent,
false
);
holder =
new
ViewHolder(convertView);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
convertView.setVisibility(View.INVISIBLE);
holder.textView.setText(text);
int
count =
3
- position %
3
;
final
TranslateAnimation translateAnimation =
new
TranslateAnimation(
Animation.RELATIVE_TO_SELF,
count,
Animation.RELATIVE_TO_SELF,
0
,
Animation.RELATIVE_TO_SELF,
0
,
Animation.RELATIVE_TO_SELF,
0
);
translateAnimation.setDuration(count*
100
);
final
View finalConvertView = convertView;
convertView.postDelayed(
new
Runnable() {
@Override
public
void
run() {
finalConvertView.startAnimation(translateAnimation);
}
}, position *
200
);
translateAnimation.setAnimationListener(
new
Animation.AnimationListener() {
@Override
public
void
onAnimationStart(Animation animation) {
finalConvertView.setVisibility(View.VISIBLE);
}
@Override
public
void
onAnimationEnd(Animation animation) {
if
(position == mList.size() -
1
) {
if
(mListener !=
null
) {
mListener.onAnimationEnd();
}
}
}
@Override
public
void
onAnimationRepeat(Animation animation) {
}
});
return
convertView;
}
static
class
ViewHolder {
TextView textView;
public
ViewHolder(View view) {
textView = (TextView) view.findViewById(R.id.tv);
}
}
public
interface
OnLastItemAnimationEndListener {
void
onAnimationEnd();
}
private
OnLastItemAnimationEndListener mListener;
public
void
setOnLastItemAnimationEndListener(OnLastItemAnimationEndListener listener) {
mListener = listener;
}
}