我在右侧有一个带字母索引器的gridview,默认情况下将其可见性设置为view.gone。当我开始滚动网格视图时,我希望该索引器显示动画,当我停止滚动时,索引器也将自动隐藏
gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
mLetter.setVisibility(View.VISIBLE);
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Animation animation = new TranslateAnimation(0, 200, 0, 0);
animation.setDuration(500);
mLetter.startAnimation(animation);
mLetter.setVisibility(View.GONE);
}
}, 5000);
}
}
});
我的问题是:
答案 0 :(得分:2)
在网格视图上使用TouchListener:
boolean isLetterShowing = false;
gridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
if(!isLetterShowing){
isLetterShowing = true;
}
break;
case MotionEvent.ACTION_SCROLL:
if(isLetterShowing){
mLetter.setVisibility(View.VISIBLE);
}
break;
case MotionEvent.ACTION_UP:
if(isLetterShowing){
isLetterShowing = false;
mLetter.setVisibility(View.INVISIBLE);
}
break;
}
return false;
}
});
答案 1 :(得分:1)
下班后我终于找到了自己的答案,实际上非常简单
gridview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
//when first touch
case MotionEvent.ACTION_DOWN:
if(mLetter.getVisibility() == View.GONE){ //make sure indexer doesn't exist
Animation animation = new TranslateAnimation(100, 0, 0, 0);
animation.setDuration(500);
mLetter.startAnimation(animation);
mLetter.setVisibility(View.VISIBLE);
}
break;
case MotionEvent.ACTION_MOVE:
mLetter.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
gridview.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
state = scrollState;
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (state == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
//make sure indexer is exist AND is not currently touched
if (mLetter.getVisibility() == View.VISIBLE && !mLetter.getBool()) {
Animation animation = new TranslateAnimation(0, 200, 0, 0);
animation.setDuration(500);
mLetter.startAnimation(animation);
mLetter.setVisibility(View.GONE);
}
}
}
}, 5000);
}
}
});
这可以成功阻止动画复制。希望这有帮助的人