React Native custom component doesn't set component's size dynamically

前端 未结 1 1630
野趣味
野趣味 2021-01-22 01:41

I\'m trying to build an RN component that displays Tweets. I forked a great RN component, react-native-fabric-twitterkit and added a component that displays a tweet. So far so g

1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-22 01:56

    The github issue linked by @smilledge has the answer.

    When adding views dynamically in a native component it seems you need to manually trigger a layout cycle in order for react-native to pick up on the changes. (perhaps related to react native updating it's shadow-view)

     @Override
        public void requestLayout() {
            super.requestLayout();
            post(measureAndLayout);
        }
    
        private final Runnable measureAndLayout = new Runnable() {
            @Override
            public void run() {
                measure(
                        MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
                layout(getLeft(), getTop(), getRight(), getBottom());
            }
        };
    

    0 讨论(0)
提交回复
热议问题