【发布时间】:2017-02-22 04:05:13
【问题描述】:
我在 NavigationView 中创建了一个 RecyclerView,其中包含一个自定义的第一项,相当于 ListView 的 addHeaderView()。在该标题布局的底部,我放置了一个 EditText,以便我可以过滤掉下面的列表。
我想要做的是将 RecyclerView 一直向上滚动,直到 EditText 成为最顶部的项目,只要 EditText 获得焦点。但是,如果我的项目数量足够小以至于无法填满 NavigationView 的整个高度,则 RecyclerView 拒绝一直向上滚动,从而显示除 EditText 之外的更多标题。另外一个问题是,即使列表足够大,一旦我开始过滤列表项,它仍然会向下滚动。
如何强制我的 RecyclerView 仍然一直向上滚动,即使我没有足够的项目来填充我的视图的高度,这样我的标题的其余部分就不会被偷看?
这是我想要实现的目标的可视化:
_______________
| HEADER LINE 1 |
| HEADER LINE 2 |
| HEADER LINE 3 |
| ------------- |
| Search box |
| ------------- |
| List item 1 |
| List item 2 |
| List item 3 |
---------------
当我专注于搜索框时的预期行为:
_______________
| ------------- |
| Search box | <- becomes the topmost item. I can already achieve this
| ------------- | with RecyclerView.smoothScrollBy(dx, dy)
| List item 1 | except when the list is small enough (see below)
| List item 2 |
| List item 3 |
| List item 4 |
| | <- empty space is okay and is desired if dataset
| | is small enough
---------------
实际发生的情况:
_______________
| HEADER LINE 2 | <- the header is 'bleeding' on top
| HEADER LINE 3 |
| ------------- |
| Search box | <- search box cannot take the topmost spot
| ------------- | as the recycler tries to position itself to fill in
| List item 1 | the bottom part with items
| List item 2 |
| List item 3 |
| List item 4 | <- items are pushed down to the bottom to fill
--------------- in the space
这是我的 NavigationView xml 的 sn-p(只是您的典型设置):
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/drawer_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>
此处带有自定义标头的 ViewHolder(RecyclerView 适配器):
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == HEADER) {
return new HeaderHolder(mHeaderView);
} else {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ItemViewHolder(v);
}
}
抽屉标题只是我在主要活动中膨胀的一个 xml 布局(我在那里分配了侦听器和动画)并通过我制作的 setHeaderView(View header) 传递给我的 RecyclerView 适配器。
【问题讨论】:
-
试过这个吗?这是不同的效果相同的结果。 stackoverflow.com/questions/26568087/…
-
已经看到了。我的标题中有控件(包括可扩展布局),所以视差效果会毁掉它。更不用说使用库了,因为我已经在我的 RecyclerView 中成功实现了一个自定义标头。问题只是滚动位置
-
尝试将回收站视图的高度更改为
wrap_content。 -
这无济于事,因为我的目标是让 RecyclerView (或其他)填充下面的空白。将其设置为 wrap_content 只会消除任何多余的空格,并且如果我的列表足够小,则会阻止进一步滚动。
标签: android android-layout android-recyclerview