我刚刚整理了一个示例应用程序,似乎可以展示您所追求的行为.该AppBar
每当将自动扩展RecyclerView
其下方滚动到顶部,而不是停止并等待另一个刷卡打开AppBar
.
最相关的组件位于自定义RecyclerView
类中.我添加了一些解释性意见:
public class ScrollFeedbackRecyclerView extends RecyclerView { private WeakReferencemCallbacks; public ScrollFeedbackRecyclerView(Context context) { super(context); attachCallbacks(context); } public ScrollFeedbackRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); attachCallbacks(context); } /*If the first completely visible item in the RecyclerView is at index 0, then we're at the top of the list, so we want the AppBar to expand **if the AppBar is also collapsed** (otherwise the AppBar will constantly attempt to expand). */ @Override public void onScrolled(int dx, int dy) { if(((LinearLayoutManager)getLayoutManager()).findFirstCompletelyVisibleItemPosition() == 0) { Log.e(getClass().getSimpleName(), "index 0 visible"); if(mCallbacks.get().isAppBarCollapsed()) { mCallbacks.get().setExpanded(true); } } super.onScrolled(dx, dy); } /* the findFirstCompletelyVisibleItem() method is only available with LinearLayoutManager and its subclasses, so test for it when setting the LayoutManager */ @Override public void setLayoutManager(LayoutManager layout) { if(!(layout instanceof LinearLayoutManager)) { throw new IllegalArgumentException(layout.toString() + " must be of type LinearLayoutManager"); } super.setLayoutManager(layout); } private void attachCallbacks(Context context) { try { mCallbacks = new WeakReference<>((Callbacks)context); } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement " + "ScrollFeedbackRecyclerView.Callbacks"); } } /* Necessary to interact with the AppBarLayout in the hosting Activity */ interface Callbacks { boolean isAppBarCollapsed(); void setExpanded(boolean expanded); } }
MainActivity.java
public class MainActivity extends AppCompatActivity implements ScrollFeedbackRecyclerView.Callbacks{ private AppBarLayout mAppBarLayout; private Toolbar mToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ScrollFeedbackRecyclerView mRecyclerView = (ScrollFeedbackRecyclerView) findViewById(R.id.rv_container); RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mAppBarLayout = (AppBarLayout) findViewById(R.id.app_bar); mToolbar = (Toolbar) findViewById(R.id.toolbar); } /* When collapsed, calling getY() on the AppBar will return a negative number. Adding this number to getHeight() will return the same value as the toolbar's height if the AppBar is fully collapsed. */ @Override public boolean isAppBarCollapsed() { final int appBarVisibleHeight = (int) (mAppBarLayout.getY() + mAppBarLayout.getHeight()); final int toolbarHeight = mToolbar.getHeight(); return (appBarVisibleHeight == toolbarHeight); } @Override public void setExpanded(boolean expanded) { mAppBarLayout.setExpanded(expanded, true); } }