2014-08-29 3 views
5

다음은 내가 수행 한 sample code입니다.RecyclerView의 Android ViewHolder 매개 변수 # onCreateViewHolder Differents

는 그리고 아래의 코드는 샘플 코드에서 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; 
} 

그리고 이것은 ViewHolder의 일부입니다

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; 
    } 

이 확인 될 것입니다.

이 예외가 무엇을 의미하는지 알고 싶습니다. 이 예외는 무엇이 발생 했습니까?

답변

22

나는 같은 샘플을 우연히 발견하고 곧 혼란스러워했다. ViewHolder에 전달 된 뷰이므로 생성자는 하나의 목록 항목에 대한 모든 뷰를 포함하는 비정상적 부모 뷰입니다. 이 전달 된 View를 사용하여 생성자에서 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); 
    } 
} 
관련 문제