如何将第一个位置的背景设置为默认值? [英] How to set background in the first position as default?
问题描述
关注代码:
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>
示例图片:
我想在第一个位置涂上黄色.
I want to paint yellow in the first position.
我想在OnCreateView
的第一个位置做背景,可以吗?
I want to make the background in the first position in OnCreateView
, is it possible?
我有一个带有数字的网格视图,我需要在第一个位置加载背景,即数字 1.我该怎么做?
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?
推荐答案
就像 ListView
一样,GridView
也重用了它的子元素.所以如果使用 View.GetChildAt(position)
并设置背景颜色.您将获得多个带有背景颜色的视图.
Just Like ListView
, GridView
also reuses its children. So if use View.GetChildAt(position)
and set the background color. you will get multiple views rendered with background color.
所以,如果你只希望第一个孩子用背景颜色渲染,你必须在 position==0
时将 Adapter 重写为 SetBackgroundColor
并清除颜色如果 position!=0
:
So, if you only want the first child to be rendered with background color, you have to rewrite the Adapter to SetBackgroundColor
when position==0
and clear the color if position!=0
:
自定义适配器的 GetView
方法:
The GetView
method of your custom adapter:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view=null;
if (convertView != null)
{
view = convertView;
}
else
{
view = LayoutInflater.FromContext(parent.Context).Inflate(Resource.Layout.item1,null);
}
if (position == 0)
{
view.SetBackgroundColor(Android.Graphics.Color.Yellow);
}
else
{
view.SetBackgroundColor(Android.Graphics.Color.Transparent);
}
(view as TextView).Text = _items[position];
return view;
}
更新:
以下是Adapter的完整代码:
Here is the complete codes of Adapter:
public class MyGridViewAdapter : BaseAdapter
{
string[] _items;
public MyGridViewAdapter(string[] items)
{
_items = items;
}
public override int Count => _items.Length;
public override Java.Lang.Object GetItem(int position)
{
return _items[position];
}
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view=null;
if (convertView != null)
{
view = convertView;
}
else
{
view = LayoutInflater.FromContext(parent.Context).Inflate(Resource.Layout.item1,null);
}
if (position == 0)
{
view.SetBackgroundColor(Android.Graphics.Color.Yellow);
}
else
{
view.SetBackgroundColor(Android.Graphics.Color.Transparent);
}
(view as TextView).Text = _items[position];
return view;
}
}
这篇关于如何将第一个位置的背景设置为默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!