Android 更新onResume()中的recyclerView;
我在onResume方法中从recyclerView“重新创建”适配器时遇到了一些问题。 基本上,这是我的计划Android 更新onResume()中的recyclerView;,android,performance,android-recyclerview,Android,Performance,Android Recyclerview,我在onResume方法中从recyclerView“重新创建”适配器时遇到了一些问题。 基本上,这是我的计划 MainActivit -ViewPager - fragment A (has a recyclerView) - fragment B (nop) - fragment C (nop) - fragment D (it has the same recyclerView as the framgnet A) 关于我一直要求的简历 > myCusto
MainActivit -ViewPager
- fragment A (has a recyclerView)
- fragment B (nop)
- fragment C (nop)
- fragment D (it has the same recyclerView as the framgnet A)
关于我一直要求的简历
> myCustomAdapter adapter = new myCustomAdapter (
> mListItems,getContext(),ParseUser.getCurrentUser().getObjectId(),
> "type");
> recyclerView.setAdapter(adapter);
但这会导致一些问题,比如当我从另一个活动返回MainActivity时。比如说
Activityt A
- click on item in recylerView
- start Activity B
Activity B
user does some actions, sees some news, and returns to Activity A.
Activity A
recreates the adapter and set recyclerView.setAdapter (new Adapter);
this is slow, causes a delay of 2 seconds after onBackPressed is pressed in Activity B.
我还有一个setMenuVisibility方法,该方法的作用与onResume相同,因为正如片段D中所告知的,我在片段a中有recyclerView,因此如果用户在片段D中的recyclerView中做了一些更改,我需要在用户返回时更新片段a的recyclerView
为什么片段D中的相同recyclerView在片段A中?
我们可以考虑以下内容,在片段A中,我有一个仅包含“用户兴趣”的DealReVIEW视图,在D段中,我有用户信息,例如用户名、照片等。还有“用户兴趣”
结论:问题在于,当我从活动B返回到活动A,并且在viewPager中交替使用片段A和片段D时,这会导致重新创建适配器的延迟 在这种情况下我该怎么办我为这糟糕的英语道歉,我正在使用谷歌翻译。在处理大图像或多个小图像时,不要使用ImageView的“src”属性设置图像 使用像Glide或毕加索这样的图像加载库。 以下代码段适用于Glide v4
Glide.with(context).load(<url_or_res_id>).into(imageView)
Glide.with(context.load())进入(imageView)
您可以将url作为字符串传递。或者,如果您正在从drawable加载图像,则可以传递其资源id(例如R.drawable.image_id)在处理大型图像或多个小型图像时,不要使用ImageView的“src”属性设置图像 使用像Glide或毕加索这样的图像加载库。 以下代码段适用于Glide v4
Glide.with(context).load(<url_or_res_id>).into(imageView)
Glide.with(context.load())进入(imageView)
您可以将url作为字符串传递。或者,如果您正在从drawable加载图像,则可以传递其资源id(例如R.drawable.image_id)这是一个
范围问题。您需要在类成员中定义适配器
如果希望片段A和片段D中显示的数据不同,则将适配器
作为彼此的一个成员,并在每个片段中初始化它。onCreate()
。所以它不会每次都重新创建适配器。(确保ViewPager有足够的缓存号,否则它仍将重新创建整个未缓存的片段)
如果希望FragmentA和FragmentD显示相同的数据或相同数据的一部分,请在main活动中创建adapter
不需要每次恢复时都重新创建适配器
,因为这确实是一项繁重的工作。无需更改任何视图图层属性。确保要共享的数据(适配器)位于同一作用域中或可以传递给它
例如:
//MainActivity
private Adapter adapter;
void onCreate(Bundle savedInstanceState){
//...other stuff
adapter = new myCustomAdapter (mListItems,getContext(),ParseUser.getCurrentUser().getObjectId(), "type");
}
public Adapter getAdapter(){ return adapter;}
//FragmentA or FragmentD
void onCreateView(){
MainActivity activity = (MainActivity) getActivity(); //This fragment should be created with `MainActivity`
recyclerView.setAdapter(activity.getAdapter());
}
这是一个范围问题。您需要在类成员中定义适配器
如果希望片段A和片段D中显示的数据不同,则将适配器
作为彼此的一个成员,并在每个片段中初始化它。onCreate()
。所以它不会每次都重新创建适配器。(确保ViewPager有足够的缓存号,否则它仍将重新创建整个未缓存的片段)
如果希望FragmentA和FragmentD显示相同的数据或相同数据的一部分,请在main活动中创建adapter
不需要每次恢复时都重新创建适配器
,因为这确实是一项繁重的工作。无需更改任何视图图层属性。确保要共享的数据(适配器)位于同一作用域中或可以传递给它
例如:
//MainActivity
private Adapter adapter;
void onCreate(Bundle savedInstanceState){
//...other stuff
adapter = new myCustomAdapter (mListItems,getContext(),ParseUser.getCurrentUser().getObjectId(), "type");
}
public Adapter getAdapter(){ return adapter;}
//FragmentA or FragmentD
void onCreateView(){
MainActivity activity = (MainActivity) getActivity(); //This fragment should be created with `MainActivity`
recyclerView.setAdapter(activity.getAdapter());
}
你是否正在将图像加载到RecyclerView项目中?是的,可能是以错误的方式加载的。我需要在recyclerView的每一行添加8个具有不同图像的ImageView。图像加载导致了延迟,使用Glide或PicassoI等图像加载库刚刚删除了添加ImageView的代码,这确实“解决”了问题,但我确实需要ImageView。我已经用过毕加索了。目前,我正在添加ImageView,如下所示。holder.ContainerImageViews.addView(图像视图);当删除这段代码时,延迟停止了,您认为如果我在xml中添加8个ImageView,会不会对性能产生任何影响?还是同样的拖延会继续下去?每个imageView的大小都是45dp/45dp您是否正在将图像加载到RecyclerView项中?是的,可能是以错误的方式加载的。我需要在recyclerView的每一行添加8个具有不同图像的ImageView。图像加载导致了延迟,使用Glide或PicassoI等图像加载库刚刚删除了添加ImageView的代码,这确实“解决”了问题,但我确实需要ImageView。我已经用过毕加索了。目前,我正在添加ImageView,如下所示。holder.ContainerImageViews.addView(图像视图);当删除这段代码时,延迟停止了,您认为如果我在xml中添加8个ImageView,会不会对性能产生任何影响?还是同样的拖延会继续下去?我使用的每个imageView的大小都是45dp/45dpI:Picasso.with(context).load(url).into(img_profilePicture);请查看我的评论,并告诉我您对holder.ContainerView.addView(img_profilePicture)的看法;您没有使用recyclerview的回收能力。不要使用addView,而是从数组列表加载每个视图,并在将项添加到列表后调用adapter.notifyDatasetChanged()。Recyclerview将管理restSo,您建议我在xml中添加8个ImageView,将它们保留为已消失,只需设置为可见(View.Visible);什么时候有必要?没有