所以我有一个包含三个片段的ViewPager。在中间,我想将TextView设置为Visible(如果已停用GPS),并将其隐藏(如果已激活)。为了对GPS模块的可用性做出反应,我正在做这样的事情:
@Override
public void onProviderDisabled(String provider) {
if (provider.equals(LocationManager.GPS_PROVIDER)) {
// gps deactivated, sucks dude
GPS_ACTIVE = false;
((Nearby) mTabsAdapter.getItem(1)).notifyGpsDeactivated();
}
}
@Override
public void onProviderEnabled(String provider) {
if (provider.equals(LocationManager.GPS_PROVIDER)) {
// gps activated, start again!
GPS_ACTIVE = true;
((Nearby) mTabsAdapter.getItem(1)).notifyGpsActivated();
}
}
在“附近”片段中,它看起来像这样:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_nearby, null, false);
int VISIBLE = (SHOW_GPS_NOTIFICATION) ? View.VISIBLE : View.GONE;
Log.d("Nearby", "Show gps not? " + SHOW_GPS_NOTIFICATION);
view.findViewById(R.id.nearby_notification_activategps).setVisibility(
VISIBLE);
return view;
}
public void notifyGpsActivated() {
SHOW_GPS_NOTIFICATION = false;
if (view != null)
view.findViewById(R.id.nearby_notification_activategps)
.setVisibility(View.VISIBLE);
else
Log.d("Nearby", "View is null :(");
}
public void notifyGpsDeactivated() {
Log.d("Nearby", "Gps deactivated.");
SHOW_GPS_NOTIFICATION = true;
if (view != null)
view.findViewById(R.id.nearby_notification_activategps)
.setVisibility(View.VISIBLE);
else
Log.d("Nearby", "View is null :(");
}
谢谢!
哦,是的,顺便说一句,我将这两个值都设置为VISIBLE因为它无论如何都不可见,只是为了测试目的 - 我从来没有看过textview ..所以不是这样。 :)