2016-11-28 4 views
4

내 레이아웃에 표시 데이터를로드하는 데 ViewStubs을 사용하고 있습니다. 을 사용하여 레이아웃 구성 요소를 바인딩하기 때문에 개별 viewstub 레이아웃의 구성 요소를 보유하는 맞춤 클래스가 있습니다. 예를 들어 이러한 뷰 스텁은 다음과 같습니다.ViewStub에 null이 아닌 ViewGroup 뷰가 있어야합니다.

public class FeaturedContentView { 
     @BindView(R.id.art) 
     ImageView art; 
     @BindView(R.id.shade) 
     View shade; 
     @BindView(R.id.title) 
     TextView featuredTitle; 
     @BindView(R.id.performer) 
     TextView performer; 
     @BindView(R.id.featured_title) 
     KerningTextView featuredText; 
     @BindView(R.id.play_button) 
     Button play; 
     @BindView(R.id.shareText) 
     TextView shareText; 
     @BindView(R.id.addToFavorites) 
     ImageView addToFavs; 

     FeaturedContentView(View view) { 
      ButterKnife.bind(this, view); 
     } 
    } 

이 같은 레이아웃 팽창 : 다음과 같이

<ViewStub 
     android:id="@+id/featuredContentStub" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inflatedId="@+id/featuredContent" 
     android:layout="@layout/featured_content" /> 

@layout/featured_content 구성 요소를 처리하는 클래스입니다 위의 방법에서 두 개의 서로 다른 장소에서라고

if (viewstub != null) { 
     View view = viewstub.inflate(); 
     featuredContentView = new FeaturedContentView(view); 
} 

을 내 파편. 첫 번째 시간은 정확하게 팽창하지만 두 번째는 java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent입니다.

어떻게 이런 상황을 처리 할 수 ​​있습니까?

+0

전체 조각 코드를 게시 할 수 있습니까, 나는 당신의'viewstub.inflate()'에 의심의 여지가 있습니다. –

답변

3

안드로이드는 다음과 같이 ViewStub을 팽창 :

  1. 는 처음에 당신이 inflate를 호출 할 때 지정된 레이아웃이보기를 교체 다른보기
  2. 같은 계층 구조 같은 방식으로 볼 수있는 ViewStub을 추가합니다.

이 의미, 코드가 두 번째로 호출 될 때, 원래 ViewStub 개체가 긴보기 계층 구조에서 분리하고 이미 전체보기로 대체된다.

저는 개인적으로 ViewStub이 현재의 형태로 매우 불편하다고 생각합니다. 특히 ButerKnife로 작업 할 때 특히 그렇습니다. 다행히도 클래스 자체는 매우 간단합니다. 사용자 정의 클래스를 만들 수 있습니다. 사용자 정의 클래스는 동일한 작업을 수행하고 필요한 메소드를 추가합니다 (예 : isInflated, addInflateCallback 등). Android 지원 라이브러리 개발자는 동일한 btw를 사용합니다.