I've implemented SearchView + Recyclerview using this on github. GITHUB
My next step is to get the item on selected part on the recyclerview.
Then I saw some code getting the child on recyclerview.
The code is working when the getChildAt(index) =0.
But when i put on index=12 or greater than that.
The program crashed.
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
final int valueThisIteration = position;
mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
TextView textViewDrawerTitle = (TextView) mRecyclerView.getChildAt(valueThisIteration).findViewById(R.id.tvText);
textViewDrawerTitle.setText("Checked");
mRecyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
})
);
And im getting this error.
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: FATAL EXCEPTION: main
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: Process: com.thesis.juandirection.juandirectionfinale, PID: 1629
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at com.thesis.juandirection.juandirectionfinale.fragments.FragmentSearch$1$1.onGlobalLayout(FragmentSearch.java:94)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:815)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1867)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:580)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:550)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-04 09:36:02.818 1629-1629/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…