【问题标题】:Android App Development Slider Menu/Pop Up ----- Need assistanceAndroid App Development Slider Menu/Pop Up ----- 需要帮助
【发布时间】:2018-06-05 02:19:36
【问题描述】:

我正在尝试创建一个从右侧滑入并允许我从按钮列表中选择一个项目的滑块菜单。选择该项目后,该滑块将消失,并显示包含该部分中所有信息的新滑块。例子。如果侧面菜单按钮显示食物和下一个饮料。如果我选择食物,它将显示一个包含各种项目的食物菜单。如果我选择比萨,它将使该菜单消失并滑出一个新菜单,显示有关比萨的所有信息。抱歉,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>

【问题讨论】:

    标签: android-studio kotlin slider


    【解决方案1】:

    我在我的大师中创建了程序化键盘。它看起来像(图片上的字母不是英文,但我想,你明白这一点): May,你觉得很有帮助。

    Keyboard 是水平的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" />
    

    以及整理的初始化适配器:

    // 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
    

    【讨论】:

    • 对不起,但这对我来说仍然存在。你能把它分解,哪些组件去哪里?我还在学习应用开发。
    • 对不起,我又读了一遍问题,明白了,我的答案不是正确的答案。您可以使用android-arsenal.com/tag/138?sort=created 吗?这个网站有很多不错的小型图书馆。检查它)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多