Я сделал CardView
в Fragment
.Но я также хочу, чтобы моя карта вызывала другое действие, когда пользователь нажимает на нее.
Я публикую часть своего кода ниже:
public class basic extends Fragment{
GridLayout mainGrid;
public basic() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_basic, container, false);
return v;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".basic">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<GridLayout
android:id="@+id/maingrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:columnCount="2"
android:padding="15dp"
android:rowCount="5">
<!-- Row1 -->
<!-- column1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:padding="0dp"
android:layout_rowWeight="1"
app:cardElevation="8dp">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAlignment="center"
android:textColor="@color/Aboutc"
android:textSize="18dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- column2 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:padding="0dp"
app:cardElevation="8dp">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAlignment="center"
android:textColor="@color/Aboutc"
android:textSize="18dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row1 -->
<!-- column1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:padding="0dp"
android:layout_rowWeight="1"
app:cardElevation="8dp">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAlignment="center"
android:textColor="@color/Aboutc"
android:textSize="18dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- column2 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:padding="0dp"
app:cardElevation="8dp">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAlignment="center"
android:textColor="@color/Aboutc"
android:textSize="18dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row1 -->
<!-- column1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:padding="0dp"
android:layout_rowWeight="1"
app:cardElevation="8dp">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAlignment="center"
android:textColor="@color/Aboutc"
android:textSize="18dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- column2 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:padding="0dp"
app:cardElevation="8dp">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAlignment="center"
android:textColor="@color/Aboutc"
android:textSize="18dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row1 -->
<!-- column1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:padding="0dp"
android:layout_rowWeight="1"
app:cardElevation="8dp">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAlignment="center"
android:textColor="@color/Aboutc"
android:textSize="18dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- column2 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:padding="0dp"
app:cardElevation="8dp">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_arrow_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAlignment="center"
android:textColor="@color/Aboutc"
android:textSize="18dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
</ScrollView>
</LinearLayout>