我花了一些时间试图找出一种方法来添加一个标题RecyclerView,但没有成功.这是我到目前为止所得到的:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
layouManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layouManager);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
headerPlaceHolder = inflater.inflate(R.layout.view_header_holder_medium, null, false);
layouManager.addView(headerPlaceHolder, 0);
...
}
Run Code Online (Sandbox Code Playgroud)
在LayoutManager似乎是处理的配置对象RecyclerView的项目.因为我无法找到任何addHeaderView(View view)方法,我决定去与LayoutManager的addView(View view, int position)方法,并增加我的头鉴于第一位置,像一个头.
Aaand这是事情变得更加丑陋的地方:
java.lang.NullPointerException: Attempt to read from field 'android.support.v7.widget.RecyclerView$ViewHolder android.support.v7.widget.RecyclerView$LayoutParams.mViewHolder' on a null object reference
at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:2497)
at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:4807)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:4803)
at com.mathieumaree.showz.fragments.CategoryFragment.setRecyclerView(CategoryFragment.java:231)
at com.mathieumaree.showz.fragments.CategoryFragment.access$200(CategoryFragment.java:47)
at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:201)
at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:196)
at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:41)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135) …Run Code Online (Sandbox Code Playgroud) 我在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 |
---------------
Run Code Online (Sandbox Code Playgroud)
当我专注于搜索框时的预期行为:
_______________
| ------------- |
| Search box | <- becomes the topmost item. I can already achieve this
| ------------- | with RecyclerView.smoothScrollBy(dx, dy)
| List item 1 | except when …Run Code Online (Sandbox Code Playgroud)