RecyclerView 在渲染布局和制作新适配器后不调用 oncreateview
RecyclerView not calling oncreateview after layout is rendered and new adapter is made
我的应用程序是事件驱动的应用程序我有一个下载管理器和一个下载 activity。
问题:
下载列表生成后,我的下载管理器会通知我的下载 activity。
如果我在下载管理器中的列表创建之前打开下载 activity 然后我的下载管理器通知我的下载 activity 新的下载项目列表已经创建但是屏幕上没有呈现视图意味着没有回收视图项目是在运行时在屏幕上创建的,在回收站视图中也没有调用 oncreateview holder。
但是如果我在下载管理器中的列表之后打开下载 activity 那么一切正常。
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.downloading_layout);
InitializeComponents();
SubscribeEvents();
AssignAdapter();
}
private void InitializeComponents()
{
_noDownloadTV = FindViewById<TextView>(Resource.Id.tv_no_download);
_downloadingList = FindViewById<RecyclerView>(Resource.Id.recyclerview_downloading);
_downloadingList.SetLayoutManager(new LinearLayoutManager(this));
}
在下载管理器中创建或更新列表后调用这些函数
private void OnCurrentDownloadRequest_Changed(object sender, EventArgs e)
{
if (_listAdapter == null)
AssignAdapter();
else
_listAdapter.UpdateList(DownloadManager.Instance.currentRequest.DownloadFiles);
}
AssignAdapter 函数
private void AssignAdapter()
{
var currentRequest = DownloadManager.Instance.currentRequest;
if (currentRequest != null && currentRequest.DownloadFiles.Count > 0)
{
_listAdapter = new DownloadListAdapter(this, currentRequest.DownloadFiles);
_downloadingList.SetAdapter(_listAdapter);
}
}
Recyclerview 实施
public DownloadListAdapter(Context context, List<DownloadFileDetail> list)
{
_context = context;
_list = list;
}
public void UpdateList(List<DownloadFileDetail> list)
{
_list = list;
NotifyDataSetChanged();
}
public override int ItemCount
{
get
{
return _list.Count;
}
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
try
{
if (holder is DownloadListViewHolder viewHolder)
{
var item = _list[position];
viewHolder.downloadItemName.Text = item.Name;
viewHolder.downloadItemStatus.Text = Enum.GetName(typeof(DownloadFileStatus), item.Status);
}
}
catch (Exception ex)
{
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.download_list_item, parent, false);
var vh = new DownloadListViewHolder(itemView);
return vh;
}
问题是我在 UI 线程外设置适配器,它抛出异常,所以在 UI 线程内使用
调用设置适配器后
RunOnUiThread(() =>
{
_downloadingList.SetLayoutManager(new LinearLayoutManager(this));
_downloadingList.SetAdapter(_listAdapter);
});
已呈现视图
我的应用程序是事件驱动的应用程序我有一个下载管理器和一个下载 activity。
问题: 下载列表生成后,我的下载管理器会通知我的下载 activity。 如果我在下载管理器中的列表创建之前打开下载 activity 然后我的下载管理器通知我的下载 activity 新的下载项目列表已经创建但是屏幕上没有呈现视图意味着没有回收视图项目是在运行时在屏幕上创建的,在回收站视图中也没有调用 oncreateview holder。
但是如果我在下载管理器中的列表之后打开下载 activity 那么一切正常。
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.downloading_layout);
InitializeComponents();
SubscribeEvents();
AssignAdapter();
}
private void InitializeComponents()
{
_noDownloadTV = FindViewById<TextView>(Resource.Id.tv_no_download);
_downloadingList = FindViewById<RecyclerView>(Resource.Id.recyclerview_downloading);
_downloadingList.SetLayoutManager(new LinearLayoutManager(this));
}
在下载管理器中创建或更新列表后调用这些函数
private void OnCurrentDownloadRequest_Changed(object sender, EventArgs e)
{
if (_listAdapter == null)
AssignAdapter();
else
_listAdapter.UpdateList(DownloadManager.Instance.currentRequest.DownloadFiles);
}
AssignAdapter 函数
private void AssignAdapter()
{
var currentRequest = DownloadManager.Instance.currentRequest;
if (currentRequest != null && currentRequest.DownloadFiles.Count > 0)
{
_listAdapter = new DownloadListAdapter(this, currentRequest.DownloadFiles);
_downloadingList.SetAdapter(_listAdapter);
}
}
Recyclerview 实施
public DownloadListAdapter(Context context, List<DownloadFileDetail> list)
{
_context = context;
_list = list;
}
public void UpdateList(List<DownloadFileDetail> list)
{
_list = list;
NotifyDataSetChanged();
}
public override int ItemCount
{
get
{
return _list.Count;
}
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
try
{
if (holder is DownloadListViewHolder viewHolder)
{
var item = _list[position];
viewHolder.downloadItemName.Text = item.Name;
viewHolder.downloadItemStatus.Text = Enum.GetName(typeof(DownloadFileStatus), item.Status);
}
}
catch (Exception ex)
{
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.download_list_item, parent, false);
var vh = new DownloadListViewHolder(itemView);
return vh;
}
问题是我在 UI 线程外设置适配器,它抛出异常,所以在 UI 线程内使用
调用设置适配器后 RunOnUiThread(() =>
{
_downloadingList.SetLayoutManager(new LinearLayoutManager(this));
_downloadingList.SetAdapter(_listAdapter);
});
已呈现视图