Xamarin替换listview中的图像imageview资源

时间:2016-08-08 09:39:04

标签: listview xamarin.android android-imageview

我是Xamarin的初学者。我有一个Listview,每行有一个textview和一个imageview。我为它写了一个adatpter,活动,一切。我的目标是,当我点击列表视图中的某个项目时,图像会更改为其他项目。

数据类:

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

我已经尝试了更多的解决方案,但是当我点击时,图像并没有真实地结合。 拜托,给我任何想法! 谢谢!

1 个答案:

答案 0 :(得分:0)

如果要在单击时更改它,则必须在实例化convertView项时添加单击事件,如下所示:

 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时添加click事件非常重要,因此listview中只有一个项目。然后,Tag的目的是避免错误的调用。如果您的列表视图包含许多项目并且您必须滚动,则可能会导致位置错误,因为滚动时它会发生变化。使用标记方法,您可以确保调用正确的方法。