gpt4 book ai didi

android - fragment 是否可以扩展 fragment ?

转载 作者:行者123 更新时间:2023-11-29 01:29:38 26 4
gpt4 key购买 nike

我是移动开发的新手,我不确定这是否可行。我有一个带有显示内容的回收 View 的 fragment 。问题是其他 fragment 也使用具有相同设计的 Recycler View。是否可以扩展该 fragment ,然后仅通过更改 TextView 和图像来重用其中的所有内容?

fragment 代码:

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; }
}
}
}

最佳答案

我已经改变了这个答案,你可以扩展 fragment 。我以前从未见过它完成,但我刚刚玩过,效果很好。我只是认为我从来没有遇到过扩展整个 fragment 有意义的情况,我倾向于认为如果某些东西在多个类中具有实用性,我会把它放在另一个类中。

Jame Gosling(制作 java 的负责人)说不要使用继承作为实现代码重用的手段。

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

关于android - fragment 是否可以扩展 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31987892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com