简体   繁体   English

旋转时自动调用onBindViewHolder

[英]Automatically Call onBindViewHolder on rotate

I am working on my app and it requires the movie posters to be 1:2 ratio. 我正在使用我的应用程序,它要求电影海报的比例为1:2。 I have found a way to do this is to run 我发现一种方法可以运行

        movieAdapterViewHolder.posterview.requestLayout();
    ViewTreeObserver viewTreeObserver = movieAdapterViewHolder.cardview.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                movieAdapterViewHolder.cardview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                movieAdapterViewHolder.posterview.getLayoutParams().height = Double.valueOf(movieAdapterViewHolder.posterview.getWidth() * 1.5).intValue();
            }
        });
    }

However, this doesn't get called immediately on rotate. 但是,旋转时不会立即调用此方法。 It only gets called from onBindViewHolder . 它只能从onBindViewHolder Is there any other way to set the height of a CardView in a RecyclerView to 2x it's width or how can I somehow call onBindViewHolder? 还有其他方法可以将RecyclerView中CardView的高度设置为其宽度的2倍,或者如何以某种方式调用onBindViewHolder?

You can notify your adapter about changes that you have made by calling yourAdapter.notifyDataSetChanged() . 您可以通过调用yourAdapter.notifyDataSetChanged()来通知适配器您所做的更改。 it calls onBindViewHolder and changes your items view with latest changes. 它调用onBindViewHolder并使用最新更改来更改您的项目视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM