Change main layout to LinearLayout

This commit is contained in:
Rafał Borcz 2019-06-22 20:25:03 +02:00
parent 42cbec6ea5
commit 7379fb697e
4 changed files with 49 additions and 50 deletions

View file

@ -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<LinearLayout.LayoutParams> {
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() {

View file

@ -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<MaterialChipItem>()
@ -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)

View file

@ -2,11 +2,15 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
android:orientation="vertical">
<io.github.wulkanowy.materialchipsinput.MaterialChipInput
android:id="@+id/mainChipsInput"
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="match_parent">
<io.github.wulkanowy.materialchipsinput.MaterialChipInput
android:id="@+id/mainChipsInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.core.widget.NestedScrollView>
</LinearLayout>

View file

@ -1,11 +1,8 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<!-- Customize your theme here. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>