我尝试实现一个动画,用于隐藏位于listview上方的RelativeLayout。我希望它具有以下行为:
- 向下滚动:隐藏RelativeLayout
- 向上滚动:只有当我在顶部时才显示RelativeLayout
我创建的以下代码有效,但存在两个问题:
- 当我向下或向上滚动时,如果我在listview的顶部,relativelayout会闪烁。我必须快速滚动才能隐藏这次攀登。 => 我的代码存在问题,也许在listIsAtTop()函数上?
- 第二个问题是,我没有动画。我使用setVisivility(VISIBLE or GONE)来显示或隐藏relativeLayout。这不够用户友好,对吗?:) 我不知道如何添加一个小平移动画来隐藏和显示这个相对布局。
在Account_Activity类的onCreate()中:
list = (com.app.frisbeee.account_view.CustomListView)findViewById(R.id.accountview_listview);
adapter = new Account_view_custom_adapter(Account_view_act.this, annoncesList);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
registerForContextMenu(list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
list.showContextMenuForChild(view);
}
});
list.setOnDetectScrollListener(new OnDetectScrollListener() {
@Override
public void onUpScrolling() {
/* do something */
Log.d("INFO", "UPPPPPPPPPPP");
if (listIsAtTop()) {
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
relative1.setVisibility(View.VISIBLE);
}
}
@Override
public void onDownScrolling() {
/* do something */
Log.d("INFO", "DOWNNNNNNNN");
if (!listIsAtTop()) {
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
relative1.setVisibility(View.GONE);
}
}
});
}
listIsAtTop函数的代码:
private boolean listIsAtTop() {
if (list.getChildCount() == 0) {
return true;
}
return list.getFirstVisiblePosition() == 0 && (list.getChildCount() == 0 || list.getChildAt(0).getTop() == 0);
}
CustomListView 用于检测滚动 UP 和 DOWN:
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
public class CustomListView extends android.widget.ListView {
private OnScrollListener onScrollListener;
private OnDetectScrollListener onDetectScrollListener;
public CustomListView(Context context) {
super(context);
onCreate(context, null, null);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
onCreate(context, attrs, null);
}
public CustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
onCreate(context, attrs, defStyle);
}
private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
setListeners();
}
private void setListeners() {
super.setOnScrollListener(new OnScrollListener() {
private int oldTop;
private int oldFirstVisibleItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (onScrollListener != null) {
onScrollListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (onScrollListener != null) {
onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
if (onDetectScrollListener != null) {
onDetectedListScroll(view, firstVisibleItem);
}
}
private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem) {
View view = absListView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop();
if (firstVisibleItem == oldFirstVisibleItem) {
if (top > oldTop) {
onDetectScrollListener.onUpScrolling();
} else if (top < oldTop) {
onDetectScrollListener.onDownScrolling();
}
} else {
if (firstVisibleItem < oldFirstVisibleItem) {
onDetectScrollListener.onUpScrolling();
} else {
onDetectScrollListener.onDownScrolling();
}
}
oldTop = top;
oldFirstVisibleItem = firstVisibleItem;
}
});
}
@Override
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
public void setOnDetectScrollListener(OnDetectScrollListener onDetectScrollListener) {
this.onDetectScrollListener = onDetectScrollListener;
}
}