我有一个包含CardView项目的RecycledView片段。每当用户点击其中一张卡时,它应该加载另一个片段,对应于卡片的位置。我到处寻找解决方案,但由于你可以用不同的方式编写RecycledView逻辑,我发现的解决方案对我不起作用。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Json;
using Android.App;
using Android.Support.V4.App;
using Android.Support.V7.Widget;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Net;
using Android.Views.Animations;
using Android.Graphics;
using Android.Graphics.Drawables;
using Newtonsoft.Json;
using Homecheck.Adapters;
using Homecheck.Models;
namespace *********.Fragments {
public class Dashboard : GridLayoutBase {
private ISharedPreferences pref;
private SessionManager session;
private string cookie;
private DeviceModel deviceModel;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private List<ItemData> itemData;
private const string URL_DASHBOARD = "http://10.1.1.20/appapi/getdashboard";
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.FetchDataWithCookieAsync(URL_DASHBOARD, cookie);
deviceModel = new DeviceModel();
deviceModel = JsonConvert.DeserializeObject<DeviceModel>(jsonDashboard);
DisplaySensorStates();
DisplayLastPhoto();
adapter = new ViewAdapter(itemData);
new System.Threading.Thread(new System.Threading.ThreadStart(() => {
Activity.RunOnUiThread(() => {
recyclerView.SetAdapter(adapter);
});
})).Start();
}
// Shows sensor states
public void DisplaySensorStates() {
itemData = new List<ItemData>();
foreach (var sensor in deviceModel.Sensors) {
itemData.Add(new ItemData() {
image = Resource.Drawable.smoke_red,
title = sensor.Name.First().ToString().ToUpper() + sensor.Name.Substring(1).ToLower() + " sensor",
});
}
}
// Shows the last camera photo
public async void DisplayLastPhoto() {
if (deviceModel.Error == "true" && deviceModel.ErrorType == "noPhoto") {
//TODO: Show a "No photo" picture
} else {
string url = deviceModel.LastPhotoLink;
Bitmap imageBitmap = await new ImageDownloader().GetImageBitmapFromUrlAsync(url, Activity, lastPhoto.Width, lastPhoto.Height);
lastPhoto.SetImageBitmap(imageBitmap);
imageBitmap.Dispose();
}
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.Inflate(Resource.Layout.Dashboard, container, false);
recyclerView = view.FindViewById<RecyclerView>(Resource.Id.dashboard_recycler_view);
layoutManager = new GridLayoutManager(Activity, 3);
recyclerView.HasFixedSize = true;
recyclerView.SetLayoutManager(layoutManager);
recyclerView.SetItemAnimator(new DefaultItemAnimator());
recyclerView.AddItemDecoration(new SpaceItemDecoration(8));
return view;
}
public class ViewAdapter : RecyclerView.Adapter {
private List<ItemData> itemData;
private ImageView imageId;
private TextView sensorStatus;
public ViewAdapter(List<ItemData> itemData) {
this.itemData = itemData;
}
public class ItemView : RecyclerView.ViewHolder {
public View mainView { get; set; }
public ImageView image { get; set; }
public TextView status { 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.DashboardItems, null);
CardView cardView = itemLayoutView.FindViewById<CardView>(Resource.Id.dashboard_card_view);
imageId = itemLayoutView.FindViewById<ImageView>(Resource.Id.sensor_image);
sensorStatus = itemLayoutView.FindViewById<TextView>(Resource.Id.sensor_status);
var viewHolder = new ItemView(itemLayoutView) { image = imageId, status = sensorStatus };
return viewHolder;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
ItemView itemHolder = viewHolder as ItemView;
itemHolder.image.SetImageResource(itemData[position].image);
itemHolder.status.Text = itemData[position].title;
}
public override int ItemCount {
get { return itemData.Count; }
}
}
public class ItemData {
public int image { get; set; }
public string title { get; set; }
}
}
}