Reputation: 392
This is a snippet of the XML of the fragment:
<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 -->
It isn't the full XML Code, it repeats itself as you can see from the Screenshot.
I'm trying to set an OnClickListener on each item, I also want to change the Icon of every Item separatly within the Java Code (later on the Image gets loaded with 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);
}
Setting the OnClickListener with view.setOnClickListener doesn't work, neither changing the Image. I don't get an event on the Logcat so I guess Java isn't able to access to the CardView correctly. Isn't this the right way to access the CardView item in a fragment?
Upvotes: 0
Views: 3333
Reputation: 950
In your CardView id in the xml you used a dash, but your java code uses an underscore. Change android:id="@+id/cardview-item1" to android:id="@+id/cardview_item1"
Upvotes: 0
Reputation: 2877
You should need to return view
instead
return inflater.inflate(R.layout.fragment, container, false);
Here is the updated line of code
// 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;
Upvotes: 2