diff --git a/library/src/main/java/io/github/wulkanowy/materialchipsinput/DropdownListView.kt b/library/src/main/java/io/github/wulkanowy/materialchipsinput/DropdownListView.kt index d8b92dc..2744498 100644 --- a/library/src/main/java/io/github/wulkanowy/materialchipsinput/DropdownListView.kt +++ b/library/src/main/java/io/github/wulkanowy/materialchipsinput/DropdownListView.kt @@ -2,25 +2,21 @@ package io.github.wulkanowy.materialchipsinput import android.content.Context -import android.content.res.Configuration.ORIENTATION_PORTRAIT import android.graphics.Rect import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION_CODES.JELLY_BEAN import android.util.AttributeSet import android.view.KeyEvent import android.view.KeyEvent.KEYCODE_BACK -import android.view.ViewGroup import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import android.view.ViewTreeObserver import android.view.animation.AlphaAnimation -import android.widget.RelativeLayout -import android.widget.RelativeLayout.ALIGN_PARENT_LEFT -import android.widget.RelativeLayout.ALIGN_PARENT_TOP -import androidx.core.view.marginLeft -import androidx.core.view.updateMargins +import android.widget.LinearLayout +import androidx.core.view.updateLayoutParams import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView -import io.github.wulkanowy.materialchipsinput.util.navBarHeight +import io.github.wulkanowy.materialchipsinput.util.convertDpToPixels internal class DropdownListView : RecyclerView { @@ -48,17 +44,9 @@ internal class DropdownListView : RecyclerView { chipInput.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { - val layoutParams = RelativeLayout.LayoutParams(context.resources.displayMetrics.widthPixels, MATCH_PARENT) - .apply { - addRule(ALIGN_PARENT_TOP) - addRule(ALIGN_PARENT_LEFT) + val layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT) - if (context.resources.configuration.orientation == ORIENTATION_PORTRAIT) { - bottomMargin = context.navBarHeight - } - } - - (chipInput.rootView as ViewGroup).addView(this@DropdownListView, layoutParams) + chipInput.addView(this@DropdownListView, layoutParams) if (SDK_INT < JELLY_BEAN) { @Suppress("DEPRECATION") @@ -95,20 +83,24 @@ internal class DropdownListView : RecyclerView { private fun fadeIn() { if (visibility == VISIBLE) return - val rect = Rect() - rootView.getWindowVisibleDisplayFrame(rect) + val visibleRect = Rect().apply { + rootView.getWindowVisibleDisplayFrame(this) + top = 0 + } - val coordinators = IntArray(2) - chipInput.getLocationInWindow(coordinators) + val coordinators = IntArray(2).apply { + chipInput.getLocationOnScreen(this) + } + + updateLayoutParams { + val defaultHeight = context.convertDpToPixels(72f).toInt() + val calculatedHeight = visibleRect.height() - (coordinators[1] + chipInput.height) + + height = if (calculatedHeight < defaultHeight) defaultHeight else calculatedHeight + } - (layoutParams as MarginLayoutParams).updateMargins( - top = coordinators[1] + chipInput.height, - bottom = rootView.height - rect.bottom, - left = if (rect.left > 0) rect.left else marginLeft - ) - requestLayout() - startAnimation(AlphaAnimation(0.0f, 1.0f).apply { duration = 200 }) visibility = VISIBLE + startAnimation(AlphaAnimation(0.0f, 1.0f).apply { duration = 200 }) } private fun fadeOut() { diff --git a/library/src/main/java/io/github/wulkanowy/materialchipsinput/MaterialChipInput.kt b/library/src/main/java/io/github/wulkanowy/materialchipsinput/MaterialChipInput.kt index bd1b35f..570207e 100644 --- a/library/src/main/java/io/github/wulkanowy/materialchipsinput/MaterialChipInput.kt +++ b/library/src/main/java/io/github/wulkanowy/materialchipsinput/MaterialChipInput.kt @@ -10,18 +10,22 @@ import android.view.KeyEvent import android.view.KeyEvent.ACTION_DOWN import android.view.KeyEvent.KEYCODE_DEL import android.view.MotionEvent +import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import android.view.inputmethod.EditorInfo.IME_FLAG_NO_EXTRACT_UI +import android.widget.LinearLayout import androidx.core.view.setPadding import com.google.android.material.chip.Chip import com.google.android.material.chip.ChipGroup import io.github.wulkanowy.materialchipsinput.util.convertDpToPixels -class MaterialChipInput : ChipGroup { +class MaterialChipInput : LinearLayout { private val dropdownListView = DropdownListView(context) - private var chipEditText = MaterialChipEditText(context) + private val chipEditText = MaterialChipEditText(context) + + private val chipGroup = ChipGroup(context) private val insertedChipList = mutableListOf() @@ -70,13 +74,17 @@ class MaterialChipInput : ChipGroup { } - addView(chipEditText) - setChipSpacing(context.convertDpToPixels(8f).toInt()) + orientation = VERTICAL + addView(chipGroup, LayoutParams(MATCH_PARENT, WRAP_CONTENT)) + with(chipGroup) { + addView(chipEditText) + setChipSpacing(context.convertDpToPixels(8f).toInt()) + } } internal fun addChipOnLastPosition(chipItem: MaterialChipItem) { insertedChipList.add(chipItem) - addView(Chip(context).apply { text = chipItem.title }, childCount - 1) + chipGroup.addView(Chip(context).apply { text = chipItem.title }, chipGroup.childCount - 1) dropdownListView.dropdownListViewAdapter.removeItem(chipItem) with(chipEditText) { @@ -89,7 +97,7 @@ class MaterialChipInput : ChipGroup { val chipItem = insertedChipList.elementAt(insertedChipList.size - 1) insertedChipList.remove(chipItem) - removeViewAt(childCount - 2) + chipGroup.removeViewAt(chipGroup.childCount - 2) dropdownListView.dropdownListViewAdapter.addItem(chipItem) if (insertedChipList.isEmpty()) chipEditText.hint = "Hint" @@ -105,16 +113,14 @@ class MaterialChipInput : ChipGroup { val editHitRect = Rect() chipEditText.getHitRect(editHitRect) - val recyclerHitRect = Rect() - getHitRect(recyclerHitRect) + val chipGroupHitRect = Rect() + chipGroup.getHitRect(chipGroupHitRect) - val extendedHitRect = Rect(editHitRect.right, editHitRect.top, recyclerHitRect.right, editHitRect.bottom) + val extendedHitRect = Rect(editHitRect.right, editHitRect.top, chipGroupHitRect.right, editHitRect.bottom) event?.let { if (extendedHitRect.contains(it.x.toInt(), it.y.toInt())) { - chipEditText.apply { - isHandled = chipEditText.dispatchTouchEvent(it) - } + isHandled = chipEditText.dispatchTouchEvent(it) } } return if (isHandled) true else super.dispatchTouchEvent(event) diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index d92cb40..d6c9dff 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -2,11 +2,15 @@ + android:orientation="vertical"> - + android:layout_height="match_parent"> + + + diff --git a/sample/src/main/res/values/styles.xml b/sample/src/main/res/values/styles.xml index d6e57fe..bbfdd91 100644 --- a/sample/src/main/res/values/styles.xml +++ b/sample/src/main/res/values/styles.xml @@ -1,11 +1,8 @@ - - -