Fix for ListViews which don't support headers

This commit is contained in:
serso 2015-02-04 20:50:01 +01:00
parent 805abda582
commit d91bc22577

View File

@ -1,7 +1,6 @@
package org.solovyev.android.calculator.preferences; package org.solovyev.android.calculator.preferences;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
@ -9,6 +8,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ListView;
import org.solovyev.android.calculator.ActivityUi; import org.solovyev.android.calculator.ActivityUi;
import org.solovyev.android.calculator.AdView; import org.solovyev.android.calculator.AdView;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
@ -22,6 +22,7 @@ import javax.annotation.Nonnull;
public abstract class BasePreferencesActivity extends PreferenceActivity { public abstract class BasePreferencesActivity extends PreferenceActivity {
private static boolean SUPPORT_HEADERS = true;
private final ActivityCheckout checkout = Checkout.forActivity(this, App.getBilling(), App.getProducts()); private final ActivityCheckout checkout = Checkout.forActivity(this, App.getBilling(), App.getProducts());
private Inventory inventory; private Inventory inventory;
private AdView adView; private AdView adView;
@ -66,29 +67,37 @@ public abstract class BasePreferencesActivity extends PreferenceActivity {
} }
protected void onShowAd(boolean show) { protected void onShowAd(boolean show) {
if(!supportsAds()) { if (!supportsHeaders()) {
return; return;
} }
final ListView listView = getListView();
if (show) { if (show) {
if (adView != null) { if (adView != null) {
return; return;
} }
adView = (AdView) LayoutInflater.from(this).inflate(R.layout.ad, null); adView = (AdView) LayoutInflater.from(this).inflate(R.layout.ad, null);
adView.show(); adView.show();
getListView().addHeaderView(adView); try {
listView.addHeaderView(adView);
} catch (IllegalStateException e) {
// doesn't support header views
SUPPORT_HEADERS = false;
adView.hide();
adView = null;
}
} else { } else {
if (adView == null) { if (adView == null) {
return; return;
} }
getListView().removeHeaderView(adView); listView.removeHeaderView(adView);
adView.hide(); adView.hide();
adView = null; adView = null;
} }
} }
private boolean supportsAds() { private boolean supportsHeaders() {
// on Android 2.3 the headers in the list view are not supported return SUPPORT_HEADERS;
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
} }
@Override @Override