我正在使用recyclelerview,在向下滚动后崩溃并发出OOM异常。
01-09 02:32:56.169 W / System.err(6816):java.lang.OutOfMemoryError:无法分配带有0个空闲字节的43字节分配和3GB直到OOM
如何改进我的代码?我需要处理东西还是自动发生?因为我在谷歌搜索后找到了不同的答案。
适配器
public override RecyclerView.ViewHolder
OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.From(parent.Context).
Inflate(Resource.Layout.MomentListItem, parent, false);
MomentViewHolder vh = new MomentViewHolder(itemView);
vh.llMain.Click += (object sender, EventArgs e) =>
{
//Check if you don't get a negative position from the header.
if (vh.AdapterPosition >= 0)
{
//Fix the position to leave out the header as a moment
int ajustedPosition = vh.AdapterPosition;
// Create a new fragment and a transaction.
FragmentTransaction fragmentTx = mActivity.FragmentManager.BeginTransaction();
MomentFragment aDifferentDetailsFrag = new MomentFragment();
// Create a bundle to pack the argumants.
Bundle utilBundle = new Bundle();
utilBundle.PutString("ID", mItems[ajustedPosition].momentID);
utilBundle.PutString("Title", mItems[ajustedPosition].title);
utilBundle.PutString("Content", mItems[ajustedPosition].content);
utilBundle.PutString("Author", mItems[ajustedPosition].author);
utilBundle.PutString("Day", mItems[ajustedPosition].day);
utilBundle.PutString("Month", mItems[ajustedPosition].month);
utilBundle.PutString("Time", mItems[ajustedPosition].time);
utilBundle.PutString("ImageURL", mItems[ajustedPosition].imageurl);
// Replace the fragment that is in the View fragment_container (if applicable).
fragmentTx.Replace(Resource.Id.frameLayout1, aDifferentDetailsFrag);
// Add the transaction to the back stack.
fragmentTx.AddToBackStack(null);
// Commit the transaction.
fragmentTx.Commit();
//Put Argument
aDifferentDetailsFrag.Arguments = utilBundle;
}
};
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
MomentViewHolder vh = holder as MomentViewHolder;
var item = mItems[holder.AdapterPosition];
string baseURL = "http://203.195.147.70:8080/daringduckBackend/api/pictures/";
vh.textTitle.Text = item.title;
// vh.textContent.Text =
vh.textAuthor.Text = item.author;
vh.textMonth.Text = item.month;
vh.textDay.Text = item.day;
vh.textTime.Text = item.time;
vh.textContent.Text = "";
if (item.imageurl != "")
{
string avatarURL = string.Concat(baseURL, item.imageurl);
Koush.UrlImageViewHelper.SetUrlDrawable(vh.ivAvatar, avatarURL);
}
if (item.momentImgurl != "")
{
string momentIMGurl = string.Concat(baseURL, item.momentImgurl);
Koush.UrlImageViewHelper.SetUrlDrawable(vh.ivImage, momentIMGurl);
}
}
}