Reputation: 322
I use an View Pager with Tab Layout and each tab has Fragment (Fragment with Listview). Scrolling inside view Pager not working .I want to just enable scrolling facility inside View Pager. Please give me suggestion. Here,I also want to inform you that Listview size can't be fix.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/_25sdp"
android:fillViewport="true"
android:paddingHorizontal="@dimen/_10sdp"
android:paddingTop="@dimen/_15sdp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_10sdp"
android:paddingLeft="@dimen/_5sdp"
android:text="@string/dashboard"
android:textColor="@color/black"
android:textSize="@dimen/_14ssp" />
<GridView
android:id="@+id/gv_dashboard"
android:layout_width="match_parent"
android:layout_height="@dimen/_175sdp"
android:horizontalSpacing="@dimen/_5sdp"
android:listSelector="@android:color/transparent"
android:numColumns="2"
android:scrollbars="none"
android:verticalSpacing="@dimen/_5sdp" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tl_comp_category"
android:layout_width="match_parent"
android:layout_height="@dimen/_25sdp"
android:layout_gravity="center"
android:layout_marginHorizontal="@dimen/_4sdp"
android:layout_marginVertical="@dimen/_10sdp"
app:tabBackground="@drawable/tab_background"
app:tabIndicator="@android:color/transparent"
app:tabMode="auto"
app:tabPaddingEnd="@dimen/_5sdp"
app:tabPaddingStart="@dimen/_5sdp"
app:tabTextAppearance="@style/ComCatTabText" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_tab"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
Upvotes: 0
Views: 318
Reputation: 322
This solution work for me.
STEP 1 : Create an Class which extends viewPager.
STEP 2 : Use this class in your XML
STEP 3 : use setPrimaryItem() function in your ViewPager Adapter Class which extends FragmentPagerAdapter
STEP - 1
public class CustomViewPager extends ViewPager {
private View mCurrentView;
public CustomViewPager(@NonNull Context context) {
super(context);
}
public CustomViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mCurrentView == null) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
int height = 0;
mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
int h = mCurrentView.getMeasuredHeight();
if (h > height) height = h;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void measureCurrentView(View currentView) {
mCurrentView = currentView;
requestLayout();
}
public int measureFragment(View view) {
if (view == null)
return 0;
view.measure(0, 0);
return view.getMeasuredHeight();
}
}
STEP - 2
<com.appName.widget.CustomViewPager
android:id="@+id/vp_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:overScrollMode="never"/>
STEP - 3
public class VPAdapter extends FragmentPagerAdapter {
// YOUR OTHER CODE
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if (position != mCurrentPosition) {
Fragment fragment = (Fragment) object;
CustomViewPager pager = (CustomViewPager) container;
if (fragment != null && fragment.getView() != null) {
mCurrentPosition = position;
pager.measureCurrentView(fragment.getView());
}
}
}
}
Upvotes: 1
Reputation: 169
Scroll View must be used with just a child. Try removing the Linear above Scroll
Documentation about Scroll View
Upvotes: 0