当用户滚动屏幕时,我想显示并隐藏Android ActionBar。我找到了一些例子,例如this问题。但是代码显示在这个问题中,它的答案使得动作隐藏在列表滚动了一些像素之后,我想让这个滚动像Google的收件箱应用程序,即操作栏由布局根据用户拉动向上或向下滚动屏幕,换句话说,我想在用户向下/向上滚动的同时显示/隐藏操作栏。
有人面临这样的问题吗?有些想法?
答案 0 :(得分:3)
超级简单:
ListView
。 ListView已成为过去。请改用RecyclerView
。RecyclerView.OnScrollListener
,以逐个像素滚动。Toolbar
。所以你可以控制它的位置。setTranslationY(val)
上致电Toolbar
,使用RecyclerView
“滚动”它。与上述课程的文档有几个链接:
答案 1 :(得分:0)
我创建了基于@Budius和link
的解决方案首先添加此类
$http.put(apiBase + 'delete', null, {
params: {
id: orderId
}
}
);
使用简单,只使用recyclerView的addOnScrollListener
添加public class HidingRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
private final int UP = 0;
private final int DOWN = 1;
private final int margin = 5;
private View mView;
RecyclerView.LayoutManager mLayoutManager;
private float currentPoint = 0;
private int lastDirection = 1;
public HidingRecyclerViewScrollListener(View view, LinearLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
mView = view;
}
public HidingRecyclerViewScrollListener(View view, GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
mView = view;
}
public HidingRecyclerViewScrollListener(View view, StaggeredGridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
mView = view;
}
public int getFirstVisibleItem(int[] firstVisibleItemPositions) {
int minSize = 0;
for (int i = 0; i < firstVisibleItemPositions.length; i++) {
if (i == 0) {
minSize = firstVisibleItemPositions[i];
} else if (firstVisibleItemPositions[i] < minSize) {
minSize = firstVisibleItemPositions[i];
}
}
return minSize;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
float nextPoint = currentPoint + dy;
if (nextPoint < 0) {
nextPoint = 0;
}
if (nextPoint > viewSize()) {
nextPoint = viewSize();
}
lastDirection = nextPoint >= currentPoint ? UP : DOWN;
currentPoint = nextPoint;
mView.setTranslationY(-currentPoint);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (lastDirection == UP) {
if (getFirstVisibleItem() > 0) {
currentPoint = viewSize();
}
}
if (lastDirection == DOWN) {
//Volta para origem
currentPoint = 0;
}
mView.animate().translationY(-currentPoint);
}
}
private int getFirstVisibleItem() {
int firstVisibleItem = 0;
if (mLayoutManager instanceof StaggeredGridLayoutManager) {
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findFirstVisibleItemPositions(null);
firstVisibleItem = getFirstVisibleItem(lastVisibleItemPositions);
} else if (mLayoutManager instanceof LinearLayoutManager) {
firstVisibleItem = ((LinearLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
} else if (mLayoutManager instanceof GridLayoutManager) {
firstVisibleItem = ((GridLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
}
return firstVisibleItem;
}
private int viewSize() {
return mView.getHeight() + margin;
}
}
mView是您要控制的视图
您可以使用LinearLayoutManager,GridLayoutManager和StaggeredGridLayoutManager来使用此类(但我从未使用GridLayoutManager进行测试)
在RecyclerView中使用下面的代码列表不会停留在你想要控制的对象之下,这个代码更简单,更好地测试,但在RecyclerView中使用Hearder就像在上面的链接中说的那样有更好的结果。
recyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView );
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnScrollListener(new HidingRecyclerViewScrollListener(mView, layoutManager));
如果您使用SwipeRefreshLayout,请使用以下代码显示ProgressBar
android:clipToPadding="false"
android:paddingTop="@dimen/outher_view_dimem"