Sai*_*waz 12 android android-viewpager android-recyclerview android-tablayout
我有一个xml布局,其中包含以下视图Scrollview-> RelativeLayout-> Some Views + Tablayout + ViewPager-> Recylerview(在ViewPager的片段中).ViewPager有一些固定的高度(保持它"Wrap_Content"根本不显示它.)现在的问题是Recylerview永远不会滚动.我已经尝试了几个已经发布过的解决方案,例如在"嵌套Scrollview"中使用 Wraping viewpager 或者甚至使LinearLayout成为子项.但没有工作.
下面是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_startdraw"
android:fillViewport="true"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="@dimen/_5sdp">
<TextView
android:id="@+id/invites_accepted"
style="@style/bodyStyleBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/committee_slots"
android:text="@string/invites_accepted" />
<!-- A lot of other Textfields for data display-->
<android.support.design.widget.TabLayout
android:id="@+id/tabs_pendingcommittee"
style="@style/TabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/draw_mode_val"
app:tabMode="fixed"></android.support.design.widget.TabLayout>b
<com.apponative.committeeapp.ui.custom.CustomViewPager
android:id="@+id/pending_member_view"
android:layout_width="match_parent"
android:layout_height="@dimen/_250sdp"
android:layout_below="@+id/tabs_pendingcommittee"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<!-- Some other views on bottom-->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Viewpager显示的片段有以下布局xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
style="@style/bodyStyleBold1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No People..." />
<android.support.v7.widget.RecyclerView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:background="@color/white"></android.support.v7.widget.RecyclerView>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
我已经尝试在Recyclerview上使用nestedScrollingEnabled和setFixedHeight属性.但是没什么用的.
Sai*_*waz 16
我想通了,问题是不与RecyclerView内ViewPager或滚动型的,但问题是与ViewPager内滚动型.当我将ViewPager放在ScrollView中时,其Wrap_Content属性不起作用,因此固定高度限制RecyclerView内容完全显示.所以我通过自定义ViewPager的onMeasure方法解决了这个问题,并且它运行顺畅.无需包装NestedScroll视图或其他给定的解决方案.
下面是我的CustomView寻呼机的代码,它在作为子项添加到ScrollView时包装内容:
public class CustomViewPager extends ViewPager {
boolean canSwipe = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View child = getChildAt(getCurrentItem());
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void canSwipe(boolean canSwipe) {
this.canSwipe = canSwipe;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.canSwipe) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.canSwipe) {
return super.onInterceptTouchEvent(event);
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
添加NestedScrollView然后设置recyclerView.setNestedScrollingEnabled(false);
检查您是否已经拥有这个 android.support.v7.widget.RecyclerView
请记住,RecyclerView 元素有自己的滚动条
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
将 ScrollView 替换为android.support.v4.widget.NestedScrollView适用于所有版本。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<android.support.v4.widget.NestedScrollView
style="@style/ScrollBarStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_startdraw"
android:fillViewport="true"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="@dimen/_5sdp">
<TextView
android:id="@+id/invites_accepted"
style="@style/bodyStyleBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/committee_slots"
android:text="@string/invites_accepted" />
<!-- A lot of other Textfields for data display-->
<android.support.design.widget.TabLayout
android:id="@+id/tabs_pendingcommittee"
style="@style/TabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/draw_mode_val"
app:tabMode="fixed"></android.support.design.widget.TabLayout>b
<com.apponative.committeeapp.ui.custom.CustomViewPager
android:id="@+id/pending_member_view"
android:layout_width="match_parent"
android:layout_height="@dimen/_250sdp"
android:layout_below="@+id/tabs_pendingcommittee"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<!-- Some other views on bottom-->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我希望它有效!:) 如果不起作用,请告诉我!我会尝试找出为什么它不起作用:)祝你有美好的一天!
| 归档时间: |
|
| 查看次数: |
3447 次 |
| 最近记录: |