Recyclerview(获取项目上Recyclerview) [英] Recyclerview(Getting item on Recyclerview)
问题描述
我已经在GitHub上使用此实现搜索查看+ Recyclerview。 github上
我的下一步是获得在recyclerview选定部分项目。
然后,我看到一些code混得recyclerview孩子。
在code的工作时,getChildAt(指数)= 0。
但是,当我穿上指数= 12或大于。
该程序崩溃。
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnItemTouchListener(
新RecyclerItemClickListener(的getContext(),新RecyclerItemClickListener.OnItemClickListener(){
@覆盖
公共无效onItemClick(查看视图,INT位置){
最终诠释valueThisIteration =位置;
mRecyclerView.getViewTreeObserver()。addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener(){
@覆盖
公共无效onGlobalLayout(){
TextView的textViewDrawerTitle =(TextView中)mRecyclerView.getChildAt(valueThisIteration).findViewById(R.id.tvText);
textViewDrawerTitle.setText(选中);
。mRecyclerView.getViewTreeObserver()removeGlobalOnLayoutListener(本);
}
});
}
})
);
和即时得到这个错误。
九月11号至四号:36:02.818 1629年至1629年/? E / AndroidRuntime:致命异常:主要
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:工艺:com.thesis.juandirection.juandirectionfinale,PID:1629
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:显示java.lang.NullPointerException:尝试在空对象引用调用虚拟方法android.view.View android.view.View.findViewById(INT)'
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在com.thesis.juandirection.juandirectionfinale.fragments.FragmentSearch$1$1.onGlobalLayout(FragmentSearch.java:94)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:815)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1867)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.view.Choreographer.doCallbacks(Choreographer.java:580)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.view.Choreographer.doFrame(Choreographer.java:550)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.os.Handler.handleCallback(Handler.java:739)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:95)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.os.Looper.loop(Looper.java:135)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5221)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在java.lang.reflect.Method.invoke(本机方法)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java:372)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
九月11号至4日:36:02.818 1629年至1629年/? E / AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
有关处理项目的点击次数在RecyclerView,我建议你将你的逻辑到你的适配器的ViewHolder。你可以有你ViewHolder实施 View.OnClickListener
,并覆盖的OnClick()
方法preform行动。如果您的操作取决于单击的项目,您可以使用 getAdapterPosition引用它()
。在code会是这个样子:
公共类MyViewHolder扩展RecyclerView.ViewHolder实现View.OnItemClickListener {
公共MyViewHolder(查看视图){
超(视图);
view.setOnClickListener(本);
}
@覆盖
公共无效的OnClick(查看视图){
//获取单击的项目
//在这个例子中,我假设你的数据源类型`名单,其中,为MyObject>`
为MyObject myObject的= mDataSource.get(getAdapterPosition());
//然后,你可以做任何操作就可以了,例如 -
myObject.setChecked();
}
}
显然,这种情况发生在的OnClick逻辑
将改变你的榜样,但我希望这将设置您在正确的轨道。对于此另一个样品,以及一个位的处理的ViewHolder内点击逻辑的优势的说明,签出博客帖子,我写的比较RecyclerView和ListView(寻找标有更明确地点击监听器一节)。
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)
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.OnItemClickListener {
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();
}
}
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屋!