私はXamarinで初心者です。私はListviewを持って、各行に1つのテキストビューと1つの画像ビューがあります。私はそれ、活動、すべてのためのadatpterを書いた。私の目標は、リストビュー内の項目をクリックすると、画像が他のものに変わります。Xamarinはlistviewのimage imageviewリソースを置き換えます
データクラス:ここ
class otherLabelListData
{
private string otherLabel;
private int image;
public otherLabelListData(string otherLabel, int image)
{
this.otherLabel = otherLabel;
this.image = image;
}
public string OtherLabel
{
get { return otherLabel; }
}
public int Image
{
get { return image; }
}
}
は、ホルダークラスです:
class otherHolder
{
public TextView labelTxt;
public ImageView iconImg;
public otherHolder(View itemView)
{
labelTxt = itemView.FindViewById<TextView>(Resource.Id.otherMessageLabel);
iconImg = itemView.FindViewById<ImageView>(Resource.Id.otherLabelIcon);
}
}
アダプタ:
class otherLabelListAdapter : BaseAdapter<otherLabelListData>
{
private JavaList<otherLabelListData> mItems;
private Context mContext;
private LayoutInflater inflater;
public otherLabelListAdapter(Context context, JavaList<otherLabelListData> items)
{
this.mItems = items;
this.mContext = context;
}
public override Java.Lang.Object GetItem(int position)
{
return mItems.Get(position);
}
public override int Count
{
get
{
return mItems.Size();
}
}
public override long GetItemId(int position)
{
return position;
}
public override otherLabelListData this[int position]
{
get
{
return mItems[position];
}
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
if(inflater == null)
{
inflater = (LayoutInflater)mContext.GetSystemService(Context.LayoutInflaterService);
}
if(convertView == null)
{
convertView = inflater.Inflate(Resource.Layout.OtherLabelViewRow, parent, false);
}
otherHolder holder = new otherHolder(convertView);
holder.labelTxt.Text = mItems[position].OtherLabel;
holder.iconImg.SetImageResource(mItems[position].Image);
return convertView;
}
}
アクティビティ:
[Activity(Label = "Activity1", MainLauncher = false, Theme = "@style/Theme.Mt")]
public class Activity_OtherScreen : Android.Support.V4.App.FragmentActivity
{
private ListView mListView;
private otherLabelListAdapter adapter;
JavaList<otherLabelListData> list;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Other_Layout);
mListView = FindViewById<ListView>(Resource.Id.otherList);
adapter = new otherLabelListAdapter(this, getOthers());
mListView.Adapter = adapter;
mListView.ItemClick += MListView_ItemClick;
}
private JavaList<otherLabelListData> getOthers()
{
list = new JavaList<otherLabelListData>();
otherLabelListData ot;
ot = new otherLabelListData("Label 1", Resource.Drawable.send2display);
list.Add(ot);
ot = new otherLabelListData("Label 2", Resource.Drawable.send2display);
list.Add(ot);
ot = new otherLabelListData("Label 3", Resource.Drawable.send2display);
list.Add(ot);
return list;
}
private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//..
???
}
}
私はより多くの解決策を試しましたが、画像をクリックすると実際には変化しません。 どうかありがとうございます! ありがとう!
この場合、アクティビティでこのメソッド「MListView_ItemClick(object sender、AdapterView.ItemClickEventArgs e)」を使用する必要はありませんか? – user1223445
いいえ、アダプタでclickイベントを直接定義しているので、アクティビティでリスナーを呼び出す必要はありません。 – YumeYume