我想创建带有粘性标头的recycleview,我可以使带有标题的recyclerview正常运行,但是当我尝试使它们粘时,我的应用程序崩溃了。我尝试了here中的代码,它在ArrayList<String>
上正常工作,但是当我在ArrayList<ItemObject>
上尝试时,我的应用程序崩溃了。
这是我的日志。
2019-07-13 15:34:53.144 22566-22566/com.elytelabs.stickyrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elytelabs.stickyrecyclerview, PID: 22566
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
at com.elytelabs.stickyrecyclerview.RecyclerSectionItemDecoration.onDrawOver(RecyclerSectionItemDecoration.java:55)
at androidx.recyclerview.widget.RecyclerView.draw(RecyclerView.java:4223)
at android.view.View.updateDisplayListIfDirty(View.java:18168)
at android.view.View.draw(View.java:18946)
at android.view.ViewGroup.drawChild(ViewGroup.java:4240)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024)
at android.view.View.updateDisplayListIfDirty(View.java:18159)
at android.view.View.draw(View.java:18946)
at android.view.ViewGroup.drawChild(ViewGroup.java:4240)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024)
at android.view.View.updateDisplayListIfDirty(View.java:18159)
at android.view.View.draw(View.java:18946)
at android.view.ViewGroup.drawChild(ViewGroup.java:4240)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024)
at android.view.View.updateDisplayListIfDirty(View.java:18159)
at android.view.View.draw(View.java:18946)
at android.view.ViewGroup.drawChild(ViewGroup.java:4240)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024)
at android.view.View.updateDisplayListIfDirty(View.java:18159)
at android.view.View.draw(View.java:18946)
at android.view.ViewGroup.drawChild(ViewGroup.java:4240)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024)
at android.view.View.updateDisplayListIfDirty(View.java:18159)
at android.view.View.draw(View.java:18946)
at android.view.ViewGroup.drawChild(ViewGroup.java:4240)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4024)
at android.view.View.draw(View.java:19221)
at com.android.internal.policy.DecorView.draw(DecorView.java:791)
at android.view.View.updateDisplayListIfDirty(View.java:18168)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:685)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:691)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:799)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3051)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2846)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2399)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1432)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6861)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1026)
at android.view.Choreographer.doCallbacks(Choreographer.java:838)
at android.view.Choreographer.doFrame(Choreographer.java:769)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1012)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
2019-07-13 15:34:53.200 22566-22566/com.elytelabs.stickyrecyclerview I/Process: Sending signal. PID: 22566 SIG: 9
这是代码。
MainActivity
mRecyclerView = findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new Adapter(this, getData());
mRecyclerView.setAdapter(adapter);
RecyclerSectionItemDecoration sectionItemDecoration =
new RecyclerSectionItemDecoration(getResources().getDimensionPixelSize(R.dimen.header),
true,
getSectionCallback(getData()));
mRecyclerView.addItemDecoration(sectionItemDecoration);
}
public ArrayList<ItemObject> getData() {
ArrayList<ItemObject> list = new ArrayList <ItemObject>();
list.add(new ItemObject("Header 1",true));
list.add(new ItemObject("Example Text 1","Something Here 1"));
list.add(new ItemObject("Example Text 2","Something Here 2 "));
list.add(new ItemObject("Example Text 3","Something Here 3"));
list.add(new ItemObject("Header 2",true));
list.add(new ItemObject("Example Text 1","Something Here 1"));
list.add(new ItemObject("Example Text 2","Something Here 2 "));
list.add(new ItemObject("Example Text 3","Something Here 3"));
list.add(new ItemObject("Header 3",true));
list.add(new ItemObject("Example Text 1","Something Here 1"));
list.add(new ItemObject("Example Text 2","Something Here 2 "));
list.add(new ItemObject("Example Text 3","Something Here 3"));
return list;
}
private RecyclerSectionItemDecoration.SectionCallback getSectionCallback(final List<ItemObject> list) {
return new RecyclerSectionItemDecoration.SectionCallback() {
@Override
public boolean isSection(int position) {
return list.get(position).isHeader();
}
@Override
public String getSectionHeader(int position) {
return list.get(position).getHeaderTitle();
}
};
}
}
这是我的RecyclerSectionItemDecoration
public class RecyclerSectionItemDecoration extends RecyclerView.ItemDecoration {
private final int headerOffset;
private final boolean sticky;
private final SectionCallback sectionCallback;
private View headerView;
private TextView header;
public RecyclerSectionItemDecoration(int headerHeight, boolean sticky, @NonNull SectionCallback sectionCallback) {
headerOffset = headerHeight;
this.sticky = sticky;
this.sectionCallback = sectionCallback;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int pos = parent.getChildAdapterPosition(view);
if (sectionCallback.isSection(pos)) {
outRect.top = headerOffset;
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if (headerView == null) {
headerView = inflateHeaderView(parent);
header = (TextView) headerView.findViewById(R.id.tv_header);
fixLayoutSize(headerView, parent);
}
CharSequence previousHeader = "";
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
final int position = parent.getChildAdapterPosition(child);
CharSequence title = sectionCallback.getSectionHeader(position);
header.setText(title);
if (!previousHeader.equals(title) || sectionCallback.isSection(position)) {
drawHeader(c, child, headerView);
previousHeader = title;
}
}
}
private void drawHeader(Canvas c, View child, View headerView) {
c.save();
if (sticky) {
c.translate(0, Math.max(0, child.getTop() - headerView.getHeight()));
} else {
c.translate(0, child.getTop() - headerView.getHeight());
}
headerView.draw(c);
c.restore();
}
private View inflateHeaderView(RecyclerView parent) {
return LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_header, parent, false);
}
private void fixLayoutSize(View view, ViewGroup parent) {
int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(),
View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(),
View.MeasureSpec.UNSPECIFIED);
int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
parent.getPaddingLeft() + parent.getPaddingRight(),
view.getLayoutParams().width);
int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
parent.getPaddingTop() + parent.getPaddingBottom(),
view.getLayoutParams().height);
view.measure(childWidth, childHeight);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
}
public interface SectionCallback {
boolean isSection(int position);
CharSequence getSectionHeader(int position);
}
}
这是我的模特
class ItemObject {
private String pageName;
private String pageUrl;
private String headerTitle;
private boolean isHeader ;
public ItemObject(String pageName , String pageUrl) {
this.pageName = pageName;
this.pageUrl = pageUrl;
}
public ItemObject(String headerTitle, boolean isHeader) {
this.headerTitle = headerTitle;
this.isHeader = isHeader;
}
public String getHeaderTitle() {
return headerTitle;
}
public String getPageName() {
return pageName;
}
public String getPageUrl() {
return pageUrl;
}
public boolean isHeader() {
return isHeader;
}
}
答案 0 :(得分:1)
问题出在onDrawOver
方法中,我们可以将其隔离为以下代码:
CharSequence previousHeader = "";
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
final int position = parent.getChildAdapterPosition(child);
CharSequence title = sectionCallback.getSectionHeader(position);
header.setText(title);
if (!previousHeader.equals(title) || sectionCallback.isSection(position)) {
drawHeader(c, child, headerView);
previousHeader = title;
}
}
stacktrace告诉我们它发生在我们称为equals
的行中,这是因为目标对象是null
。因此,我们推断previousHeader
在某些时候必须为空。
它已初始化为""
,所以这一定是由于在这一行中分配了null
:
previousHeader = title;
换句话说,sectionCallback.getSectionHeader(position)
必须在某个时候返回null
。
那该怎么办?
要视情况而定。
如果将“节标题”设为null
不正确,请找出原因,并加以解决。
如果正确(或者无论如何都要处理!),那么可以防止NPE的最小更改就是更改
previousHeader = title;
到
previousHeader = (title == null) ? "" : title;
答案 1 :(得分:-1)
在RecyclerSectionItemDecoration中编辑代码
if (headerView == null) {
headerView = inflateHeaderView(parent);
header = (TextView) headerView.findViewById(R.id.tv_header);
fixLayoutSize(headerView, parent);
}
收件人
headerView = inflateHeaderView(parent);
header = (TextView) headerView.findViewById(R.id.tv_header);
fixLayoutSize(headerView, parent);
如果无法使用Canvas c,我跟踪它,但没有任何参考。