[英]What context name should be passed to Glide method?
In order to load image when using viewholder
and fragment
, I don't know context object name I should pass. 为了在使用
viewholder
和fragment
时加载图像,我不知道应该传递上下文对象名称。 Below is my glide code: 以下是我的滑行代码:
Glide.with(activity).load(cheeses.getImageView()).fitCenter().into(mImageView);
Error: 错误:
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1555)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:667)
at com.bumptech.glide.manager.RequestManagerRetriever.getSupportRequestManagerFragment(RequestManagerRetriever.java:187)
at com.bumptech.glide.manager.RequestManagerRetriever.supportFragmentGet(RequestManagerRetriever.java:195)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:104)
at com.bumptech.glide.Glide.with(Glide.java:644)
at com.support.android.designlibdemo.ViewHolderClass.ViewHolder.bindToCheese(ViewHolder.java:42)
at com.support.android.designlibdemo.CheeseListFragment$1.populateViewHolder(CheeseListFragment.java:112)
at com.support.android.designlibdemo.CheeseListFragment$1.populateViewHolder(CheeseListFragment.java:93)
在ViewHolder中使用getActivity,而不是MainActivity mainActvity;
The issue is here: 问题在这里:
Glide.with(activity)
You are creating a new Activity, rather than getting the current activity, which is why you get that error. 您正在创建一个新的Activity,而不是获取当前的Activity,这就是您收到该错误的原因。 You should do:
你应该做:
Glide.with(getActivity())...
Ideally, you would pass the Fragment context into your RecyclerView
, and use that context in the call. 理想情况下,您将Fragment上下文传递到
RecyclerView
,并在调用中使用该上下文。 The documents say: 文件说:
public static RequestManager with(Context context)
公共静态RequestManager with(Context context)
Begin a load with Glide by passing in a context.
通过传递上下文开始使用Glide进行加载。 Any requests started using a context will only have the application level options applied and will not be started or stopped based on lifecycle events.
使用上下文启动的任何请求将仅应用应用程序级别选项,并且不会基于生命周期事件启动或停止。 In general, loads should be started at the level the result will be used in. If the resource will be used in a view in a child fragment, the load should be started with with(android.app.Fragment)} using that child fragment.
通常,应从使用结果的级别开始加载。如果在子片段的视图中使用资源,则应使用该子片段以(android.app.Fragment)}开始加载。 Similarly, if the resource will be used in a view in the parent fragment, the load should be started with with(android.app.Fragment) using the parent fragment.
同样,如果在父片段的视图中使用该资源,则应使用父片段以(android.app.Fragment)开始加载。 In the same vein, if the resource will be used in a view in an activity, the load should be started with with(android.app.Activity)}.
同样,如果将资源用于活动的视图中,则应以(android.app.Activity)}开始加载。
This method is appropriate for resources that will be used outside of the normal fragment or activity lifecycle (For example in services, or for notification thumbnails).
此方法适用于将在正常片段或活动生命周期之外使用的资源(例如,在服务中或用于通知缩略图)。
Parameters: context - Any context, will not be retained.
参数:context-任何上下文,将不会保留。 Returns: A RequestManager for the top level application that can be used to start a load.
返回:用于顶层应用程序的RequestManager,可用于启动加载。
You could do: 您可以这样做:
public ViewHolder(View view, Context context) {
super(view);
mView = view;
mImageView = (ImageView) view.findViewById(R.id.avatar);
mTextView = (TextView) view.findViewById(android.R.id.text1);
activity = context;
}
Then you could use activity
. 然后,您可以使用
activity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.