У меня есть вопрос по поводу ListView в Android. Я добавил список из 30 пунктов, что означает, что мне нужно прокрутить вниз. Я использую адаптер, как показано ниже:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#70292929"
android:layout_gravity="top|right"
android:orientation="horizontal">
<TableLayout
android:id="@+id/tablelayout1"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow android:gravity="left">
<ListView
android:id="@+id/listview1"
android:background="#1b4f72"
android:layout_weight="0.5"
android:layout_gravity="top|left"
android:layout_width="10px"
android:layout_height="match_parent">
</ListView>
</TableRow>
</TableLayout>
</LinearLayout>
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.RequestWindowFeature(Android.Views.WindowFeatures.NoTitle);
SetContentView(Resource.Layout.Main);
LayoutInflater.Inflate(Resource.Layout.MENU, null);
Android.Views.View globalMENUVIEW = LayoutInflater.Inflate(Resource.Layout.MENU, null); //Listview layout
AlertDialog globalBUILDER = new AlertDialog.Builder(this).Create();
globalBUILDER.Window.SetBackgroundDrawableResource(Resource.Drawable.transparant);
globalBUILDER.Window.SetGravity(Android.Views.GravityFlags.Top | Android.Views.GravityFlags.Right);
globalBUILDER.SetView(globalMENUVIEW);
globalBUILDER.SetCanceledOnTouchOutside(true);
globalBUILDER.Window.SetLayout(800, 500);
globalBUILDER.Show();
globalLISTVIEW1 = globalMENUVIEW.FindViewById<ListView>(Resource.Id.listview1);
List<String> mainMENU = new List<String>();
for (int i = 0; i < 30; i++)
{
mainMENU.Add("hello" + i);
}
globalLISTVIEW1.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mainMENU.ToArray());
//Set item 5 as orange background
for (int i = 0; i < globalLISTVIEW1.ChildCount; i++)
{
if (i == 5) { globalLISTVIEW1.GetChildAt(i).SetBackgroundColor(Android.Graphics.Color.Orange); }
else { globalLISTVIEW1.GetChildAt(i).SetBackgroundColor(Android.Graphics.Color.Transparent); }
}
}
Теперь я установил цвет фона для itemindex 0. Однако когда я прокручиваю вниз, другой элемент также имеет цвет фона.
У меня есть красный, которыйListView использует переработку элементов, вызывающих такое поведение.
Теперь к моему актуальному вопросу. В моем случае я никогда не буду использовать более 30 предметов, и у меня будет красный цвет, чтобы можно было отключить переработку, добавив приведенные ниже значения в адаптер.
Но я не знаю, как на самом деле это сделать и где именно я буду помещать этот код?
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}