Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-125: search auto scroll #319

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pluto.plugins.network.internal.interceptor.ui

import android.os.Bundle
import android.os.Parcelable
import android.text.Layout
import android.view.View
import android.view.View.VISIBLE
import androidx.core.view.isVisible
Expand Down Expand Up @@ -55,6 +56,8 @@ internal class ContentFragment : Fragment(R.layout.pluto_network___fragment_cont
append("\n")
}
}

scrollToText(search.trim())
}
}
}
Expand Down Expand Up @@ -85,6 +88,32 @@ internal class ContentFragment : Fragment(R.layout.pluto_network___fragment_cont
}
}

/**
* helps to auto scroll to target search
*/
private fun scrollToText(targetText: String) {
if (targetText.isEmpty()) {
return
}

val contentText = binding.content.getText().toString().lowercase()
val index = contentText.indexOf(targetText.lowercase())

if (index != -1) {
binding.content.post {
val layout: Layout? = binding.content.layout
if (layout != null) {
val lineNumber = layout.getLineForOffset(index)
val x = layout.getPrimaryHorizontal(index.plus(targetText.length)).toInt()
val y = layout.getLineTop(lineNumber)

binding.horizontalScroll.smoothScrollTo(x / 2, 0)
binding.contentNestedScrollView.smoothScrollTo(0, y / 2)
}
}
}
}

companion object {
internal const val DATA = "data"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<androidx.core.widget.NestedScrollView
android:id="@+id/content_nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
Expand Down
Loading