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.isAvailableAndConnected(Context)) {
のAndroid Studioは、赤い下線のコンテキストをメッセージと共に:「発現が期待されます」。私が試してみましたcontext
、getApplicationContext
、getBaseContext
、this
それらのどれも動作しているようだありません。
どこが間違っているのですか?
footerViewオブジェクトを使用してコンテキストを取得できます。
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
//...
public ProgressViewHolder(View footerView){
if(NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
// Do stuff
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加