Recalculate view's height when tab is selected

Viewpager2 created view based on the first tab height, when tab is
selected, viewpager2 should recalculate height.

But this solution may not suitable for App list, so we only enable
for Location Settings which have different items in personal & work
profile.

Bug: 224521665
Test: manual
Change-Id: Ib19b30cb82b8b4f13f651795906289da53ded4ed
This commit is contained in:
Edgar Wang
2022-04-21 11:44:10 +08:00
parent 27aa5f9d94
commit f3270e5c1c
8 changed files with 61 additions and 74 deletions

View File

@@ -16,21 +16,20 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto" xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/location_services_preference_title">
<PreferenceCategory
android:key="location_services" android:key="location_services"
android:layout="@layout/preference_category_no_label" android:title="@string/location_services_preference_title"
settings:controller="com.android.settings.location.LocationInjectedServicesPreferenceController"/> settings:controller="com.android.settings.location.LocationInjectedServicesPreferenceController">
<Preference <Preference
android:fragment="com.android.settings.location.WifiScanningFragment" android:fragment="com.android.settings.location.WifiScanningFragment"
android:order="1000"
android:key="location_services_wifi_scanning" android:key="location_services_wifi_scanning"
android:title="@string/location_scanning_wifi_always_scanning_title" android:title="@string/location_scanning_wifi_always_scanning_title"
settings:controller="com.android.settings.location.LocationServicesWifiScanningPreferenceController"/> settings:controller="com.android.settings.location.LocationServicesWifiScanningPreferenceController"/>
<Preference <Preference
android:fragment="com.android.settings.location.BluetoothScanningFragment" android:fragment="com.android.settings.location.BluetoothScanningFragment"
android:order="1001"
android:key="location_services_bluetooth_scanning" android:key="location_services_bluetooth_scanning"
android:title="@string/location_scanning_bluetooth_always_scanning_title" android:title="@string/location_scanning_bluetooth_always_scanning_title"
settings:controller="com.android.settings.location.LocationServicesBluetoothScanningPreferenceController"/> settings:controller="com.android.settings.location.LocationServicesBluetoothScanningPreferenceController"/>

View File

@@ -16,12 +16,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto" xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/location_services_screen_title"> android:title="@string/location_services_screen_title"
<PreferenceCategory
android:title="@string/managed_profile_location_services"
android:layout="@layout/preference_category_no_label"
android:key="location_services_managed_profile" android:key="location_services_managed_profile"
settings:controller="com.android.settings.location.LocationInjectedServicesForWorkPreferenceController"/> settings:controller="com.android.settings.location.LocationInjectedServicesForWorkPreferenceController">
</PreferenceScreen> </PreferenceScreen>

View File

@@ -99,6 +99,8 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
private ViewGroup mContentView; private ViewGroup mContentView;
private ViewPager2 mViewPager;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@@ -111,12 +113,21 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
final int selectedTab = getTabId(activity, getArguments()); final int selectedTab = getTabId(activity, getArguments());
final View tabContainer = mContentView.findViewById(R.id.tab_container); final View tabContainer = mContentView.findViewById(R.id.tab_container);
final ViewPager2 viewPager = tabContainer.findViewById(R.id.view_pager); mViewPager = tabContainer.findViewById(R.id.view_pager);
viewPager.setAdapter(new ProfileSelectFragment.ViewPagerAdapter(this)); mViewPager.setAdapter(new ProfileSelectFragment.ViewPagerAdapter(this));
final TabLayout tabs = tabContainer.findViewById(R.id.tabs); final TabLayout tabs = tabContainer.findViewById(R.id.tabs);
new TabLayoutMediator(tabs, viewPager, new TabLayoutMediator(tabs, mViewPager,
(tab, position) -> tab.setText(getPageTitle(position)) (tab, position) -> tab.setText(getPageTitle(position))
).attach(); ).attach();
mViewPager.registerOnPageChangeCallback(
new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
updateHeight(position);
}
}
);
tabContainer.setVisibility(View.VISIBLE); tabContainer.setVisibility(View.VISIBLE);
final TabLayout.Tab tab = tabs.getTabAt(selectedTab); final TabLayout.Tab tab = tabs.getTabAt(selectedTab);
tab.select(); tab.select();
@@ -133,6 +144,36 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
return mContentView; return mContentView;
} }
protected boolean forceUpdateHeight() {
return false;
}
private void updateHeight(int position) {
if (!forceUpdateHeight()) {
return;
}
ViewPagerAdapter adapter = (ViewPagerAdapter) mViewPager.getAdapter();
if (adapter == null || adapter.getItemCount() <= position) {
return;
}
Fragment fragment = adapter.createFragment(position);
View newPage = fragment.getView();
if (newPage != null) {
int viewWidth = View.MeasureSpec.makeMeasureSpec(newPage.getWidth(),
View.MeasureSpec.EXACTLY);
int viewHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
newPage.measure(viewWidth, viewHeight);
int currentHeight = mViewPager.getLayoutParams().height;
int newHeight = newPage.getMeasuredHeight();
if (newHeight != 0 && currentHeight != newHeight) {
ViewGroup.LayoutParams layoutParams = mViewPager.getLayoutParams();
layoutParams.height = newHeight;
mViewPager.setLayoutParams(layoutParams);
}
}
}
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
return METRICS_CATEGORY_UNKNOWN; return METRICS_CATEGORY_UNKNOWN;

View File

@@ -50,4 +50,9 @@ public class ProfileSelectLocationServicesFragment extends ProfileSelectFragment
protected int getPreferenceScreenResId() { protected int getPreferenceScreenResId() {
return R.xml.location_services_header; return R.xml.location_services_header;
} }
@Override
protected boolean forceUpdateHeight() {
return true;
}
} }

View File

@@ -20,7 +20,6 @@ import android.content.Context;
import android.os.UserHandle; import android.os.UserHandle;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.widget.RestrictedAppPreference; import com.android.settings.widget.RestrictedAppPreference;
@@ -41,8 +40,6 @@ public class LocationInjectedServicesForWorkPreferenceController extends
@Override @Override
protected void injectLocationServices(PreferenceScreen screen) { protected void injectLocationServices(PreferenceScreen screen) {
final PreferenceCategory categoryLocationServices =
screen.findPreference(getPreferenceKey());
final Map<Integer, List<Preference>> prefs = getLocationServices(); final Map<Integer, List<Preference>> prefs = getLocationServices();
for (Map.Entry<Integer, List<Preference>> entry : prefs.entrySet()) { for (Map.Entry<Integer, List<Preference>> entry : prefs.entrySet()) {
for (Preference pref : entry.getValue()) { for (Preference pref : entry.getValue()) {
@@ -51,7 +48,7 @@ public class LocationInjectedServicesForWorkPreferenceController extends
} }
} }
if (entry.getKey() != UserHandle.myUserId()) { if (entry.getKey() != UserHandle.myUserId()) {
LocationSettings.addPreferencesSorted(entry.getValue(), categoryLocationServices); LocationSettings.addPreferencesSorted(entry.getValue(), screen);
} }
} }
} }

View File

@@ -19,7 +19,6 @@ import android.content.Context;
import android.os.UserHandle; import android.os.UserHandle;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.widget.RestrictedAppPreference; import com.android.settings.widget.RestrictedAppPreference;
@@ -41,8 +40,6 @@ public class LocationInjectedServicesPreferenceController
@Override @Override
protected void injectLocationServices(PreferenceScreen screen) { protected void injectLocationServices(PreferenceScreen screen) {
final PreferenceCategory categoryLocationServices =
screen.findPreference(getPreferenceKey());
final Map<Integer, List<Preference>> prefs = getLocationServices(); final Map<Integer, List<Preference>> prefs = getLocationServices();
for (Map.Entry<Integer, List<Preference>> entry : prefs.entrySet()) { for (Map.Entry<Integer, List<Preference>> entry : prefs.entrySet()) {
for (Preference pref : entry.getValue()) { for (Preference pref : entry.getValue()) {
@@ -51,10 +48,7 @@ public class LocationInjectedServicesPreferenceController
} }
} }
if (entry.getKey() == UserHandle.myUserId()) { if (entry.getKey() == UserHandle.myUserId()) {
if (categoryLocationServices != null) { LocationSettings.addPreferencesSorted(entry.getValue(), screen);
LocationSettings.addPreferencesSorted(entry.getValue(),
categoryLocationServices);
}
} }
} }
} }

View File

@@ -22,12 +22,8 @@ import android.content.Context;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/** /**
* A page that configures the Location Services settings including Wi-Fi scanning, Bluetooth * A page that configures the Location Services settings including Wi-Fi scanning, Bluetooth
* scanning, and injected location services. * scanning, and injected location services.
@@ -51,11 +47,6 @@ public class LocationServices extends DashboardFragment {
return TAG; return TAG;
} }
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context);
}
@Override @Override
public void onAttach(Context context) { public void onAttach(Context context) {
super.onAttach(context); super.onAttach(context);
@@ -63,23 +54,9 @@ public class LocationServices extends DashboardFragment {
use(LocationInjectedServicesPreferenceController.class).init(this); use(LocationInjectedServicesPreferenceController.class).init(this);
} }
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new WifiScanningPreferenceController(context));
controllers.add(new BluetoothScanningPreferenceController(context));
return controllers;
}
/** /**
* For Search. * For Search.
*/ */
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.location_services) { new BaseSearchIndexProvider(R.xml.location_services);
@Override
public List<AbstractPreferenceController> createPreferenceControllers(Context
context) {
return buildPreferenceControllers(context);
}
};
} }

View File

@@ -22,12 +22,8 @@ import android.content.Context;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/** /**
* A page that configures the Location Services settings for work profile. * A page that configures the Location Services settings for work profile.
*/ */
@@ -50,32 +46,15 @@ public class LocationServicesForWork extends DashboardFragment {
return TAG; return TAG;
} }
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context);
}
@Override @Override
public void onAttach(Context context) { public void onAttach(Context context) {
super.onAttach(context); super.onAttach(context);
use(LocationInjectedServicesForWorkPreferenceController.class).init(this); use(LocationInjectedServicesForWorkPreferenceController.class).init(this);
} }
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
return controllers;
}
/** /**
* For Search. * For Search.
*/ */
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.location_services_workprofile) { new BaseSearchIndexProvider(R.xml.location_services_workprofile);
@Override
public List<AbstractPreferenceController> createPreferenceControllers(Context
context) {
return buildPreferenceControllers(context);
}
};
} }