Android App开发滑块菜单/弹出-----需要帮助

时间:2018-06-05 02:19:37

标签: android-studio kotlin slider

我正在尝试创建一个从右侧滑入的滑块菜单,并允许我从按钮列表中选择一个项目。选择该项目后,该滑块将消失,并显示包含该部分中所有信息的新滑块。例。如果侧面菜单按钮显示食物和下一杯饮料。如果我选择食物,它将显示包含各种项目的食物菜单。如果我选择披萨,它会让菜单消失并滑出一个新菜单,显示披萨的所有内容。对不起,Android应用开发新手。这就是我到目前为止......我需要所有可能的帮助。我现在只和Kotlin一起工作。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".menu">

    <Button
        android:id="@+id/food"
        android:layout_width="34dp"
        android:layout_height="257dp"
        android:text="Food"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/drinks"
        tools:layout_editor_absoluteX="2dp" />

    <Button
        android:id="@+id/drinks"
        android:layout_width="34dp"
        android:layout_height="257dp"
        android:text="Drinks"
        app:layout_constraintBottom_toTopOf="@+id/food"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="2dp" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

我在我的主人中创建了程序设计的键盘。它看起来像(图片上的字母不是英文,但我想,你理解这一点): enter image description here五月,你觉得它很有帮助。

键盘是水平的recyclerView。看这个。 (你可以简化它) 的适配器

class KeyboardAdapter(context: Context, private var listener: PressKeyboardButton) : RecyclerView.Adapter<ItemKeyboardViewHolder>() {
    private var keyboard: ArrayList<KeyBtn> = ArrayList()
    private var inflater: LayoutInflater = LayoutInflater.from(context)

    init {
        initKeyboard()
    }

    override fun onBindViewHolder(holder: ItemKeyboardViewHolder, position: Int) {
        holder.bindTo(keyboard[position])
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ItemKeyboardViewHolder {
        return ItemKeyboardViewHolder.create(inflater, viewGroup, listener)
    }

    override fun getItemCount(): Int {
        return keyboard.size
    }

    fun initKeyboard(){
        keyboard.clear()
        keyboard.add(KeyBtn("dote", ".", KeyBtn.ADD))
        keyboard.add(KeyBtn("0", "0", KeyBtn.ADD))
        keyboard.add(KeyBtn("1", "1", KeyBtn.ADD))
        keyboard.add(KeyBtn("backspace", "backspace", KeyBtn.BACKSPACE))
        keyboard.add(KeyBtn("delete", "delete", KeyBtn.DELETE))

        this.notifyDataSetChanged()
    }

    fun initKeyboard(numbers: ArrayList<CustomNumber>) {
        initKeyboard()

        // additional buttons
        for (number in numbers)      // Conversation format to Summation format (0.000000.000 -> 00.000000.000)
            keyboard.add(KeyBtn(number.comment, number.numberString, KeyBtn.REINIT))

        this.notifyDataSetChanged()
    }


    @SuppressLint("SetTextI18n")
    fun updateTextEdit(keyBtn: KeyBtn, numberField: EditText?, comment: TextView?, context: Context) {
        if(numberField != null) {
            val start = Math.max(numberField.selectionStart, 0)
            val end = Math.max(numberField.selectionEnd, 0)
            val length = numberField.text.toString().length

            when (keyBtn.typeButton) {
                KeyBtn.REINIT -> {
                    numberField.setText(keyBtn.data)
                    if (comment != null)
                        comment.text = "// ${keyBtn.text}"
                }
                KeyBtn.ADD -> {
                    numberField.text.replace(Math.min(start, end), Math.max(start, end),
                            keyBtn.data, 0, keyBtn.data.length)
                }
                KeyBtn.BACKSPACE -> {
                    numberField.text.replace(Math.max(0, start - 1), end, "", 0, 0)
                }
                KeyBtn.DELETE -> {
                    numberField.text.replace(start, Math.min(length, end + 1), "", 0, 0)
                }
            }

            val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
            if(Build.VERSION.SDK_INT < 26)
                oldVibrate(vibrator)
            else
                newVibrate(vibrator)
        }
    }

    @Suppress("DEPRECATION")
    private fun oldVibrate(vibrator: Vibrator){
        if(Build.VERSION.SDK_INT < 26)
            vibrator.vibrate(27)
    }

    private fun newVibrate(vibrator: Vibrator){
        if(Build.VERSION.SDK_INT >= 26) {
            val effect = VibrationEffect.createOneShot(27, VibrationEffect.DEFAULT_AMPLITUDE)
            vibrator.vibrate(effect)
        }
    }
}


interface PressKeyboardButton {
    fun simpleClick(keyBtn: KeyBtn, context: Context)
}

所以 ViewHolder

class ItemKeyboardViewHolder private constructor(private val view: View,
                                                 private val listener: PressKeyboardButton) :
        RecyclerView.ViewHolder(view), View.OnTouchListener {
    val itemKeyboard = view.itemKeyboard!!

    companion object {
        fun create(inflater: LayoutInflater,
                   parent: ViewGroup,
                   listener: PressKeyboardButton
        ): ItemKeyboardViewHolder {
            return ItemKeyboardViewHolder(
                    inflater.inflate(R.layout.item_keyboard, parent, false),
                    listener)
        }
    }

    @SuppressLint("SetTextI18n")
    fun bindTo(keyBtn: KeyBtn) {
        itemKeyboard.text = keyBtn.text
        view.setOnClickListener { listener.simpleClick(keyBtn, view.context) }
        view.setOnTouchListener(this)
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> itemKeyboard.isSelected = true
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> itemKeyboard.isSelected = false
        }

        return false
    }
}

所以布局:

<强> ViewHolder

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/itemLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/border"
    android:padding="16dp">

    <TextView
        android:id="@+id/itemKeyboard"
        style="@style/main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="A" />

</android.support.constraint.ConstraintLayout>

回收商视图

 <android.support.v7.widget.RecyclerView
    android:id="@+id/keyboard"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:background="@drawable/sign_in_gradient"
    android:orientation="horizontal"
    android:visibility="gone"
    app:layout_constraintBottom_toTopOf="@+id/bottomSpacer"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

完成init适配器:

// a custom simple keyboard
keyboardAdapter = KeyboardAdapter(this, object : PressKeyboardButton {
    override fun simpleClick(keyBtn: KeyBtn, context: Context) {
        val numberField: EditText? = currentFragment.recyclerViewAdapter?.getFocusedEditText()
        val commentField: TextView? = currentFragment.recyclerViewAdapter?.getFocusedCommentField()
        keyboardAdapter.updateTextEdit(keyBtn, numberField, commentField, context)
    }
})
val horizontalLayoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
keyboard.layoutManager = horizontalLayoutManager
keyboard.adapter = keyboardAdapter