Skip to content

Commit 1aae681

Browse files
committed
Fixed view size stretching
1 parent de94001 commit 1aae681

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import java.util.*
3535
*/
3636
class CodeView : RelativeLayout {
3737

38-
private val vRoot: View
3938
private val vPlaceholder: View
4039
private val vShadowRight: View
4140
private val vShadowBottomLine: View
@@ -61,7 +60,6 @@ class CodeView : RelativeLayout {
6160
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
6261
inflater.inflate(R.layout.layout_code_view, this, true)
6362

64-
vRoot = findViewById(R.id.v_root)
6563
vPlaceholder = findViewById(R.id.v_placeholder)
6664
vShadowRight = findViewById(R.id.v_shadow_right)
6765
vShadowBottomLine = findViewById(R.id.v_shadow_bottom_line)
@@ -145,13 +143,11 @@ class CodeView : RelativeLayout {
145143
// default color theme provided by enum
146144
fun setColorTheme(colorTheme: ColorTheme) = addTask {
147145
adapter.colorTheme = colorTheme.with()
148-
fillBackground(colorTheme.bgContent)
149146
}
150147

151148
// custom color theme provided by user
152149
fun setColorTheme(colorTheme: ColorThemeData) = addTask {
153150
adapter.colorTheme = colorTheme
154-
fillBackground(colorTheme.bgContent)
155151
}
156152

157153
/**
@@ -207,10 +203,10 @@ class CodeView : RelativeLayout {
207203
build(content)
208204
ViewState.PREPARE ->
209205
Thread.delayed {
210-
adapter.updateCodeContent(content)
206+
update(content)
211207
}
212208
ViewState.PRESENTED ->
213-
adapter.updateCodeContent(content)
209+
update(content)
214210
}
215211
}
216212

@@ -231,13 +227,25 @@ class CodeView : RelativeLayout {
231227
Thread.delayed {
232228
rvCodeContent.adapter = CodeContentAdapter(context, content)
233229
processBuildTasks()
234-
//fillBackground()
235230
setupShadows()
236231
hidePlaceholder()
237232
state = ViewState.PRESENTED
238233
}
239234
}
240235

236+
/**
237+
* Hot view updating.
238+
*
239+
* @param content Code content
240+
*/
241+
private fun update(content: String) {
242+
state = ViewState.PREPARE
243+
measurePlaceholder(extractLines(content).size)
244+
adapter.updateCodeContent(content)
245+
hidePlaceholder()
246+
state = ViewState.PRESENTED
247+
}
248+
241249
// - Setup actions
242250

243251
/**
@@ -246,27 +254,24 @@ class CodeView : RelativeLayout {
246254
*/
247255
private fun setupShadows() = setShadowsVisible(!adapter.isFullShowing)
248256

249-
/**
250-
* Fill background to color accordingly to color theme.
251-
*
252-
* @color Background color
253-
*/
254-
private fun fillBackground(color: Int = adapter.colorTheme.bgContent) =
255-
vRoot.setBackgroundColor(color.color())
256-
257257
/**
258258
* Placeholder fills space at start and stretched to marked up view size
259-
* (by extracting code lines) because at this point it's not built yet.
259+
* (by code lines count) because at this point it's not built yet.
260260
*
261261
* @param linesCount Count of lines to measure space for placeholder
262262
*/
263263
private fun measurePlaceholder(linesCount: Int) {
264264
val lineHeight = dpToPx(context, 24)
265-
val topMargin = dpToPx(context, 8)
266-
val height = linesCount * lineHeight + 2 * topMargin
265+
val topPadding = dpToPx(context, 8)
266+
267+
// double padding (top & bottom) for big view, one is enough for small
268+
val padding = (if (linesCount > 1) 2 else 1) * topPadding
269+
270+
val height = linesCount * lineHeight + padding
267271

268272
vPlaceholder.layoutParams = RelativeLayout.LayoutParams(
269273
RelativeLayout.LayoutParams.MATCH_PARENT, height)
274+
vPlaceholder.visibility = View.VISIBLE
270275
}
271276

272277
// - Animations

codeview/src/main/res/layout/layout_code_view.xml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
6-
android:layout_height="wrap_content"
7-
android:id="@+id/v_root">
6+
android:layout_height="wrap_content">
87

98
<io.github.kbiakov.codeview.BidirectionalScrollView
109
android:id="@+id/v_scroll"
1110
android:layout_width="match_parent"
12-
android:layout_height="wrap_content">
11+
android:layout_height="wrap_content"
12+
android:fillViewport="true">
1313

1414
<android.support.v7.widget.RecyclerView
1515
android:id="@+id/rv_code_content"
@@ -25,27 +25,26 @@
2525
android:layout_height="match_parent"
2626
android:layout_alignParentRight="true"
2727
android:background="@drawable/shadow_right"
28-
android:visibility="invisible"/>
28+
android:visibility="gone"/>
2929

3030
<LinearLayout
3131
android:layout_width="match_parent"
3232
android:layout_height="16dp"
3333
android:layout_alignParentBottom="true"
34-
android:orientation="horizontal">
34+
android:orientation="horizontal"
35+
android:visibility="gone">
3536

3637
<View
3738
android:id="@+id/v_shadow_bottom_line"
3839
android:layout_width="24dp"
3940
android:layout_height="match_parent"
40-
android:background="@drawable/shadow_bottom_line"
41-
android:visibility="invisible"/>
41+
android:background="@drawable/shadow_bottom_line"/>
4242

4343
<View
4444
android:id="@+id/v_shadow_bottom_content"
4545
android:layout_width="match_parent"
4646
android:layout_height="match_parent"
47-
android:background="@drawable/shadow_bottom_content"
48-
android:visibility="invisible"/>
47+
android:background="@drawable/shadow_bottom_content"/>
4948

5049
</LinearLayout>
5150

example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2222

2323
// use chaining to build view
2424
codeView.highlightCode("js")
25-
//.setColorTheme(ColorTheme.SOLARIZED_LIGHT.withBgContent(myColor))
25+
.setColorTheme(ColorTheme.SOLARIZED_LIGHT)
2626
.setCodeContent(getString(R.string.listing_js));
2727

2828
// do not use chaining for built view
2929
// (you can, but follow it should be performed sequentially)
3030
codeView.setCodeContent(getString(R.string.listing_java));
31+
codeView.setColorTheme(ColorTheme.DEFAULT);
3132
codeView.highlightCode("java");
3233
}
3334
}

example/src/main/res/layout/activity_listings.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
<io.github.kbiakov.codeview.CodeView
1010
android:id="@+id/code_view"
1111
android:layout_width="match_parent"
12-
android:layout_height="120dp"/>
13-
14-
<TextView
15-
android:layout_width="match_parent"
16-
android:layout_height="match_parent"
17-
android:layout_below="@+id/code_view"
18-
android:text="123123123123"/>
12+
android:layout_height="wrap_content"/>
1913

2014
</RelativeLayout>

0 commit comments

Comments
 (0)