2016-08-08 6 views
0

私は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) 
    { 
     //.. 
     ??? 
    } 
} 

私はより多くの解決策を試しましたが、画像をクリックすると実際には変化しません。 どうかありがとうございます! ありがとう!

答えて

0

あなたがクリックwhneそれを変更したい場合は、convertViewアイテムをinstanciatingときに、このように、クリックイベントを追加する必要があります。

public override View GetView (int position, View convertView, ViewGroup parent) 
{ 
    if(convertView == null) 
    { 
     convertView = inflater.Inflate(Resource.Layout.OtherLabelViewRow, parent, false); 
     convertView.Click += (object sender, EventArgs e) => { 
      int p = (int)(sender as View).Tag; 
      ChangeImageResource(convertView, p); 
     }; 
    } 
    convertView.Tag = position; 
} 

void ChangeImageResource(View convertView, int position) 
{ 
    otherHolder holder = new otherHolder(convertView); 
    holder.iconImg.SetImageResource(mItems[position].Image); 
} 

convertViewをinstanciatingときにクリックイベントを追加することが重要です、あなたのリストビューには項目ごとに1つしかありません。それから、Tagのことは誤った呼び出しを避けることです。あなたのリストビューに多くのアイテムが含まれていて、スクロールする必要がある場合、スクロールするときに変化するので、位置にエラーが発生する可能性があります。タグメソッドを使用すると、正しいメソッドを呼び出すことができます。

+0

この場合、アクティビティでこのメソッド「MListView_ItemClick(object sender、AdapterView.ItemClickEventArgs e)」を使用する必要はありませんか? – user1223445

+0

いいえ、アダプタでclickイベントを直接定義しているので、アクティビティでリスナーを呼び出す必要はありません。 – YumeYume

関連する問題