片段中带有 CardView 的 GridLayout

GridLayout with CardView in Fragment

这是XML片段的片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@drawable/bg_grey_radial"
android:weightSum="10"
android:paddingTop="10dp"
android:paddingBottom="45dp"
>


<GridLayout
    android:id="@+id/mainGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="8"
    android:alignmentMode="alignMargins"
    android:columnCount="2"
    android:columnOrderPreserved="false"
    android:padding="14dp"
    android:rowCount="3"

    >


    <!-- Row 1 -->

    <!-- ITEM 1 -->
    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        app:cardBackgroundColor="@color/zxing_transparent"
        app:cardCornerRadius="8dp"
        app:cardElevation="0dp"
        android:id="@+id/cardview-item1">

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


            <ImageView
                android:id="@+id/icon1"
                android:layout_width="wrap_content"
                android:layout_height="55dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="8"
                android:src="@drawable/ic_diamond"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:elegantTextHeight="true"
                android:singleLine="true"
                android:text="ITEM1"
                android:textAlignment="center"
                android:textColor="@color/text"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@+id/icon7"
                android:paddingTop="15dp"
                android:lines="2"
                />


        </LinearLayout>

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

    <!-- Item 2 in Row 1 -->
    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"

        app:cardBackgroundColor="@color/zxing_transparent"
        app:cardCornerRadius="8dp"
        app:cardElevation="0dp">

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="55dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="8"
                android:src="@drawable/ic_diamond" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:elegantTextHeight="true"
                android:singleLine="true"
                android:text="ITEM 2"
                android:textAlignment="center"
                android:textColor="@color/text"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@+id/icon7"
                android:paddingTop="15dp"
                android:lines="2"
                />

        </LinearLayout>

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


    <!-- Row 2 -->

    <!-- Item 1 in Row 2 -->

这不是完整的 XML 代码,如您在屏幕截图中所见,它会自己重复。

我正在尝试为每个项目设置一个 OnClickListener,我还想在 Java 代码中单独更改每个项目的图标(稍后图像会加载 Picasso)。

 public fragment() {
        // Required empty public constructor
    }


public static fragment newInstance(String param1, String param2) {
    fragment = new fragment();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment, container, false);

    mainGrid = view.findViewById(R.id.mainGrid);
    //Set Event

    CardView infobase = view.findViewById(R.id.cardview_item1);
    infobase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), activity.class);
            Log.e("Cardview","CLICK");
            startActivity(intent);
        }
    });
    ImageView icon1 = view.findViewById(R.id.icon1);
    icon1.setImageResource(R.drawable.ic_bank);


    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment, container, false);
}

用 view.setOnClickListener 设置 OnClickListener 不起作用,也没有更改图像。我没有在 Logcat 上收到事件,所以我猜想 Java 无法正确访问 CardView。这不是在片段中访问 CardView 项的正确方法吗?

您应该 return view 而不是

return inflater.inflate(R.layout.fragment, container, false);

这是更新后的代码行

// Inflate the layout for this fragment
// you have already inflated layout in first line of onCreateView method 
// return inflater.inflate(R.layout.fragment, container, false);
return view;

您在 xml 中的 CardView id 中使用了破折号,但您的 java 代码使用了下划线。将 android:id="@+id/cardview-item1" 更改为 android:id="@+id/cardview_item1"