我正在使用RecyclerView和Xamarin.Android。我正在使用List<Article>
填充适配器。当用户向下滚动时,为了向列表添加更多项目,我正在使用RecyclerView.OnScrollListener
。它在事件处理程序中触发偶数和内部我正在检索数据并将其添加到旧列表中,然后调用adapter.NotifyDataSetChanged ();
。它检索数据并触发事件,但不更新设备上的recyclerview。
我做错了什么?
RecyclerView recyclerview;
NewsAdapter adapter;
private List<Article> listofarticles;
var onScrollListener = new XamarinRecyclerViewOnScrollListener (layoutmanager);
onScrollListener.LoadMoreEvent += async (object sender, EventArgs e) => {
//Load more stuff here
Console.WriteLine ("*** loading more stuff ***");
var newarticles = await GetData ("2");
listofarticles = listofarticles.Concat (newarticles).ToList ();
adapter.NotifyDataSetChanged ();
};
这是我的适配器:(省略了clickhandler和itemcount方法)
// Adapter
public class NewsAdapter : RecyclerView.Adapter
{
// Event handler for item clicks:
public event EventHandler<int> ItemClick;
private Context globalContext = null;
// Underlying data set:
public List<Article> _listofarticles;
// Load the adapter with the data set (articles) at construction time:
public NewsAdapter (List<Article> listofarticle, Context context)
{
this._listofarticles = listofarticle;
globalContext = context;
}
// Create a new article CardView (invoked by the layout manager):
public override RecyclerView.ViewHolder OnCreateViewHolder (ViewGroup parent, int viewType)
{
// Inflate the CardView for the photo:
View itemView = LayoutInflater.From (parent.Context).Inflate (Resource.Layout.latestnews_recycler_article, parent, false);
// Create a ViewHolder to find and hold these view references, and
// register OnClick with the view holder:
NewsViewHolder vh = new NewsViewHolder (itemView, OnClick);
return vh;
}
// Fill in the contents of the article card (invoked by the layout manager):
public override void OnBindViewHolder (RecyclerView.ViewHolder holder, int position)
{
NewsViewHolder vh = holder as NewsViewHolder;
// Set the ImageView and TextView in this ViewHolder's CardView
// from this position in the photo album:
//vh.Thumbnail.SetImageResource (_listofarticles[position].Thumbnail);
// We use Picasso to load images efficiently
Picasso.With (globalContext).Load (_listofarticles[position].Thumbnail)
//.Placeholder(R.drawable.ic_placeholder) // optional
//.Error(R.drawable.ic_error_fallback) // optional
//.Resize(250, 200) // optional
//.Rotate(90) // optional
.Into (vh.Thumbnail);
vh.Title.Text = _listofarticles[position].Title;
}
}
答案 0 :(得分:2)
我需要将新项添加到内部适配器(不是活动类的那个)_listofarticles
,然后在适配器类中调用NotifyDataSetChanged,如{{1} }。为了实现这一点,我在适配器中添加了一个名为this.NotifyDataSetChanged ();
的方法:
AddToList
我在事件处理程序中调用它来滚动像:
public void AddToList(List<Article> newitemslist)
{
_listofarticles = _listofarticles.Concat (newitemslist).ToList ();
this.NotifyDataSetChanged ();
}
这会正确更新适配器并将项目添加到recyclerview的末尾。
答案 1 :(得分:0)
在Xamarin中,由于使用ArrayAdapter时的内存管理和垃圾回收,实现会在您创建后备份列表的副本。
为了能够使用 NotifyDataSetChanged ,您需要使用Android.Runtime.JavaList而不是.Net IList 。
这是一个片段:
ArrayAdapter adapter;
Android.Runtime.JavaList<string> messages = new Android.Runtime.JavaList<string>();
然后在 OnCreate 或 OnResume 中添加以下代码:
adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, Android.Resource.Id.Text1, messages);
listView.Adapter = adapter;
您现在可以在列表中添加项目并调用 NotifyDataSetChanged ,您应该能够获得更改。
在我的情况下,我在按钮上有一个点击处理程序,用于更新列表视图:
void BtnSend_Click(object sender, System.EventArgs e)
{
var message = editText.Text;
messages.Add(message);
adapter.NotifyDataSetChanged();
}