Skip to content

Commit 08a7b7b

Browse files
authored
Merge pull request #266 from hotwired/update_layout_params_based_on_parent
Set WebView's height to wrap content if its parent is ScrollingView
2 parents 1e3f6c7 + 5c63981 commit 08a7b7b

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

turbo/src/main/kotlin/dev/hotwire/turbo/views/TurboView.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import android.view.ViewGroup
99
import android.webkit.WebView
1010
import android.widget.FrameLayout
1111
import android.widget.ImageView
12-
import androidx.core.view.contains
13-
import androidx.core.view.drawToBitmap
14-
import androidx.core.view.isVisible
12+
import androidx.core.view.*
1513
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
1614
import dev.hotwire.turbo.R
1715

@@ -47,6 +45,13 @@ class TurboView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
4745
// doesn't have a new parent, since we have no control
4846
// over the message queue.
4947
if (isAttachedToWindow && webView.parent == null) {
48+
webView.updateLayoutParams {
49+
height = when (webViewContainer) {
50+
is ScrollingView -> LayoutParams.WRAP_CONTENT
51+
else -> LayoutParams.MATCH_PARENT
52+
}
53+
}
54+
5055
webViewContainer.addView(webView)
5156
onAttachedToNewDestination(true)
5257
}

turbo/src/main/kotlin/dev/hotwire/turbo/views/TurboWebView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ open class TurboWebView @JvmOverloads constructor(context: Context, attrs: Attri
2929
settings.javaScriptEnabled = true
3030
settings.domStorageEnabled = true
3131
settings.setSupportMultipleWindows(true)
32-
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
32+
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
3333
}
3434

3535
/**

turbo/src/test/kotlin/dev/hotwire/turbo/views/TurboViewTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import android.os.Build
55
import android.view.LayoutInflater
66
import android.view.ViewGroup
77
import android.webkit.WebView
8+
import android.widget.FrameLayout.LayoutParams
89
import androidx.test.core.app.ApplicationProvider
10+
import com.nhaarman.mockito_kotlin.whenever
911
import dev.hotwire.turbo.R
1012
import org.assertj.core.api.Assertions.assertThat
1113
import org.junit.Before
@@ -30,6 +32,10 @@ class TurboViewTest {
3032
context = ApplicationProvider.getApplicationContext()
3133
view = LayoutInflater.from(context).inflate(R.layout.turbo_view, null) as ViewGroup
3234
turboView = view.findViewById(R.id.turbo_view)
35+
36+
whenever(webView.layoutParams).thenReturn(
37+
LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
38+
)
3339
}
3440

3541
@Test fun refreshLayoutIsFirstChild() {

0 commit comments

Comments
 (0)