1

因此,我試圖根據回收視圖的onBindViewHolder方法更改ImageView的高度,具體取決於同一視圖持有者中另一個對象的高度。當我通過getHeight()訪問高度時,前幾個元素的結果爲0,這很可能是因爲它們現在沒有被繪製。在回收視圖中訪問對象的正確高度

general solution這個問題不適合我的工作,因爲回收視圖創建和綁定了很多意見加入GlobalLayoutListener他們每個人,這似乎是混亂的事情了給我錯誤的結果(例如,只是改變第一個元素的高度)。

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

    final ViewHolderDouble doubleHolder = (ViewHolderDouble) holder; 

    if (secProduct != null) { 
     final ViewTreeObserver observer = doubleHolder.linearLayoutSD.getViewTreeObserver(); 
     observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 

       int difference = doubleHolder.firstCardView.getHeight() - doubleHolder.secCardView.getHeight(); 

       if (difference > 0) { 
        ImageView rightImage = doubleHolder.imageViewR; 
        rightImage.getLayoutParams().height = rightImage.getHeight() + difference; 
        rightImage.requestLayout(); 
       } else if (difference < 0) { 
        ImageView leftImage = doubleHolder.imageViewL; 
        leftImage.getLayoutParams().height = leftImage.getHeight() + (difference * -1); 
        leftImage.requestLayout(); 
       } 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
        doubleHolder.linearLayoutSD.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
       } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
        doubleHolder.linearLayoutSD.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       } 
      } 
     }); 
    } 
} 

有沒有什麼辦法可以訪問doubleHolder.firstCardView & doubleHolder.secCardView高度的意見後得出的,所以我得到正確的高度,而不是0?

回答

0

前段時間我的任務非常相似,所以我就是這麼做的。

一,適配器。我沒有做太多的onBindViewHolder,我剛剛運行的方法「displayItem」的內容 - 填補了實際View

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int position) { 
    if (position >= contentItems.size()) { 
     return; 
    } 

    ((ContentItemCardView)viewHolder.itemView).displayItem(contentItems.get(position)); 
} 

關鍵部分發生在我的情況下ContentItemCardViewextends CardView。下面是它的短版:

public class ContentItemCardView extends CardView { 

    @InjectView(R.id.previewImageView) 
    ImageView previewImageView; 

    @InjectView(R.id.titleTextView) 
    TextView titleTextView; 

    public ContentItemCardView(Context context){ 
     super(context); 
     ButterKnife.inject(this); 
    } 

    ..... 

    public void displayItem(ContentItem contentItem) { 
     .... 
     titleTextView.setText(contentItem.Title); 
     displayPreview(contentItem); 
     .... 
    } 

    public void displayPreview(ContentItem contentItem) { 
     int requiredHeight = determineHeight(.....); 
     previewImageView.getLayoutParams().height = requiredHeight; 
     previewImageView.requestLayout(); 
     .... 
    } 

    public int determineHeight() { 
     .... 
    } 
} 

因此,所有我們所剩下的就是要有正確的determineHeight()(比方說,它應該是高兩倍,比填充titleTextView):

public int determineHeight() { 
    final Resources resources = getResources(); 
    int screenWidth = resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? 
      resources.getDisplayMetrics().widthPixels : 
      resources.getDisplayMetrics().heightPixels; 

    int widthMeasureSpec = MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.AT_MOST); 
    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 

    titleTextView.measure(widthMeasureSpec, heightMeasureSpec); 

    return titleTextView.getMeasuredHeight() * 2; 
}    

因此,所有你需要的是明確設置計算出的高度和requestLayout()

 int requiredHeight = determineHeight(.....); 
     previewImageView.getLayoutParams().height = requiredHeight; 
     previewImageView.requestLayout(); 

我希望它有幫助。