Recyclerview(在 Recyclerview 上获取项目) [英] Recyclerview(Getting item on Recyclerview)
问题描述
我已经在 github 上使用它实现了 SearchView + Recyclerview.GITHUB
我的下一步是在 recyclerview 上获取选定部分上的项目.
然后我看到一些代码让孩子进入 recyclerview.
当 getChildAt(index) =0.
时代码正在工作但是当我设置 index=12 或更大时.
程序崩溃了.
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);
}
});
}
})
);
我收到了这个错误.
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)
推荐答案
为了处理 RecyclerView 中的项目点击,我建议您将逻辑移到 Adapter 的 ViewHolder 中.您可以让 ViewHolder 实现 View.OnClickListener
,并覆盖 onClick()
方法以执行操作.如果您的操作取决于点击的项目,您可以使用 getAdapterPosition()
引用它.代码看起来像这样:
For handling item clicks in a RecyclerView, I recommend you move your logic into the ViewHolder of your Adapter. You can have your ViewHolder implement View.OnClickListener
, and override the onClick()
method to preform an action. If your action depends on the item clicked, you can reference it using getAdapterPosition()
. The code will look something like this:
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// Get the item clicked
// For this example, I'm assuming your data source is of type `List<MyObject>`
MyObject myObject = mDataSource.get(getAdapterPosition());
// Then you can do any actions on it, for example:
myObject.setChecked();
}
}
很明显,onClick
内部发生的逻辑将更改为您的示例,但我希望这能让您走上正确的轨道.对于另一个示例,以及对处理 ViewHolder 内部点击逻辑的好处的一些解释,请查看 博文,我写的比较 RecyclerView 和 ListView(查找标有更明确的点击侦听器"的部分).
Clearly the logic that happens inside onClick
will change to your example, but I hope this sets you on the right track. For another sample of this, as well as a bit of an explanation for the benefits of handling click logic inside the ViewHolder, check out a blog post that I wrote comparing RecyclerView and ListView (look for the section labeled "More explicit click listeners").
这篇关于Recyclerview(在 Recyclerview 上获取项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!