我正在尝试在已存在的RecyclerView
中实现粘贴标头,其中包含标头,但不是粘性标头。 SuperSLiM看起来很有希望,但我无法将库集成到现有代码中。随后的源代码示例运行得很好,我已尝试适应这一点,并遵循SuperSLiM wiki上的步骤。
我的RecyclerView
最初是空的。我遇到的问题是当我第一次向我的适配器添加项目并在这些项目上调用Adapter.notifyItemChanged()
时。在我这样做之后,我被抛入LayoutManager.onItemsUpdated()
。该方法如下所示:
public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount)
{
super.onItemsUpdated(recyclerView, positionStart, itemCount);
View first = this.getChildAt(0); // null
View last = this.getChildAt(this.getChildCount() - 1); // null
if(positionStart + itemCount > this.getPosition(first)) // NullPointerException
{
if(positionStart <= this.getPosition(last))
{
this.requestLayout();
}
}
}
此处,first
和last
都变为null
,因此会在下面的行中抛出NullPointerException
。请注意,示例应用中未调用onItemsUpdated()
,因此此错误不会发生。我不知道为什么;在示例应用中也会调用Adapter.notifyItemChanged()
。
可能是什么问题?
我的片段类:
请注意,在下面的代码中切换回正常RecyclerView.LayoutManager
会使一切正常工作。
public class MyFragment extends Fragment
{
private MyRecyclerView mRecyclerView;
private MyAdapter mAdapter;
// private RecyclerView.LayoutManager mLayoutManager; // Normal RecyclerView.LayoutManager working just fine
private LayoutManager mLayoutManager; // SuperSLiM LayoutManager crashing
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.my_fragment, container, false);
MyActivity myActivity = (MyActivity) rootView.getContext();
mRecyclerView = (MyRecyclerView) rootView.findViewById(R.id.my_recyclerview);
// mLayoutManager = new LinearLayoutManager(myActivity); // Normal RecyclerView.LayoutManager
mLayoutManager = new LayoutManager(myActivity); // SuperSLiM LayoutManager
mAdapter = new MyAdapter(myActivity);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
myActivity.onShoppingListCreated();
return rootView;
}
public void onDataSetChanged()
{
if (mAdapter == null)
{
return;
}
mAdapter.notifyDataSetChanged();
}
// This is called multiple times after a button press at some point.
// Thereafter, if it is the SuperSLiM LayoutManager that is in play,
// the error explained above occurs.
public void onItemChanged(int position)
{
if (mAdapter == null)
{
return;
}
mAdapter.notifyItemChanged(position);
}
}
崩溃时的堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at android.support.v7.widget.RecyclerView$LayoutManager.getPosition(RecyclerView.java:6097)
at com.tonicartos.superslim.LayoutManager.onItemsUpdated(LayoutManager.java:542)
at android.support.v7.widget.RecyclerView$5.dispatchUpdate(RecyclerView.java:665)
at android.support.v7.widget.RecyclerView$5.onDispatchSecondPass(RecyclerView.java:675)
at android.support.v7.widget.AdapterHelper.consumeUpdatesInOnePass(AdapterHelper.java:547)
at android.support.v7.widget.RecyclerView.processAdapterUpdatesAndSetAnimationFlags(RecyclerView.java:2530)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2318)
at android.view.View.measure(View.java:17547)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1488)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17547)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventRe
答案 0 :(得分:0)
这些天我不得不做类似的事情,我使用了这个库:https://github.com/timehop/sticky-headers-recyclerview
如果您习惯使用RecyclerViews适配器,那将非常容易。
答案 1 :(得分:0)
将此行添加到 onBindViewHolder 方法的末尾:
//configuring sticky headers
final LayoutManager.LayoutParams lp = LayoutManager.LayoutParams.from(holder.itemView.getLayoutParams());
lp.setSlm(LinearSLM.ID);
lp.setFirstPosition(getHeaderPosition(position));
holder.itemView.setLayoutParams(lp);
getHeaderPosition 方法返回每个项目的标题位置