Podążałem za odpowiedziami this i that, znalazłem także link this.Viewpager z dynamiczną wysokością nie działa (zawsze używaj wysokości pierwszego fragmentu)
Używam tych zasobów, aby wykonać próbne błędy &. Teraz mój niestandardowy ViewPager
z powodzeniem mierzy zawartość, , ale tylko pierwsza wyświetlana. FYI, mój ViewPager
posiada pewne skomplikowane widoki, takie jak ExpendableListView
- to dlatego żaden kod w tych zasobach nie działa idealnie, muszę zmodyfikować kod sam.
Mam 4 fragmenty (treść), więc używam pager.setOffscreenPageLimit(3);
To jest mój zwyczaj ViewPager
:
public class CustomViewPager extends ViewPager{
public CustomViewPager (Context context) {
super(context);
}
public CustomViewPager (Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
final View tab = getChildAt(0);
int width = getMeasuredWidth();
int tabHeight = tab.getMeasuredHeight();
if (wrapHeight) {
// Keep the current measured width.
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
}
int fragmentHeight = measureFragment(((Fragment) getAdapter().instantiateItem(this, getCurrentItem())).getView());
heightMeasureSpec = MeasureSpec.makeMeasureSpec(tabHeight + fragmentHeight + (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()), MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public int measureFragment(View view) {
if (view == null)
return 0;
view.measure(0, 0);
return view.getMeasuredHeight();
}
}
To jest mój CustomPagerTabStrip
:
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
android.view.ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
I to jest mój XML :
<RelativeLayout
android:id="@+id/relativePager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.xxx.yyy.util.CustomViewPager
android:id="@+id/detailPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.xxx.yyy.util.CustomPagerTabStrip
android:id="@+id/detailTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#161C28"
android:textColor="#FFFFFF" />
</com.xxx.yyy.util.CustomViewPager>
</RelativeLayout>
przykładowym przypadku:
Jeśli wysokość pierwszy fragment jest 100px, to wszystkie inne fragmenty będą pozostawać na 100px. Jeśli więc wysokość drugiego fragmentu wynosi 130 pikseli, zostanie ono przycięte do 100 pikseli.
Kiedy próbowałem debugowania The onMeasure
na moich CustomViewPager
zawsze zwanych 4 razy, gdy Activity
jest tworzony, ale wszystkie z nich odczytać tylko pierwszego fragmentu. nie można uzyskać ponownie po tym, nawet jeśli zmieniam (przesuń w lewo/w prawo) z jednego fragmentu na inny.
Proszę mi pomóc, poświęciłem wiele godzin, aby to zadziałało. Po prostu zapytaj mnie, czy potrzebujesz więcej informacji, dziękuję za poświęcony czas.
Proszę o pomoc? –
Witam lubię wiedzieć, jak rozwiązać ten problem .. –
@RenjithKN Niestety nie udało mi się rozwiązać tego problemu, więc używam innego podejścia (nie używając jakiegoś złożonego widoku w widoku pagera) :( –