我在RecyclerView
上向上滚动时显示一个转到顶部的按钮。如果用户向上滚动,则显示按钮。
这是嵌套在RelativeLayout
中的xml代码。
<android.support.v7.widget.RecyclerView
android:id="@+id/userFeedsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_goto_top"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="@string/icon_up_arrow"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:visibility="gone"
android:textSize="10sp"
android:layout_centerHorizontal="true"
android:background="@drawable/round_button"/>
这是java代码
btnGotoTop = (Button) findViewById(R.id.btn_goto_top);
Const.setTypeFace(btnGotoTop, getApplication());
btnGotoTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userFeedsRecyclerView.smoothScrollToPosition(0);
view.setVisibility(View.GONE);
}
});
userFeedsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy < 0){
btnGotoTop.setVisibility(View.VISIBLE);
}
else{
btnGotoTop.setVisibility(View.GONE);
}
}
});
在onClick()
的{{1}}方法中,btnGotoTop
正确滚动到顶部,但按钮不会消失。
我尝试将RecyclerView
更改为view.setVisibility(View.GONE)
,但即使滚动完成后也会显示该按钮。
答案 0 :(得分:0)
你的病情有误。您在滚动时可以看到按钮,但只想在向下滚动时显示。 改变这个
if(dy < 0){
btnGotoTop.setVisibility(View.VISIBLE);
}
else{
btnGotoTop.setVisibility(View.GONE);
}
到
if(dy > 0){
btnGotoTop.setVisibility(View.VISIBLE);
}
else{
btnGotoTop.setVisibility(View.GONE);
}
答案 1 :(得分:0)
试试这个,我还建议你在这里使用浮动动作按钮。我创建了一个简单的项目,以防你需要查看带有 FloatingActionButton 的recylerView,并且需要相同的功能。 Link.
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0 && btnGotoTop.getVisibility() == View.VISIBLE)
{
btnGotoTop.hide();
} else if (dy < 0 && btnGotoTop.getVisibility() != View.VISIBLE)
{
btnGotoTop.show();
}
}
});
答案 2 :(得分:0)
DataFrame.stack()
使用 private boolean IsRecyclerViewAtTop(){
if (userFeedsRecyclerView.getChildCount() == 0)
return true;
return userFeedsRecyclerView.getChildAt(0).getTop() == 0;
}
userFeedsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (IsRecyclerViewAtTop())
{
btnGotoTop.setVisibility(View.GONE);
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy < 0 && btnGotoTop.getVisibility()==View.GONE) {
btnGotoTop.setVisibility(View.VISIBLE);
} else if (dy > 0 && btnGotoTop.getVisibility()==View.VISIBLE){
btnGotoTop.setVisibility(View.GONE);
}
}
});
方法查看回收站视图是否滚动到顶部并调用它是IsRecyclerViewAtTop
方法。
答案 3 :(得分:0)
以下是我如何解决它。在RecyclerView的onScrollListener的onScrollStateChanged()中更改了按钮的可见性
btnGotoTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
userFeedsRecyclerView.smoothScrollToPosition(0);
view.setVisibility(View.GONE);
headerAppBarLayout.setExpanded(true);
}
});
private boolean isUserFeedRecyclerViewAtTop(){
int numColumns = 2;
int pos[] = new int[numColumns];
((StaggeredGridLayoutManager)(userFeedsRecyclerView.getLayoutManager())).findFirstCompletelyVisibleItemPositions(pos);
return pos[0] == 0;
}
userFeedsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState){
case RecyclerView.SCROLL_STATE_IDLE:
if(isUserFeedRecyclerViewAtTop()) {
btnGotoTop.setVisibility(View.GONE);
}
break;
case RecyclerView.SCROLL_STATE_DRAGGING:
break;
case RecyclerView.SCROLL_STATE_SETTLING:
break;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy < 0 && btnGotoTop.getVisibility() == View.GONE){
btnGotoTop.setVisibility(View.VISIBLE);
}
else if (dy > 0 && btnGotoTop.getVisibility() == View.VISIBLE){
btnGotoTop.setVisibility(View.GONE);
}
}
});