Skip to content

Commit ef55a9b

Browse files
committed
Fixed top & bottom padding
1 parent 7f47e38 commit ef55a9b

5 files changed

Lines changed: 71 additions & 59 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import android.text.Html
77
import android.text.SpannableString
88
import android.text.Spanned
99
import android.text.TextUtils
10-
import android.util.Log
1110
import android.util.TypedValue
1211
import java.io.BufferedReader
1312
import java.io.InputStreamReader
1413
import java.util.concurrent.Executors
15-
import java.util.stream.IntStream
1614

1715
object Const {
1816
val DefaultDelay = 250L

codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import android.widget.TextView
1212
import io.github.kbiakov.codeview.*
1313
import io.github.kbiakov.codeview.Thread.async
1414
import io.github.kbiakov.codeview.Thread.ui
15+
import io.github.kbiakov.codeview.adapters.AbstractCodeAdapter.ViewHolderType.Companion.BordersCount
16+
import io.github.kbiakov.codeview.adapters.AbstractCodeAdapter.ViewHolderType.Companion.LineStartIdx
1517
import io.github.kbiakov.codeview.classifier.CodeClassifier
1618
import io.github.kbiakov.codeview.classifier.CodeProcessor
1719
import io.github.kbiakov.codeview.highlight.*
@@ -174,33 +176,38 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
174176
val tvLineContent = lineView.findViewById(R.id.tv_line_content) as TextView
175177
tvLineContent.typeface = options.font
176178

177-
val holder = ViewHolder(lineView)
178-
holder.setIsRecyclable(false)
179-
return holder
179+
val isLine = viewType == ViewHolderType.Line.viewType
180+
val lineViewHeight = if (isLine) R.dimen.line_height else R.dimen.line_border_height
181+
lineView.layoutParams.height = context.resources.getDimension(lineViewHeight).toInt()
182+
183+
if (isLine) {
184+
val holder = LineViewHolder(lineView)
185+
holder.setIsRecyclable(false)
186+
return holder
187+
} else {
188+
return BorderViewHolder(lineView)
189+
}
180190
}
181191

182192
override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
183-
val codeLine = lines[pos]
184-
holder.mItem = codeLine
185-
186-
options.lineClickListener?.let {
187-
holder.itemView.setOnClickListener {
188-
options.lineClickListener?.onCodeLineClicked(pos, codeLine)
193+
if (holder is LineViewHolder) {
194+
val lineNum = pos - LineStartIdx
195+
val lineCode = lines[lineNum]
196+
holder.mItem = lineCode
197+
198+
options.lineClickListener?.let {
199+
holder.itemView.setOnClickListener {
200+
options.lineClickListener?.onCodeLineClicked(lineNum, lineCode)
201+
}
189202
}
203+
setupLine(lineNum, lineCode, holder)
204+
displayLineFooter(lineNum, holder)
190205
}
191-
192-
setupLine(pos, codeLine, holder)
193-
displayLineFooter(pos, holder)
194-
addExtraPadding(pos, holder)
195206
}
196207

197-
override fun getItemCount() = lines.size
208+
override fun getItemCount() = lines.size + BordersCount // lines + borders
198209

199-
private fun Int.isFirst() = this == 0
200-
private fun Int.isLast() = this == itemCount - 1
201-
private fun Int.isJustFirst() = isFirst() && !isLast()
202-
private fun Int.isJustLast() = isLast() && !isFirst()
203-
private fun Int.isBorder() = isFirst() || isLast()
210+
override fun getItemViewType(pos: Int) = ViewHolderType.get(pos, itemCount)
204211

205212
// - Helpers (for view holder)
206213

@@ -209,7 +216,7 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
209216
holder.tvLineContent.text = html(line)
210217
holder.tvLineContent.setTextColor(options.theme.noteColor.color())
211218

212-
if (options.shortcut && pos == MAX_SHORTCUT_LINES) {
219+
if (options.shortcut && pos == MaxShortcutLines) {
213220
holder.tvLineNum.textSize = 10f
214221
holder.tvLineNum.text = context.getString(R.string.dots)
215222
} else {
@@ -233,31 +240,38 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
233240
}
234241
}
235242

236-
private fun addExtraPadding(pos: Int, holder: ViewHolder) {
237-
if (pos.isBorder()) {
238-
val dp8 = dpToPx(context, 8)
239-
val topPadding = if (pos.isJustFirst()) dp8 else 0
240-
val bottomPadding = if (pos.isJustLast()) dp8 else 0
241-
holder.tvLineNum.setPadding(0, topPadding, 0, bottomPadding)
242-
holder.tvLineContent.setPadding(0, topPadding, 0, bottomPadding)
243-
} else {
244-
holder.tvLineNum.setPadding(0, 0, 0, 0)
245-
holder.tvLineContent.setPadding(0, 0, 0, 0)
246-
}
247-
}
248-
249243
companion object {
250-
private const val MAX_SHORTCUT_LINES = 6
244+
private const val MaxShortcutLines = 6
251245

252246
private fun Pair<List<String>, List<String>>.linesToShow() = first
253247
private fun Pair<List<String>, List<String>>.droppedLines() = second
254248
}
255249

250+
// - ViewHolder
251+
252+
enum class ViewHolderType(val viewType: Int) {
253+
Line(0), Border(1);
254+
255+
companion object {
256+
const val LineStartIdx = 1
257+
const val BordersCount = 2
258+
259+
fun Int.lineEndIdx() = this - BordersCount
260+
261+
fun get(pos: Int, n: Int) = when (pos) {
262+
in LineStartIdx .. n.lineEndIdx() ->
263+
ViewHolderType.Line.viewType
264+
else ->
265+
ViewHolderType.Border.viewType
266+
}
267+
}
268+
}
269+
256270
/**
257271
* View holder for code adapter.
258272
* Stores all views related to code line layout.
259273
*/
260-
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
274+
open class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
261275
val tvLineNum = itemView.findViewById(R.id.tv_line_num) as TextView
262276
val tvLineContent = itemView.findViewById(R.id.tv_line_content) as TextView
263277
val llLineFooter = itemView.findViewById(R.id.ll_line_footer) as LinearLayout
@@ -266,6 +280,14 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
266280

267281
override fun toString() = "${super.toString()} '$mItem'"
268282
}
283+
284+
class LineViewHolder(itemView: View) : ViewHolder(itemView)
285+
286+
/**
287+
* View holder for padding.
288+
* Stores all views related to code line layout.
289+
*/
290+
class BorderViewHolder(itemView: View) : ViewHolder(itemView)
269291
}
270292

271293
/**

codeview/src/main/java/io/github/kbiakov/codeview/views/BidirectionalScrollView.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,20 @@ class BidirectionalScrollView : HorizontalScrollView {
3333
currentY = event.rawY.toInt()
3434
return super.dispatchTouchEvent(event)
3535
}
36-
3736
MotionEvent.ACTION_MOVE -> {
3837
val deltaX = Math.abs(currentX - event.rawX)
3938
val deltaY = Math.abs(currentY - event.rawY)
4039
scroll(event)
4140

4241
val movedOnDistance = dpToPx(context, 2)
43-
4442
if (deltaX > movedOnDistance || deltaY > movedOnDistance)
4543
isMoved = true
4644
}
47-
4845
MotionEvent.ACTION_UP -> {
4946
if (!isMoved)
5047
return super.dispatchTouchEvent(event)
51-
5248
isMoved = false
5349
}
54-
5550
MotionEvent.ACTION_CANCEL -> isMoved = false
5651
}
5752
return true
@@ -69,21 +64,18 @@ class BidirectionalScrollView : HorizontalScrollView {
6964
}
7065

7166
override fun measureChild(child: View, parentWidthMeasureSpec: Int, parentHeightMeasureSpec: Int) {
72-
val childWidthMeasureSpec = makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
73-
val childHeightMeasureSpec = makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
74-
child.measure(childWidthMeasureSpec, childHeightMeasureSpec)
67+
val measureSpecZero = makeMeasureSpec(0)
68+
child.measure(measureSpecZero, measureSpecZero)
7569
}
7670

7771
override fun measureChildWithMargins(child: View,
7872
parentWidthMeasureSpec: Int, widthUsed: Int,
7973
parentHeightMeasureSpec: Int, heightUsed: Int) {
8074
val params = child.layoutParams as MarginLayoutParams
81-
82-
val childWidthMeasureSpec = makeMeasureSpec(
83-
params.leftMargin + params.rightMargin, MeasureSpec.UNSPECIFIED)
84-
val childHeightMeasureSpec = makeMeasureSpec(
85-
params.topMargin + params.bottomMargin, MeasureSpec.UNSPECIFIED)
86-
75+
val childWidthMeasureSpec = makeMeasureSpec(params.leftMargin + params.rightMargin)
76+
val childHeightMeasureSpec = makeMeasureSpec(params.topMargin + params.bottomMargin)
8777
child.measure(childWidthMeasureSpec, childHeightMeasureSpec)
8878
}
79+
80+
private fun makeMeasureSpec(size: Int) = makeMeasureSpec(size, MeasureSpec.UNSPECIFIED)
8981
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
5-
android:layout_height="wrap_content">
6+
android:layout_height="@dimen/line_height">
67

78
<TextView
89
android:id="@+id/tv_line_num"
910
android:layout_width="@dimen/line_num_width"
10-
android:layout_height="@dimen/line_height"
11+
android:layout_height="match_parent"
1112
android:gravity="center"
12-
android:fontFamily="monospace"
1313
android:textSize="@dimen/line_text_size"
14-
android:text="@string/stub_line_num"/>
14+
tools:text="@string/stub_line_num"/>
1515

1616
<TextView
1717
android:id="@+id/tv_line_content"
1818
android:layout_width="wrap_content"
19-
android:layout_height="@dimen/line_height"
19+
android:layout_height="match_parent"
2020
android:layout_marginLeft="16dp"
2121
android:layout_marginRight="16dp"
2222
android:layout_toRightOf="@+id/tv_line_num"
2323
android:gravity="center_vertical"
24-
android:fontFamily="monospace"
25-
android:singleLine="true"
24+
android:maxLines="1"
2625
android:textSize="@dimen/line_text_size"
27-
android:text="@string/stub_line_content"/>
26+
tools:text="@string/stub_line_content"/>
2827

2928
<LinearLayout
3029
android:id="@+id/ll_line_footer"

codeview/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<resources>
33
<dimen name="line_num_width">32dp</dimen>
44
<dimen name="line_height">24dp</dimen>
5+
<dimen name="line_border_height">6dp</dimen>
56
<dimen name="line_text_size">12sp</dimen>
67
</resources>

0 commit comments

Comments
 (0)