Open fragment by clicking on Cardview

Asked

Viewed 219 times

0

My home screen has 3 cardviews:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:weightSum="10"
    tools:context=".MainActivity">

    <GridLayout
        android:id="@+id/mainGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:alignmentMode="alignMargins"
        android:background="#eeeeee"
        android:columnCount="1"
        android:columnOrderPreserved="true"
        android:padding="14dp"
        android:rowCount="3">

        <android.support.v7.widget.CardView
            android:id="@+id/btnCursos"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:backgroundTint="#222831"
            app:cardCornerRadius="8dp"
            app:cardElevation="2dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/ic_menu_curso"
                    app:srcCompat="@drawable/ic_menu_curso_branco" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/menu_cursos"
                    android:textAlignment="center"
                    android:textColor="#FFFFFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/btnComunicados"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:backgroundTint="#393e46"
            app:cardCornerRadius="8dp"
            app:cardElevation="2dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/ic_menu_comunicados"
                    app:srcCompat="@drawable/ic_menu_comunicados_branco" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/menu_comunicados"
                    android:textAlignment="center"
                    android:textColor="#FFFFFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/btnSobre"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:backgroundTint="#00adb5"
            app:cardCornerRadius="8dp"
            app:cardElevation="2dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/ic_menu_sobre"
                    app:srcCompat="@drawable/ic_menu_sobre_branco" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/menu_sobre"
                    android:textAlignment="center"
                    android:textColor="#FFFFFF"
                    android:textSize="16sp"
                    android:textStyle="bold" />

            </LinearLayout>

        </android.support.v7.widget.CardView>

    </GridLayout>

</LinearLayout>

This layout is linked with this java class:

public class PrincipalFragment extends Fragment {

    View minhaView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        minhaView = inflater.inflate(R.layout.principal, container, false);
        getActivity().setTitle("Principal");
        return minhaView;
    }
}

I need to make cardview, when selected, call another fragment.

Somebody help me please, I’ve looked everywhere and I can’t find answers.

1 answer

1


You can add a Cardview click listener according to his ID and then open your Fragment.

Your Cardview:

<android.support.v7.widget.CardView
    android:id="@+id/cvSobre"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp"
    android:backgroundTint="#00adb5"
    app:cardCornerRadius="8dp"
    app:cardElevation="2dp"/>

Your class:

public class PrincipalFragment extends Fragment {
    View minhaView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        minhaView = inflater.inflate(R.layout.fragment_principal, container, false);
        getActivity().setTitle("Principal");
        CardView cvSobre = minhaView.findViewById(R.id.cvSobre);
        // onClickListener para quando selecionar um CardView
        cvSobre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Lógica para abrir seu Fragment, ex.:
                getFragmentManager()
                .beginTransaction()
                .replace(R.id.fragmentContainer, new SobreFragment())
                .commit();
            }
        });
        return minhaView;
    }

}
  • Damn it, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.