Guest User

Untitled

a guest
Mar 6th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.15 KB | None | 0 0
  1. class PageBarFragment: Fragment() {
  2.     ...
  3.     private val pageTextView: TextView get() = buildPageTextView()
  4.  
  5.     private fun buildPageTextView(): TextView {
  6.         val pageTextView = TextView(context)
  7.         pageTextView.setOnClickListener {
  8.             currentPage = pageTextView.text.toString().toInt()
  9.             changePage(currentPage)
  10.         }
  11.  
  12.         val params = RelativeLayout.LayoutParams(
  13.             ViewGroup.LayoutParams.WRAP_CONTENT,
  14.             ViewGroup.LayoutParams.MATCH_PARENT)
  15.         params.rightMargin = 8
  16.         pageTextView.layoutParams = params
  17.         pageTextView.textAlignment = View.TEXT_ALIGNMENT_CENTER
  18.         pageTextView.textSize = 20.toFloat()
  19.  
  20.         val textColor = ContextCompat.getColor(requireContext(), R.color.colorText)
  21.         pageTextView.setTextColor(textColor)
  22.         return pageTextView
  23.     }
  24.  
  25.     fun updatePageList() {
  26.         binding.pageList.removeAllViewsInLayout()
  27.         ...
  28.  
  29.         fun addPagesRange(start: Int, end: Int) {
  30.             for (page in start..end) {
  31.                 val pageView = pageTextView
  32.                 pageView.text = page.toString()
  33.                 if (page == currentPage) {
  34.                     val accentColor = ContextCompat.getColor(requireContext(), R.color.colorLinkText)
  35.                     pageView.setTextColor(accentColor)
  36.                 }
  37.                 binding.pageList.addView(pageView)
  38.             }
  39.         }
  40.         ...
  41. }
  42.  
  43.  
  44.  
  45. <?xml version="1.0" encoding="utf-8"?>
  46. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  47.     xmlns:app="http://schemas.android.com/apk/res-auto"
  48.     xmlns:tools="http://schemas.android.com/tools"
  49.     android:layout_width="match_parent"
  50.     android:layout_height="wrap_content"
  51.     android:background="@color/colorPrimary">
  52.  
  53.     <LinearLayout
  54.         android:id="@+id/page_list"
  55.         android:layout_width="0dp"
  56.         android:layout_height="30dp"
  57.         android:layout_marginStart="8dp"
  58.         android:layout_marginTop="8dp"
  59.         android:layout_marginEnd="8dp"
  60.         android:layout_marginBottom="8dp"
  61.         android:orientation="horizontal"
  62.         app:layout_constraintBottom_toBottomOf="parent"
  63.         app:layout_constraintEnd_toStartOf="@+id/next_page_button"
  64.         app:layout_constraintHorizontal_bias="0.0"
  65.         app:layout_constraintStart_toEndOf="@+id/prev_page_button"
  66.         app:layout_constraintTop_toTopOf="parent"
  67.         app:layout_constraintVertical_bias="1.0" >
  68.     </LinearLayout>
  69.  
  70.     <ImageView
  71.         android:id="@+id/prev_page_button"
  72.         android:layout_width="30dp"
  73.         android:layout_height="30dp"
  74.         android:layout_marginStart="8dp"
  75.         android:layout_marginTop="8dp"
  76.         android:layout_marginBottom="8dp"
  77.         android:textAlignment="center"
  78.         app:layout_constraintBottom_toBottomOf="parent"
  79.         app:layout_constraintStart_toStartOf="parent"
  80.         app:layout_constraintTop_toTopOf="parent"
  81.         app:layout_constraintVertical_bias="1.0"
  82.         app:srcCompat="@drawable/ic_arrow_left" />
  83.  
  84.     <ImageView
  85.         android:id="@+id/next_page_button"
  86.         android:layout_width="30dp"
  87.         android:layout_height="30dp"
  88.         android:layout_marginTop="8dp"
  89.         android:layout_marginEnd="8dp"
  90.         android:layout_marginBottom="8dp"
  91.         android:textAlignment="center"
  92.         app:layout_constraintBottom_toBottomOf="parent"
  93.         app:layout_constraintEnd_toEndOf="parent"
  94.         app:layout_constraintTop_toTopOf="parent"
  95.         app:layout_constraintVertical_bias="1.0"
  96.         app:srcCompat="@drawable/ic_arrow_right" />
  97.  
  98.     <EditText
  99.         android:id="@+id/page_number_field"
  100.         android:layout_width="54dp"
  101.         android:layout_height="32dp"
  102.         android:layout_marginTop="8dp"
  103.         android:layout_marginBottom="8dp"
  104.         android:ems="10"
  105.         android:inputType="number"
  106.         app:layout_constraintBottom_toTopOf="@+id/page_list"
  107.         app:layout_constraintEnd_toEndOf="parent"
  108.         app:layout_constraintStart_toStartOf="parent"
  109.         app:layout_constraintTop_toTopOf="parent" />
  110.  
  111. </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment