这是我跟踪的sample code。
下面的代码是示例代码中onCreateViewHolder
的一部分:
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
// set the view's size, margins, paddings and layout parameters
...
ViewHolder vh = new ViewHolder(v);
return vh;
}
这是视窗的一部分:
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
显然,参数
TextView v
是错误的。然后我在
onCreateViewHolder
方法中玩了一个小把戏,如下所示: ...
TextView textView = (TextView) v.findViewById(R.id.tv_test);
ViewHolder vh = new ViewHolder(textView);
但我运行应用程序时出现异常。
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3693)
at android.view.ViewGroup.addView(ViewGroup.java:3546)
at android.view.ViewGroup.addView(ViewGroup.java:3491)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:3533)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:3558)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1012)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:524)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1461)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:1600)
at android.view.View.layout(View.java:15273)
at android.view.ViewGroup.layout(ViewGroup.java:4763)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1069)
at android.view.View.layout(View.java:15273)
at android.view.ViewGroup.layout(ViewGroup.java:4763)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:457)
at android.widget.FrameLayout.onLayout(FrameLayout.java:392)
at android.view.View.layout(View.java:15273)
at android.view.ViewGroup.layout(ViewGroup.java:4763)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
at android.view.View.layout(View.java:15273)
at android.view.ViewGroup.layout(ViewGroup.java:4763)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:457)
at android.widget.FrameLayout.onLayout(FrameLayout.java:392)
at android.view.View.layout(View.java:15273)
at android.view.ViewGroup.layout(ViewGroup.java:4763)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2057)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1814)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1044)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5749)
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$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
但是当我像这样更改viewholder代码时:
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v;
}
不会有事的。
我想知道这个例外是什么意思?
是什么导致了这个例外?
最佳答案
我偶然发现了同一个样本,很快也被弄糊涂了。传递给viewholder的视图,因此构造函数是包含一个列表项的所有视图的膨胀父视图。使用此传递的视图初始化其构造函数中ViewHolder的所有字段,如下所示:
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView titleWithIcon;
public TextView teaser;
public MyViewHolder(View itemView) {
super(itemView);
titleWithIcon = (TextView) itemView.findViewById(R.id.title_with_icon);
teaser = (TextView) itemView.findViewById(R.id.teaser);
}
}