如何在ViewPager android中设置片段包含RecycleView的动态高度?
我得到了来自和其他链接的帮助,但没有帮助我,我的问题也没有得到解决 我的问题:我的ViewPager有3个片段,其中2个包含动态数据的RecycleView 现在ViewPage获得了最大的高度!!!!我想将碎片的高度设置为原始大小 我的代码:如何在ViewPager android中设置片段包含RecycleView的动态高度?,android,android-fragments,android-recyclerview,android-viewpager,Android,Android Fragments,Android Recyclerview,Android Viewpager,我得到了来自和其他链接的帮助,但没有帮助我,我的问题也没有得到解决 我的问题:我的ViewPager有3个片段,其中2个包含动态数据的RecycleView 现在ViewPage获得了最大的高度!!!!我想将碎片的高度设置为原始大小 我的代码: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout styl
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
style="@style/CardStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.duolingo.open.rtlviewpager.RtlViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_marginTop="@dimen/default_margin_double"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tab_layout" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
style="@style/TabbedCard"
android:layout_width="match_parent"
android:layout_height="@dimen/tabbed_card_tab_layout_height"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabPaddingTop="@dimen/default_margin_half"
app:tabTextAppearance="@style/investTab" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scrollbarStyle="insideOverlay"
android:orientation="vertical"
tools:context=".mvvm.screens.investment.view.fragment.tabs.RequestListFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/account_list"
style="@style/InvestmentRecyclerView"
android:scrollbarStyle="insideOverlay"
tools:listitem="@layout/investment_request_item"
android:paddingBottom="@dimen/default_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/loading"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="?attr/cardTitle"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="@+id/view_error"
layout="@layout/view_error"
android:visibility="gone" />
<include
android:id="@+id/view_empty"
layout="@layout/no_investment_found"
android:visibility="gone" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
和我的一个包含RecyclerView的片段:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
style="@style/CardStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.duolingo.open.rtlviewpager.RtlViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_marginTop="@dimen/default_margin_double"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tab_layout" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
style="@style/TabbedCard"
android:layout_width="match_parent"
android:layout_height="@dimen/tabbed_card_tab_layout_height"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabPaddingTop="@dimen/default_margin_half"
app:tabTextAppearance="@style/investTab" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scrollbarStyle="insideOverlay"
android:orientation="vertical"
tools:context=".mvvm.screens.investment.view.fragment.tabs.RequestListFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/account_list"
style="@style/InvestmentRecyclerView"
android:scrollbarStyle="insideOverlay"
tools:listitem="@layout/investment_request_item"
android:paddingBottom="@dimen/default_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/loading"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="?attr/cardTitle"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="@+id/view_error"
layout="@layout/view_error"
android:visibility="gone" />
<include
android:id="@+id/view_empty"
layout="@layout/no_investment_found"
android:visibility="gone" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
最后,我从ViewPager创建自定义类,如下所示:
public class WrappedHeightTabbedCardViewPager extends NonSwipeableViewPager {
private int currentPagePosition = 0;
public WrappedHeightTabbedCardViewPager(@NonNull Context context) {
super(context);
}
public WrappedHeightTabbedCardViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
View child = getChildAt(currentPagePosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
if (heightMeasureSpec != 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void reMeasureCurrentPage(int position) {
currentPagePosition = position;
requestLayout();
}
}
最后,我从ViewPager创建自定义类,如下所示:
public class WrappedHeightTabbedCardViewPager extends NonSwipeableViewPager {
private int currentPagePosition = 0;
public WrappedHeightTabbedCardViewPager(@NonNull Context context) {
super(context);
}
public WrappedHeightTabbedCardViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
View child = getChildAt(currentPagePosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
if (heightMeasureSpec != 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void reMeasureCurrentPage(int position) {
currentPagePosition = position;
requestLayout();
}
}