片段是否可以扩展片段?

时间:2015-08-13 12:11:48

标签: android android-fragments xamarin

我是移动开发的新手,我不确定这是否可行。我有一个带有Recycled View的片段,显示内容。事情是其他片段也使用具有相同设计的Recycler View。是否可以通过更改文本视图和图像来扩展该片段然后重用其中的所有内容?

片段代码:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Json;
using Android.Support.V4.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.Widget;
using Android.Graphics;
using Homecheck.Adapters;
using Homecheck.Models;
using Newtonsoft.Json;

namespace Homecheck.Fragments {
    public class SmokeSensor : Fragment {
        private RecyclerView recyclerView;
        private RecyclerView.Adapter adapter;
        private RecyclerView.LayoutManager layoutManager;
        private List<ItemData> itemData;
        private ISharedPreferences pref;
        private SessionManager session;
        private DeviceModel deviceModel;
        private string cookie;
        private const string URL_SMOKE_DATA = "http://10.1.1.20/appapi/getsmokedata";

        public async override void OnStart() {
            base.OnStart();

            session = new SessionManager();
            pref = Activity.GetSharedPreferences("UserSession", FileCreationMode.Private);
            cookie = pref.GetString("PHPSESSID", string.Empty);
            var jsonFetcher = new JsonFetcher();
            JsonValue jsonDashboard = await jsonFetcher.FetchJsonAsync(URL_SMOKE_DATA, null, null, cookie);
            deviceModel = new DeviceModel();
            deviceModel = JsonConvert.DeserializeObject<DeviceModel>(jsonDashboard);

            AddSmokeData();

            adapter = new ViewAdapter(itemData);

            new System.Threading.Thread(new System.Threading.ThreadStart(() => {
                Activity.RunOnUiThread(() => {
                    recyclerView.SetAdapter(adapter);
                });
            })).Start();
        }

        public void AddSmokeData() {
            itemData = new List<ItemData>();

            foreach (SensorData sensorData in deviceModel.SensorData) {
                if (sensorData.Value == "1") {
                    itemData.Add(new ItemData() {
                        title = GetString(Resource.String.smoke_detected_text),
                        date = sensorData.Timestamp,
                        image = Resource.Drawable.smoke_red,
                    });
                }
            }
        }

        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);
            recyclerView.HasFixedSize = true;
            layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.Vertical);

            recyclerView.SetLayoutManager(layoutManager);
            recyclerView.SetItemAnimator(new DefaultItemAnimator());
            recyclerView.AddItemDecoration(new SpaceItemDecoration(20));

            return view;
        }

        public class SpaceItemDecoration : RecyclerView.ItemDecoration {
            private int space;

            public SpaceItemDecoration(int space) {
                this.space = space;
            }

            public override void GetItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.Left = space;
                outRect.Right = space;
                outRect.Bottom = space;

                if (parent.GetChildAdapterPosition(view) == 0) {
                    outRect.Top = space;
                }
            }
        }

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

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

            public class ItemView : RecyclerView.ViewHolder {
                public View mainView { get; set; }

                public TextView title { get; set; }

                public TextView date { get; set; }

                public ImageView image { get; set; }

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

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

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

                var viewHolder = new ItemView(itemLayoutView) { title = titleText, date = dateText, image = imageId };

                return viewHolder;
            }

            public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
                ItemView itemHolder = viewHolder as ItemView;

                itemHolder.title.Text = itemData[position].title;
                itemHolder.date.Text = itemData[position].date;
                itemHolder.image.SetImageResource(itemData[position].image);
            }

            public override int ItemCount {
                get { return itemData.Count; }
            }
        }

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

            public string date { get; set; }

            public int image { get; set; }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我已经改变了这个答案,你可以扩展片段。我之前从未见过它,但我只是玩了一个游戏,它运行良好。我只是觉得我从来没有一个扩展整个片段有意义的案例,我倾向于认为如果某个东西在多个课程中有效,我就把它放在另一个课堂上。

Jame Gosling(制作java的主管)说不使用继承作为实现代码重用的手段。

更多信息:http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html

答案 1 :(得分:0)

这是个坏主意,我只会使用一个会显示不同信息的片段,具体取决于它的内容。