0
我一直在試圖建立一個multiselected的ListView像它看起來的圖片在這個環節: picture 當用戶選中在左上角的ListViewItem佈局的複選框必須更改爲未選中狀態。 現在我使用它:自定義列表視圖multiselected的Android
public class ProcedureAdapter : BaseAdapter<Procedure>
{
List<Procedure> items;
Activity context;
Dictionary<int, bool> CheckedItems = new Dictionary<int, bool>();
public ProcedureAdapter(Activity context, List<Procedure> items)
: base()
{
this.context = context;
this.items = items;
for (int i = 0; i < items.Count; i++)
{
CheckedItems.Add(i, false);
}
}
public override long GetItemId(int position)
{
return position;
}
public override Procedure this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public void toggleCheck(int position)
{
if (CheckedItems.ContainsKey(position))
{
CheckedItems[position] = !CheckedItems[position];
base.NotifyDataSetChanged();
}
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.ProcedureListViewItem, null);
if (!CheckedItems.ContainsKey(position))
CheckedItems.Add(position, false);
if (CheckedItems[position])
{
checkBox.Visibility = ViewStates.Visible;
checkBox.Checked = true;
ProcedureTypeImage.Visibility = ViewStates.Gone;
}
else
{
checkBox.Visibility = ViewStates.Gone;
checkBox.Checked = false;
ProcedureTypeImage.Visibility = ViewStates.Visible;
}
return view;
}
}
在活動:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.ProceduresLayout, container, false);
listView = view.FindViewById<ListView>(Resource.Id.listView1);
listView.ItemLongClick += listView_ItemLongClick;
procAdapter = new ProcedureAdapter(Activity, procedures);
listView.Adapter = procAdapter;
return view;
}
void listView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
{
procAdapter.toggleCheck(e.Position);
}
但我面臨的問題: 我如何改變佈局時,用戶選中的複選框? 我已經嘗試在適配器中處理CheckedChange事件,但我怎麼知道這個ListViewItem的位置? 我的解決方案對我來說似乎不是很好,請給我建議,我該如何做得更好。 我想知道你是否向我展示了一個關於C#或Java的簡單例子。 謝謝