在回收器视图中访问对象的正确高度

时间:2015-12-15 04:54:14

标签: android android-layout android-recyclerview android-viewholder

所以,我正在尝试根据同一视图持有者中另一个对象的高度来更改我的回收器视图的ImageView方法中onBindViewHolder的高度。当我使用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&amp;的高度绘制视图后doubleHolder.secCardView,所以我得到正确的高度而不是0?

1 个答案:

答案 0 :(得分:0)

前一段时间我的任务非常相似,所以我就是这样做的。

首先,适配器。我在onBindViewHolder做了很多事情,我只是运行方法&#34; displayItem&#34;内容 - 填写实际View

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

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

关键部分发生在ContentItemCardView extends 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();

我希望,这有帮助。