如何解决视图寻呼机滚动在 Android 的滚动视图中不起作用
我使用带有选项卡布局的视图分页器,每个选项卡都有片段(带列表视图的片段)。 在视图寻呼机内部滚动不起作用。我只想在视图寻呼机内启用滚动功能。 请给我建议。在这里,我还想通知您,Listview 的大小无法修复。
<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>
解决方法
滚动视图必须仅用于孩子。尝试删除上方的线性滚动
Documentation about Scroll View
,这个解决方案对我有用。
第 1 步:创建一个扩展 viewPager 的类。
第 2 步:在您的 XML 中使用这个类
第 3 步:在扩展 FragmentPagerAdapter 的 ViewPager 适配器类中使用 setPrimaryItem() 函数
步骤 - 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();
}
}
步骤 - 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"/>
步骤 - 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());
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。