Android 如何向RecyclerView.ViewHolder提供上下文参数

Android 如何向RecyclerView.ViewHolder提供上下文参数,android,android-recyclerview,Android,Android Recyclerview,在我的RecyclerView.ViewHolder中,我想检查是否存在网络,如果返回true,则取消隐藏进度条 我使用此类检查网络连接: public class NetworkCheck { public static boolean isAvailableAndConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Co

在我的RecyclerView.ViewHolder中,我想检查是否存在网络,如果返回true,则取消隐藏进度条

我使用此类检查网络连接:

public class NetworkCheck {

    public static boolean isAvailableAndConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean isNetworkAvailable = cm.getActiveNetworkInfo() != null;
        boolean isNetWorkConnected = isNetworkAvailable && cm.getActiveNetworkInfo().isConnected();

        return isNetWorkConnected;

    }

}
然后在RecyclerView.ViewHolder中,我执行以下操作:

public static class ProgressViewHolder extends RecyclerView.ViewHolder {
        Button loadButton;
        ProgressBar progressBar;
        public ProgressViewHolder(View footerView){
            super(footerView);
            loadButton = (Button) footerView.findViewById(R.id.reload_button);
            progressBar = (ProgressBar) footerView.findViewById(R.id.progress_load);

            if(NetworkCheck.isAvailableAndConnected(Context)) {
                loadButton.setVisibility(View.VISIBLE);

            }
        }
    }
if(NetworkCheck.isavailable and connected(Context)){
Android Studio红色下划线Context,带有消息:“Expression expected”。我尝试了
Context
getApplicationContext
getBaseContext
但它们似乎都不起作用


请问,我哪里弄错了?

getContext
仅对活动、片段或视图可用

在您的情况下,您应该只使用传递给视图持有者的
itemView


使用
footerView.getContext
itemView.getContext
调用
View
实例的方法。

getContext
仅对活动、片段或视图可用

在您的情况下,您应该只使用传递给视图持有者的
itemView


使用
footerView.getContext
itemView.getContext
调用
View
实例的方法。

正如您所知,您需要一个
Context
对象来
膨胀
您的
itemView
并在
onCreateViewHolder
方法中创建保持架,因此在适配器中保留一个实例这是一种在每次需要时引用
上下文
对象的方式,类似于:

public class Adapter extends RecyclerView.Adapter<Adapter.ProgressViewHolder> {
       Context mContext //you need this to inflate views
       public Adapter(Context context, arguments you need...) {
              mContext = context; //here you keep an reference to Context object
              ...
       }
    ... 
    static class ProgressViewHolder extends RecyclerView.ViewHolder {
    //your implementation
    }
}
公共类适配器扩展了RecyclerView.Adapter{
Context mContext//您需要使用此选项来膨胀视图
公共适配器(上下文、您需要的参数…){
mContext=context;//这里保留对context对象的引用
...
}
... 
静态类ProgressViewHolder扩展了RecyclerView.ViewHolder{
//您的实现
}
}

正如您所知,您需要一个
上下文
对象来
充气
您的
项目视图
并在
onCreateViewHolder
方法中创建支架,因此在适配器中保留一个实例,这样您每次需要时都可以引用
上下文
对象,如下所示:

public class Adapter extends RecyclerView.Adapter<Adapter.ProgressViewHolder> {
       Context mContext //you need this to inflate views
       public Adapter(Context context, arguments you need...) {
              mContext = context; //here you keep an reference to Context object
              ...
       }
    ... 
    static class ProgressViewHolder extends RecyclerView.ViewHolder {
    //your implementation
    }
}
公共类适配器扩展了RecyclerView.Adapter{
Context mContext//您需要使用此选项来膨胀视图
公共适配器(上下文、您需要的参数…){
mContext=context;//这里保留对context对象的引用
...
}
... 
静态类ProgressViewHolder扩展了RecyclerView.ViewHolder{
//您的实现
}
}

您可以使用footerView对象检索上下文:

 public static class ProgressViewHolder extends RecyclerView.ViewHolder {
                //...
                 public ProgressViewHolder(View footerView){
                    if(NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
                       // Do stuff

                }
            }

您可以使用footerView对象检索上下文:

 public static class ProgressViewHolder extends RecyclerView.ViewHolder {
                //...
                 public ProgressViewHolder(View footerView){
                    if(NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
                       // Do stuff

                }
            }

你的视图保持架是静态的,所以把它放在你正在扩展的适配器中,然后传递你用来膨胀这个保持架的上下文。你的视图保持架是静态的,所以把它放在你正在扩展的适配器中,然后传递你用来膨胀这个保持架的上下文。只需在最后一段中的两个示例中的一个添加括号,即可调用函数并获得上下文…只需在最后一段中的两个示例之一中添加括号,即可调用函数并获取上下文。。。