简体   繁体   中英

Open a fragment from a cardView

How can i open a fragment from onClick in CardView? i have a list in Cardview and when i clicked want to show in another fragment. This is the adapter.class

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView fecha_celula;
        public TextView nombre_celula;

        public ViewHolder(View itemView) {
            super(itemView);
            fecha_celula = (TextView) itemView.findViewById(R.id.fecha_celula);
            nombre_celula = (TextView) itemView.findViewById(R.id.nombre_celula);

            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            Fragment nameAdd = new name_add();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, predicaAdd).commit();
        }
    }

and this is the layout.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:clickable="true"
    android:layout_margin="10dp"
    android:id="@+id/card_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="2dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:duplicateParentState="true">

        <ImageView
            android:id="@+id/avatar_celula"
            android:src="@drawable/ic_action_celula"
            android:scaleType="centerCrop"
            android:layout_marginRight="10dp"
            android:layout_width="150dp"
            android:layout_height="150dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/fecha_celula"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/avatar_celula" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/nombre_celula"
            android:layout_below="@+id/fecha_celula"
            android:layout_toEndOf="@+id/avatar_celula" />


    </RelativeLayout>

I resolve the problem whith the onClick, now my problem is that i can't open another fragment. how i can call my method in myActivity?

The OnClickListener is not actually used anywhere, you have to connect it to a view, eg your itemView in the constructor.

itemView.setOnClickListener(this);

In the onClick() method you can then use getPosition() to get the index of the clicked element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM