Follow code:
Fragment1.cs:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.Inflate(Resource.Layout.fragment_1, container, false);
gridView = (GridView)view.FindViewById(Resource.Id.grid_view);
gridView.ItemClick += (sender, e) =>
{
if (view_1 != null)
{
view_1.SetBackgroundColor(Color.Transparent);
}
view_1 = e.View;
e.View.SetBackgroundColor(Color.ParseColor("#FFC107"));
};
adapter = new ArrayAdapter<string>(Activity, Resource.Layout.item_1, numbers);
gridView.Adapter = adapter;
return view;
}
item_1.axml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
fragment_1.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:columnWidth="40dp"
android:verticalSpacing="15dp"
android:horizontalSpacing="1dp"
android:paddingTop="2dp"
android:stretchMode="columnWidth"
android:layout_centerInParent="true" />
</RelativeLayout>
Example picture:
I want to paint yellow in the first position.
I want to make the background in the first position in OnCreateView
, is it possible?
I have a gridview with numbers I need to load a background in the first position which is the number 1. How can I do this?
Just Like
ListView
,GridView
also reuses its children. So if useView.GetChildAt(position)
and set the background color. you will get multiple views rendered with background color.So, if you only want the first child to be rendered with background color, you have to rewrite the Adapter to
SetBackgroundColor
whenposition==0
and clear the color ifposition!=0
:The
GetView
method of your custom adapter:Update:
Here is the complete codes of Adapter:
You must select the item to which you want to apply the color in the OnCreate event
Since GridView doesn't get populated from the adapter after the first layout pass, you have to change the background color using a
ViewTreeObserver
. To get yourView
'sViewTreeObserver
, you can use theView.ViewTreeObserver
property. Add aViewTreeObserver.IOnGlobalLayoutListener
to theViewTreeObserver
by using it'sAddOnGlobalLayoutListener()
method. Your GridView should be populated by the first time the listener gets invoked.Get the cell's view by using
GridView
'sGetChildAt(int position)
method in theOnGlobalLayoutListener
and set the background color just like you did in your code sample.