【问题标题】:Unable to hide toolbar when recyclerview is scrolled using Coordinator Layout使用 Coordinator Layout 滚动 recyclerview 时无法隐藏工具栏
【发布时间】:2017-02-20 05:55:33
【问题描述】:

我在 Fragment 中使用以下代码。当我滚动 appbarlayout 时,工具栏会隐藏,但是当我滚动 recyclerview 时它不会隐藏。我在这里做错了什么?

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/tabanim_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include
        android:id="@+id/toolbars"
        layout="@layout/custom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways" />

</android.support.design.widget.AppBarLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/lv_nearby"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/floatbutton_nearby"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"
    android:scaleType="center"
    android:src="@drawable/filter" />


</android.support.design.widget.CoordinatorLayout>

custom_toolbar 布局代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways">

<GridView
    android:id="@+id/grid_nearby"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="4"
    android:verticalSpacing="10dp">

</GridView>

</android.support.v7.widget.Toolbar>

【问题讨论】:

  • 尝试不使用“”可能会起作用。
  • @aksacha 试过了,不行
  • 你的 targetSdkVersion 是什么?
  • 我的 targetSdkVersion 是 19
  • 更新 targetSdk 为 23 然后检查。

标签: android android-recyclerview android-coordinatorlayout


【解决方案1】:

我已实施且工作正常,请检查: 我们的xml:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

  xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="AppBarLayoutSnapBehavior">


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                app:layout_scrollFlags="scroll|enterAlways"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:focusableInTouchMode="true">

            </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/message_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:focusableInTouchMode="false"
        />

</android.support.design.widget.CoordinatorLayout>

和 ReclerView 的项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />

然后是 AppbarLayout 中使用的 AppBarLayoutSnapBehavior 类:

public class AppBarLayoutSnapBehavior extends AppBarLayout.Behavior {

private ValueAnimator mAnimator;
private boolean mNestedScrollStarted = false;

public AppBarLayoutSnapBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
                                   View directTargetChild, View target, int nestedScrollAxes) {
    mNestedScrollStarted = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    if (mNestedScrollStarted && mAnimator != null) {
        mAnimator.cancel();
    }
    return mNestedScrollStarted;
}

@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target) {
    super.onStopNestedScroll(coordinatorLayout, child, target);

    if (!mNestedScrollStarted) {
        return;
    }

    mNestedScrollStarted = false;

    int scrollRange = child.getTotalScrollRange();
    int topOffset = getTopAndBottomOffset();

    if (topOffset <= -scrollRange || topOffset >= 0) {
        // Already fully visible or fully invisible
        return;
    }

    if (topOffset < -(scrollRange / 2f)) {
        // Snap up (to fully invisible)
        animateOffsetTo(-scrollRange);
    } else {
        // Snap down (to fully visible)
        animateOffsetTo(0);
    }
}

private void animateOffsetTo(int offset) {
    if (mAnimator == null) {
        mAnimator = new ValueAnimator();
        mAnimator.setInterpolator(new DecelerateInterpolator());
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setTopAndBottomOffset((int) animation.getAnimatedValue());
            }
        });
    } else {
        mAnimator.cancel();
    }

    mAnimator.setIntValues(getTopAndBottomOffset(), offset);
    mAnimator.start();
}

还有我们的带有适配器的 Activity 类:

public class Second extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.main);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.message_list_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    MyRecyclerViewAdapter recyclerAdapter = new MyRecyclerViewAdapter(createItemList(), this);
    recyclerView.setAdapter(recyclerAdapter);
}


private ArrayList<String> createItemList() {
    ArrayList<String> list = new ArrayList();
    for(int i = 0; i < 200; i++) {
        list.add(new String("List Item " + i));
    }
    return list;
}
@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
}

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter<MyRecyclerViewAdapter
        .DataObjectHolder> {
    private  String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList<String> mDataset;

    public  MyRecyclerViewAdapter(ArrayList<String>list, Context context)
    {
        this.mDataset=list;
        notifyDataSetChanged();
    }



    public  class DataObjectHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView label;
        TextView dateTime;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
        }

        @Override
        public void onClick(View v) {
        }
    }



    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_item, parent, false);

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.label.setText(mDataset.get(position));
    }


    @Override
    public int getItemCount() {
        return mDataset.size();
    }

 }

} 试试这个可能会有帮助。

【讨论】:

    【解决方案2】:

    您需要将RecycleView 放在NestedScrollView 中,并将布局行为设置为NestedScrollView 在单独的布局中,并像这样包含在主布局中

    content_scrolling.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <android.support.v7.widget.RecyclerView
        android:id="@+id/lv_nearby"
        android:clipToPadding="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    </android.support.v4.widget.NestedScrollView>
    

    你的布局

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/tabanim_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <include
            android:id="@+id/toolbars"
            layout="@layout/custom_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways" />
    
    </android.support.design.widget.AppBarLayout>
    
       //Include your scroll layout here
       <include layout="@layout/content_scrolling"/>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatbutton_nearby"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        android:scaleType="center"
        android:src="@drawable/filter" />
    
    
    </android.support.design.widget.CoordinatorLayout>
    

    之后将RecycleView 的嵌套滚动设置为 false,如下所示:

    yourrecyclerView.setNestedScrollingEnabled(false);
    

    【讨论】:

      【解决方案3】:

      将 FitSystemWindows 添加到 Coordinator Layout 和 AppbarLayout:

      android:fitsSystemWindows="true"
      

      希望对你有帮助:

      【讨论】:

      • 从工具栏中删除此属性:app:layout_scrollFlags="scroll|enterAlways"
      • 滚动工作需要。反正我试过了,还是不行。
      • 这也不行。奇怪的是,当我滚动工具栏时,它可以正常工作,但是当我滚动 recyclerview 时它不起作用
      【解决方案4】:

      你说得对,滚动RecyclerView时不会隐藏Toolbar

      我已经测试了代码,现在,通过删除 Include,将 app:layout_scrollFlags="scroll|enterAlways" 添加到 ToolbarAppBarLayout,它正在工作,此外,还有一些修复。

      代码如下:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true">
      
          <android.support.design.widget.AppBarLayout
              android:id="@+id/tabanim_appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_scrollFlags="scroll|enterAlways">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:minHeight="?attr/actionBarSize"
                  app:layout_scrollFlags="scroll|enterAlways">
      
                  <GridView
                      android:id="@+id/grid_nearby"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginBottom="10dp"
                      android:gravity="center"
                      android:horizontalSpacing="10dp"
                      android:numColumns="4"
                      android:verticalSpacing="10dp" />
      
              </android.support.v7.widget.Toolbar>
      
          </android.support.design.widget.AppBarLayout>
      
          <android.support.v7.widget.RecyclerView
              android:id="@+id/lv_nearby"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" />
      
          <android.support.design.widget.FloatingActionButton
              android:id="@+id/floatbutton_nearby"
              android:layout_width="48dp"
              android:layout_height="48dp"
              android:layout_gravity="bottom|right"
              android:layout_marginBottom="8dp"
              android:layout_marginRight="8dp"
              android:scaleType="center"
              android:src="@mipmap/ic_launcher" />
      
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        【解决方案5】:

        您的代码很好。只需将您的 gradle 构建版本升级到更新的版本即可正常工作。我已经在

        中测试过这段代码
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        

        目标版本targetSdkVersion 25

        并设计 lib compile 'com.android.support:design:25.2.0' 及其正常工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-17
          • 1970-01-01
          • 2016-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多