如何在安卓中禁用列表视图的回收



我有一个关于安卓列表视图的问题。我添加了 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); }
}
}

现在我已经在项目索引 0 上设置了背景颜色。但是,当我向下滚动时,另一个项目也有背景色。

我有红色,列表视图使用回收导致此行为的项目。

现在谈谈我的实际问题。就我而言,我永远不会使用超过 30 个项目,并且红色表示可以通过将以下值添加到适配器来禁用回收。

但是我不知道如何实际执行此操作以及我将把这段代码放在哪里?

@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}

这就是我的适配器在代码中的样子。我添加了一个"新项目"并选择适配器作为生成下面代码的项目。

如所见,我已经覆盖了:getItemId 和 getItemViewType,它们应该会阻止回收。但是这段代码似乎并不完整。

我仍然需要能够像在原始适配器中一样传入所有这些参数:

globalLISTVIEW1.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mainMENU.ToArray());

那么我们如何完成以下适配器呢?


class Adapter1 : BaseAdapter
{
Context context;
public Adapter1(Context context)
{

this.context = context;
}
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override long GetItemId(int position)
{
return position;
}
public override int GetItemViewType(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView;
Adapter1ViewHolder holder = null;
if (view != null)
holder = view.Tag as Adapter1ViewHolder;
if (holder == null)
{
holder = new Adapter1ViewHolder();
var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
//replace with your item and your holder items
//comment back in
//view = inflater.Inflate(Resource.Layout.item, parent, false);
//holder.Title = view.FindViewById<TextView>(Resource.Id.text);
view.Tag = holder;
}
//fill in your items
//holder.Title.Text = "new text here";
return view;
}
//Fill in cound here, currently 0
public override int Count
{
get
{
return 0;
}
}
}
class Adapter1ViewHolder : Java.Lang.Object
{
//Your adapter views to re-use
//public TextView Title { get; set; }
}

最新更新