-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from inu-appcenter/feat/spinner
스피너(홈화면 카테고리, 필터) 수정 및 홈화면에서 검색 화면 들어가도록 구현
- Loading branch information
Showing
16 changed files
with
239 additions
and
117 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
app/src/main/java/org/inu/events/lib/spinner/UniSpinner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.inu.events.lib.spinner | ||
|
||
import android.content.Context | ||
import android.util.AttributeSet | ||
import android.widget.ArrayAdapter | ||
import android.widget.FrameLayout | ||
import android.widget.ListPopupWindow | ||
import android.widget.TextView | ||
import androidx.constraintlayout.widget.ConstraintLayout | ||
import org.inu.events.R | ||
|
||
class UniSpinner @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyleAttr: Int = 0 | ||
) : ConstraintLayout(context, attrs, defStyleAttr) { | ||
|
||
private lateinit var items: Array<CharSequence> | ||
private lateinit var selectedText: TextView | ||
private val popUp = ListPopupWindow(context) | ||
private lateinit var adapter: ArrayAdapter<CharSequence> | ||
|
||
init { | ||
inflate(context, R.layout.uni_spinner, this) | ||
|
||
setAttrs(attrs) | ||
setAdapter() | ||
setPopup() | ||
setSelectedText() | ||
} | ||
|
||
private fun setAttrs(attrs: AttributeSet?) { | ||
context.obtainStyledAttributes(attrs, R.styleable.UniSpinner).run { | ||
items = getTextArray(R.styleable.UniSpinner_items) ?: arrayOf("전체", "간식나눔", "공고") | ||
recycle() | ||
} | ||
} | ||
|
||
|
||
private fun setAdapter() { | ||
adapter = ArrayAdapter(context, R.layout.uni_spinner_item, items) | ||
popUp.setAdapter(adapter) | ||
} | ||
|
||
private fun measureWidth(): Int { | ||
var width = 0 | ||
for (i in items.indices) { | ||
val view = adapter.getView(i, null, FrameLayout(context)) | ||
view.measure(0, 0) | ||
width = width.coerceAtLeast(view.measuredWidth) | ||
} | ||
return width | ||
} | ||
|
||
private fun setSelectedText() { | ||
selectedText = findViewById<TextView>(R.id.selected_item).apply { | ||
text = items.first() | ||
width = popUp.width | ||
setOnClickListener { | ||
if (popUp.isShowing) { | ||
popUp.dismiss() | ||
} else { | ||
popUp.show() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun setPopup() { | ||
popUp.apply { | ||
isModal = true | ||
anchorView = this@UniSpinner | ||
verticalOffset = this.height + DISTANCE_POPUP_TITLE | ||
width = measureWidth() + MARGIN_END_POPUP_ITEM | ||
setBackgroundDrawable(context.getDrawable(R.drawable.popup_background)) | ||
} | ||
|
||
} | ||
|
||
fun setOnItemClick(onClick: (pos:Int) -> Unit) { | ||
popUp.setOnItemClickListener { _, _, position, _ -> | ||
selectedText.text = items[position] | ||
popUp.dismiss() | ||
onClick(position) | ||
} | ||
} | ||
|
||
companion object { | ||
const val DISTANCE_POPUP_TITLE = 8 * 3 | ||
const val MARGIN_END_POPUP_ITEM = 16 * 3 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/org/inu/events/ui/binding/UniSpinnerBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.inu.events.ui.binding | ||
|
||
import androidx.databinding.BindingAdapter | ||
import org.inu.events.lib.spinner.UniSpinner | ||
|
||
@BindingAdapter("setOnItemClick") | ||
fun bindSetOnItemClick(view: UniSpinner, onClick: (position: Int) -> Unit) { | ||
view.setOnItemClick { position -> | ||
onClick(position) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 35 additions & 35 deletions
70
app/src/main/java/org/inu/events/ui/home/SpinnerAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
package org.inu.events.ui.home | ||
|
||
import android.content.Context | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ArrayAdapter | ||
import androidx.annotation.LayoutRes | ||
import org.inu.events.databinding.UniSpinnerBinding | ||
import org.inu.events.databinding.UniSpinnerPopupBinding | ||
|
||
class SpinnerAdapter( | ||
context: Context, | ||
@LayoutRes private val resId: Int, | ||
private val list: MutableList<String>, | ||
private val spinnerTitle: String | ||
) : ArrayAdapter<String>(context, resId, list) { | ||
override fun getCount(): Int = list.size | ||
override fun getItem(position: Int): String = list[position] | ||
|
||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { | ||
val binding = UniSpinnerBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
binding.selectedItem.text = if (position == 0) spinnerTitle else list[position] | ||
|
||
return binding.root | ||
} | ||
|
||
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View { | ||
val binding = UniSpinnerPopupBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
val model = list[position] | ||
binding.item.text = model | ||
|
||
return binding.root | ||
} | ||
} | ||
//package org.inu.events.ui.home | ||
// | ||
//import android.content.Context | ||
//import android.view.LayoutInflater | ||
//import android.view.View | ||
//import android.view.ViewGroup | ||
//import android.widget.ArrayAdapter | ||
//import androidx.annotation.LayoutRes | ||
//import org.inu.events.databinding.UniSpinnerBinding | ||
//import org.inu.events.databinding.UniSpinnerPopupBinding | ||
// | ||
//class SpinnerAdapter( | ||
// context: Context, | ||
// @LayoutRes private val resId: Int, | ||
// private val list: MutableList<String>, | ||
// private val spinnerTitle: String | ||
//) : ArrayAdapter<String>(context, resId, list) { | ||
// override fun getCount(): Int = list.size | ||
// override fun getItem(position: Int): String = list[position] | ||
// | ||
// override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { | ||
// val binding = UniSpinnerBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
// binding.selectedItem.text = if (position == 0) spinnerTitle else list[position] | ||
// | ||
// return binding.root | ||
// } | ||
// | ||
// override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View { | ||
// val binding = UniSpinnerPopupBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
// val model = list[position] | ||
// binding.item.text = model | ||
// | ||
// return binding.root | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.