[英]Adnroid ViewPager2 wrap content of asynchronous fragment child
我在滾動視圖中有一個 ViewPager2 小部件,我想將其設置為類似於當前顯示片段的內容的高度。 片段包含 gridview 或高度設置為 wrap_content 的列表視圖。
問題是 gridview 在創建片段視圖后被內容填充,因此 viewpager 的子高度為“0”。 每個片段的高度也可以與其他片段不同。
如果我將 gridview 高度設置為例如 500dp 一切正常。
是否有某種偵聽器可以檢測 gridview 何時充滿內容/ gridview 的高度已更改為將 viewpager 高度設置為類似於片段的高度?
我已經嘗試過這里描述的解決方案
如何將 Android ViewPager2 的高度包裝到當前項目的高度? 和
具有不同項目高度和 WRAP_CONTENT 的 ViewPager2
布局:
瀏覽器:
<RelativeLayout>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewPagerID"
android:background="@color/light_blue">
</androidx.viewpager2.widget.ViewPager2>
</RelativeLayout>
滾動視圖:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/top_navigation_view_bar"
android:id="@+id/center"
android:layout_above="@id/bottom_navigation_view_bar">
<include layout="@layout/layout_viewpager"> </include>
</ScrollView>
測試片段:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:id="@+id/grid_fragment_0"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="top"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
</GridView>
</RelativeLayout>
設置 ViewPager
活動中的Function
private void setUpFragmentsTest(){
String[] fragment_names = {"Fragment0", "Fragment1", "Fragment2"};
mViewPager2 = (ViewPager2) findViewById(R.id.viewPagerID);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
SectionsPagerAdapterNew sectionsPagerAdapterNew = new SectionsPagerAdapterNew(this);
sectionsPagerAdapterNew.addFragment(new Fragment0());
sectionsPagerAdapterNew.addFragment(new Fragment1());
sectionsPagerAdapterNew.addFragment(new Fragment2());
mViewPager2.setAdapter(sectionsPagerAdapterNew);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout, mViewPager2, (tab, position) -> {
tab.setText(fragment_names[position]);
});
tabLayoutMediator.attach();
}
部分尋呼機適配器class:
public class SectionsPagerAdapterNew extends FragmentStateAdapter {
private static final String TAG = "SectionsPagerAdapter";
private final List<Fragment> mFragmentList = new ArrayList<>();
public SectionsPagerAdapterNew(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
public SectionsPagerAdapterNew(@NonNull Fragment fragment) {
super(fragment);
}
public SectionsPagerAdapterNew(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mFragmentList.get(position);
}
@Override
public int getItemCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment){
mFragmentList.add(fragment);
}
}
所以我放棄了在 xml 布局中設置 ViewPager2 的高度。 在我的情況下,我只需要 ViewPager 來覆蓋任何導航都不使用的空閑區域。 因此,我根據顯示大小和比例計算了 viewpager 的高度像素,以適應頂部 header / TabLayout 和底部導航(您將在下面的代碼中看到)。
變化:
ViewPager2 小部件:
<RelativeLayout>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/viewPagerID"
android:background="@color/light_blue">
</androidx.viewpager2.widget.ViewPager2>
</RelativeLayout>
Function 里面的活動:
private void setUpFragmentsTest(){
//SET UP THE VIEWPAGER THAT HOLDS THE PROFILE CONTENT FRAGMENTS
String[] fragment_names = {"Fragment0", "Fragment1", "Fragment2"};
mViewPager2 = (ViewPager2) findViewById(R.id.activity_profile_viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.activity_profile_tab_layout);
SectionsPagerAdapterNew sectionsPagerAdapterNew = new SectionsPagerAdapterNew(this);
sectionsPagerAdapterNew.addFragment(new Fragment0());
sectionsPagerAdapterNew.addFragment(new Fragment1());
sectionsPagerAdapterNew.addFragment(new Fragment2());
//SET THE VIEWPAGER HEIGHT RELATIVE TO THE DISPLAY METRICS & DISPLAY RATIO
//GET THE METRICS & DISPLAY RATIO USED BY THE DIFFERENT NAVIGATION BARS
ViewGroup.LayoutParams tabLayoutLayoutParams = tabLayout.getLayoutParams();
ViewGroup.LayoutParams navigationViewLayoutParams = findViewById(R.id.bottom_navigation_view_bar).getLayoutParams();
Resources resources = ProfileActivity.this.getResources();
int androidTopBarID = resources.getIdentifier("status_bar_height", "dimen", "android");
float androidTopBarHeight = resources.getDimensionPixelSize(androidTopBarID);
//SETTING THE ACTUAL VIEWPAGER HEIGHT
float pixelsUsedByNavigation = tabLayoutLayoutParams.height + (2* navigationViewLayoutParams.height);
//2* navigationViewLayoutParams.height because I'm using a top and bottom navigation bar, if there is just 1 don't double that value
ViewGroup.LayoutParams viewPagerLayoutParams = mViewPager2.getLayoutParams();
DisplayMetrics displayMetrics = ProfileActivity.this.getResources().getDisplayMetrics();
float viewpagerHeight;
float ratio = ((float)displayMetrics.heightPixels / (float)displayMetrics.widthPixels);
if(ratio > 1.667){
//LONG DISPLAY RATIO
viewpagerHeight = displayMetrics.heightPixels - pixelsUsedByNavigation;
}
else {
//NOT LONG DISPLAY RATIO
viewpagerHeight = displayMetrics.heightPixels - pixelsUsedByNavigation - androidTopBarHeight;
}
viewPagerLayoutParams.height = Math.round(viewpagerHeight);
mViewPager2.setLayoutParams(viewPagerLayoutParams);
mViewPager2.setAdapter(sectionsPagerAdapterNew);
//SET UP THE TAB LAYOUT TO DISPLAY AND HIGHLIGHT THE CURRENT FRAGMENT NAME
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout, mViewPager2, (tab, position) -> {
tab.setText(fragment_names[position]);
});
tabLayoutMediator.attach();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.