N
N
neitoo2022-04-18 20:22:01
Android
neitoo, 2022-04-18 20:22:01

Why does clicking on one card in RecyclerView open another card?

I have a RecyclerView to which ad cards are added. For example, at the moment 3 (for convenience, let's call them A, B, C) cards in the form of a tape have been added. One and a half cards can fit on the screen at the same time, for example, I click on A - B opens, scrolling through the tape I click on B - C opens. So, how can this be fixed? OnClick is attached to the card and through it I open a new activity, if it matters. This problem is solved only if the card is stretched to full screen, then the desired activity opens, but this solution is not what I need ...
Feed XML:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#373745"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/settingButton"
        android:layout_width="162dp"
        android:layout_height="57dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/button"
        android:backgroundTint="#6C63FF"
        android:fontFamily="@font/comfortaa"
        android:isScrollContainer="false"
        android:text="Фильтры"
        android:textColor="#ffffff"
        android:textSize="12sp"
        android:textStyle="bold"
        app:backgroundTint="@null"
        app:icon="@drawable/control_2"
        app:iconGravity="textStart"
        app:iconSize="30dp"
        app:iconTint="#FFFFFF"
        app:iconTintMode="src_atop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="SpeakableTextPresentCheck" />

    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="32dp"
        android:scrollbars="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="0dp"
        android:layout_height="58dp"
        android:background="@drawable/rectangle_rounded_some"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.6"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <Button
            android:id="@+id/addAnAd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button"
            android:backgroundTint="#6C63FF"
            android:fontFamily="@font/comfortaa"
            android:text="Подать объявление"
            android:textColor="#ffffff"
            android:textSize="12sp"

            android:textStyle="bold"
            app:backgroundTint="@null"
            app:icon="@drawable/home"
            app:iconSize="30dp"
            app:iconTint="#ffffff"
            app:iconTintMode="src_atop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>



</androidx.constraintlayout.widget.ConstraintLayout>


Card Template XML:
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="20dp"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp"
    app:cardElevation="15dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#3d3d4d"
        android:onClick="SelectInfoAd"
        android:paddingLeft="20dp"
        android:paddingTop="15dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp">

        <TextView
            android:id="@+id/costHouseAd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/comfortaa"
            android:text="20 000 Р"
            android:textColor="@color/white"
            android:textSize="20dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/locationAd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/costHouseAd"
            android:fontFamily="@font/comfortaa"
            android:paddingBottom="6dp"
            android:text="location"
            android:textColor="#9EFFFFFF"
            android:textSize="15dp" />

        <ImageView
            android:id="@+id/house_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@id/locationAd"
            android:background="@drawable/input_design"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/cropInfoAd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/house_image"
            android:fontFamily="@font/comfortaa"
            android:paddingTop="6dp"
            android:paddingBottom="20dp"
            android:text="info"
            android:textColor="@color/white"
            android:textSize="17dp" />

    </RelativeLayout>

</com.google.android.material.card.MaterialCardView>


Activity screenshot:
https://imgur.com/a/AjkZdX3

Code with adding a card to the screen and onClick:
override fun onStart() {
        super.onStart()


        val options = FirebaseRecyclerOptions.Builder<Houses>()
            .setQuery(HouseReference!!, Houses::class.java).build()

        val adapter: FirebaseRecyclerAdapter<Houses, HouseViewHolder> =
            object : FirebaseRecyclerAdapter<Houses, HouseViewHolder>(options) {
                @SuppressLint("SetTextI18n")
                override fun onBindViewHolder(@NotNull holder: HouseViewHolder, i: Int, @NotNull model: Houses) {
                    Log.v("adapter","222")
                    holder.txtCost.text = ("₽"+ model.getCostV())

                    holder.txtLocation.text = model.getLocationV()

                    val lengthInfo = holder.txtInfo.text.length
                    if (lengthInfo < 40) holder.txtInfo.text = model.getInfoV().substring(0,40).replaceRange(37,40,"...")
                    else holder.txtInfo.text = model.getInfoV()

                    Picasso.get().load(model.getImage()).into(holder.imageView)

                    cost = ("₽"+ model.getCostV())
                    location = (model.getLocationV())
                    room= (model.getRoomsV())
                    square = (model.getSquareV())
                    info = (model.getInfoV())
                    number = (model.getNumberV())
                    image = (model.getImage())
                }

                @NotNull
                override fun onCreateViewHolder(@NotNull parent: ViewGroup, viewType: Int): HouseViewHolder {
                    Log.v("viewholder","222")
                    val view = LayoutInflater.from(parent.context)
                        .inflate(R.layout.house_image_layout, parent, false)
                    return HouseViewHolder(view)
                }
            }
        recyclerView!!.adapter = adapter
        adapter.startListening()
    }


    fun SelectInfoAd(view: View){

        val IntentInfoAd = Intent(this@MainActivity, InfoMenuAd::class.java)
        IntentInfoAd.putExtra(Constant.COST, cost)
        IntentInfoAd.putExtra(Constant.LOCATION, location)
        IntentInfoAd.putExtra(Constant.ROOM, room)
        IntentInfoAd.putExtra(Constant.SQUARE, square)
        IntentInfoAd.putExtra(Constant.INFO, info)
        IntentInfoAd.putExtra(Constant.NUMBER, number)
        IntentInfoAd.putExtra(Constant.IMAGE, image)
        startActivity(IntentInfoAd)


    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacen11, 2022-04-19
@neitoo

been looking for a click handler for a long time

android:onClick="SelectInfoAd"

they don’t do that, it’s now accepted to process pressing differently. It is necessary to hang up a licener on a view in the code. In the viewholdor, you will have
a very eye-catching code, read the style code, for example, in Java from Google. They don’t write like that in Kotlin, but it doesn’t correspond to Java either. See what hotkey auto-formats the code in the studio

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question