Xamarin - Recycled View会出错

时间:2015-08-07 09:19:49

标签: c# android xamarin android-recyclerview

我是应用程序开发的新手,我正在尝试在片段上创建循环视图,但是有一些问题,它会引发以下错误:

enter image description here

代码:

namespace Homecheck.Fragments {
    public class SmokeSensor : Fragment {
        private RecyclerView recyclerView;
        private RecyclerView.Adapter adapter;
        private RecyclerView.LayoutManager layoutManager;
        private List<ItemData> itemData;

        public override void OnCreate(Bundle savedInstanceState) {
            base.OnCreate(savedInstanceState);

            recyclerView.HasFixedSize = true;

            layoutManager = new LinearLayoutManager(Activity);
            recyclerView.SetLayoutManager(layoutManager);

            itemData = new List<ItemData>();
            itemData.Add(new ItemData() {title = "Testing", image = Resource.Drawable.ic_circle_red});

            adapter = new MyAdapter(itemData);
            recyclerView.SetAdapter(adapter);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.Inflate(Resource.Layout.SmokeSensor, container, false);
            recyclerView = view.FindViewById<RecyclerView>(Resource.Id.smoke_recycler_view);

            return view;
        }
    }

    public class MyAdapter : RecyclerView.Adapter {
        private List<ItemData> itemData;
        private TextView titleText;
        private ImageView imageId;

        public MyAdapter(List<ItemData> itemData) {
            this.itemData = itemData;
        }

        public class ViewHolder : RecyclerView.ViewHolder {
            public View mainView { get; set; }
            public TextView title { get; set; }
            public ImageView image { get; set; }

            public ViewHolder (View view) : base (view) {
                mainView = view;
            }
        }

        public override MyAdapter.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            View itemLayoutView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.SmokeItemView, null);

            titleText = itemLayoutView.FindViewById<TextView>(Resource.Id.item_title);
            imageId = itemLayoutView.FindViewById<ImageView>(Resource.Id.item_icon);

            ViewHolder viewHolder = new ViewHolder(itemLayoutView) { title = titleText, image = imageId };

            return viewHolder;
        }

        public override void OnBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.title.SetText(itemData[position]);
        }

        public override int getItemCount() {
            return itemData.Count();
        }
    }

    public class ItemData {
        public string title { get; set; }
        public int image { get; set; }
    }
}

我知道这很可能是一个愚蠢的错误,但我似乎无法找到它并修复它。

1 个答案:

答案 0 :(得分:2)

OnCreateViewHolder的返回类型错误,应为Android.Support.V7.Widget.RecyclerView.ViewHolder

您需要覆盖ItemCount

 public override int ItemCount {get{...}}

参数类型错误,请按以下方式修复:

 public override void OnBindViewHolder(Android.Support.V7.Widget.RecyclerView.ViewHolder viewHolder, int position)