Android Studio ItemOnClickListener存在项目过滤器和XML问题

rqenqsqc  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(131)

我有一个问题reagrding在Android Studio中的RecyclerViews。我希望能够启动一个新的活动项目点击并传递一些东西,以便能够识别它在我的常量文件夹中,以获得有关该项目的所有信息。位置是不够的,因为我已经实现了一个项目过滤器,改变项目的位置从过滤器到过滤器。代码是有点混乱,我是一个纽比,代码是不干净的。
具有回收视图的片段:
这是重定向的部分:

override fun onItemClick(position: Int) {
                val intent = Intent(context, ExerciseActivity::class.java)
                Constants.PositionItemListener = position
                intent.putExtra("Position", Constants.PositionItemListener)
                startActivity(intent)
            }
        })

这是整个代码的片段:

package ch.skimfit.skimfitapp

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import ch.skimfit.skimfitapp.databinding.ActivityMainBinding

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

private lateinit var adapter : RecyclerViewAdapterTraining
private lateinit var recyclerView: RecyclerView
private lateinit var exerciseList:ArrayList<Exercises>
private lateinit var tempArrayList:ArrayList<Exercises>

lateinit var exerciseImage:Array<Int>
lateinit var exerciseName:Array<String>

/**
 * A simple [Fragment] subclass.
 * Use the [home.newInstance] factory method to
 * create an instance of this fragment.
 */
class home : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private lateinit var content:ArrayList<Exercises>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment home.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            home().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dataInitialize()
        setupFilter()
        val layoutManager = LinearLayoutManager(context)
        recyclerView = view.findViewById(R.id.rv_All)
        recyclerView.layoutManager = layoutManager
        recyclerView.setHasFixedSize(true)
        var adapter = RecyclerViewAdapterTraining(tempArrayList)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(object : RecyclerViewAdapterTraining.onItemClickListerner{
            override fun onItemClick(position: Int) {
                val intent = Intent(context, ExerciseActivity::class.java)
                Constants.PositionItemListener = position
                intent.putExtra("Position", Constants.PositionItemListener)
                startActivity(intent)
            }
        })
    }

    private fun setupFilter() {
        var rbAll: RadioButton? = view?.findViewById(R.id.rbAll)
        var rbPush:RadioButton? = view?.findViewById(R.id.rbPush)
        var rbPull:RadioButton? = view?.findViewById(R.id.rbPull)
        var rbLeg:RadioButton? = view?.findViewById(R.id.rbLeg)

        rbAll?.setOnClickListener{
            tempArrayList.clear()
            exerciseList.forEach {
                if(it.Category.contains("exercise")){
                    tempArrayList.add(it)
                }
            }
            recyclerView.adapter!!.notifyDataSetChanged()

        }
        rbPush?.setOnClickListener{
            tempArrayList.clear()
            exerciseList.forEach {
                if(it.Category.contains("push")){
                    tempArrayList.add(it)
                }
            }
            recyclerView.adapter!!.notifyDataSetChanged()
        }
        rbPull?.setOnClickListener{
            tempArrayList.clear()
            exerciseList.forEach {
                if(it.Category.contains("pull")){
                    tempArrayList.add(it)
                }
            }
            recyclerView.adapter!!.notifyDataSetChanged()
        }
        rbLeg?.setOnClickListener{
            tempArrayList.clear()
            exerciseList.forEach {
                if(it.Category.contains("leg")){
                    tempArrayList.add(it)
                }
            }
            recyclerView.adapter!!.notifyDataSetChanged()
        }
    }

    private fun dataInitialize(){
        exerciseList = Constants.getExerciseList()
        tempArrayList = Constants.getExerciseList()

    }
}

常量:

package ch.skimfit.skimfitapp

object Constants {

    var PositionItemListener: Int = 0

    fun getExerciseList(): ArrayList<Exercises> {
        val exerciseList = ArrayList<Exercises>()

        val exe1 = Exercises(1,"Push exercise 1", R.drawable.training_recyclerview, "Test", "Test1", 0, 0, "pushexercise")
        exerciseList.add(exe1)
        val exe2 = Exercises(2,"Pull exercise 1", R.drawable.training_recyclerview, "Test", "Test2", 0, 0,"pullexercise")
        exerciseList.add(exe2)
        val exe3 = Exercises(3,"Pull exercise 2", R.drawable.training_recyclerview, "Test", "Test3", 0, 0, "pullexercise")
        exerciseList.add(exe3)
        val exe4 = Exercises(4,"Pull exercise 3", R.drawable.training_recyclerview, "Test", "Test4", 0, 0, "pullexercise")
        exerciseList.add(exe4)
        val exe5 = Exercises(5,"leg exercise 1", R.drawable.training_recyclerview, "Test", "Test5", 0, 0, "legexercise")
        exerciseList.add(exe5)
        val exe6 = Exercises(6,"leg exercise 2", R.drawable.training_recyclerview, "Test", "Test6", 0, 0, "legexercise")
        exerciseList.add(exe6)

        return exerciseList
    }
}

还有一个问题:
如果我将框架布局约束到父顶部,为什么xml格式的框架布局不能连接到完整的顶部

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/BackgroundMain"
    android:paddingTop="?attr/actionBarSize">

    <FrameLayout
        android:id="@+id/fl_main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/bottom_nav_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

    </FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/BottomNavBarColor"
        app:itemIconTint="@drawable/bottom_nav_bar_color_selected"
        app:itemTextColor="@drawable/bottom_nav_bar_color_selected"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

我希望它像红线一样:enter image description here

kcwpcxri

kcwpcxri1#

找到了一个适合我的解决方案。可能不是最好的一个,但正在工作。我现在只是传入用户当前所在的类别,以便在我的接收活动中设置相同的tempList。但仍然没有xml问题的解决方案。编辑:好的,在xml中发现了这个问题,只是在顶部有一个ActionBarSize的填充。

相关问题