我已经尝试了StackOverflow中提供的许多解决方案,但该解决方案无法满足我的期望。我想禁用“回收者视图滚动”,仅允许嵌套的scrollView滚动我的内容。
下面是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent"
android:clickable="true"
android:focusable="true"
android:background="@color/backgroundColor">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clProfileInfoContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_divider"
android:paddingStart="@dimen/ps_container_padding_start"
android:paddingTop="@dimen/ps_container_padding_top"
android:paddingEnd="@dimen/ps_container_padding_end"
android:paddingBottom="@dimen/ps_container_padding_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/ivPhotoSelector"
android:layout_width="@dimen/ps_photo_selector_max_width"
android:layout_height="@dimen/ps_photo_selector_max_height"
android:contentDescription="@string/cd_photo_selector"
android:maxWidth="@dimen/ps_photo_selector_max_width"
android:maxHeight="@dimen/ps_photo_selector_max_height"
android:src="@drawable/blue_profile_img_placeholder"
app:civ_circle_background_color="@color/colorAccent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/llUsernameContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="@dimen/ps_username_margin_start"
android:layout_marginTop="@dimen/ps_username_margin_top"
app:layout_constraintStart_toEndOf="@+id/ivPhotoSelector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_vertical"
android:text="Username"
android:textColor="@color/tvMainTextColor"
android:textSize="@dimen/ps_username_text_size"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/ivPhotoSelector"
app:layout_constraintTop_toTopOf="@+id/ivPhotoSelector" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/btnEditProfile"
style="@style/AppTheme.Button.Blue"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="@dimen/small_button_height"
android:layout_gravity="end"
android:src="@drawable/edit_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/llLocationContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginStart="@dimen/ps_location_margin_start"
android:layout_marginTop="@dimen/ps_location_margin_top"
app:layout_constraintStart_toEndOf="@+id/ivPhotoSelector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/llUsernameContainer" >
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvLocation"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Location"
android:textColor="@color/tvSubTextColor"
android:textSize="@dimen/ps_location_text_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnShare"
style="@style/AppTheme.Button.Blue"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="@dimen/small_button_height"
android:minWidth="@dimen/small_share_button_min_width"
android:text="@string/ps_label_share_profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.appcompat.widget.LinearLayoutCompat>
<me.zhanghai.android.materialratingbar.MaterialRatingBar
android:id="@+id/rbRating"
style="@style/Widget.MaterialRatingBar.RatingBar.Indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/ps_star_rating_margin_start"
android:layout_marginTop="@dimen/ps_star_rating_margin_top"
android:layout_marginEnd="@dimen/ps_star_rating_margin_end"
android:maxHeight="@dimen/ps_star_rating_max_height"
android:minHeight="@dimen/ps_star_rating_min_height"
android:numStars="5"
android:rating="4"
app:layout_constraintStart_toEndOf="@+id/ivPhotoSelector"
app:layout_constraintTop_toBottomOf="@+id/llLocationContainer"
app:mrb_fillBackgroundStars="true"
app:mrb_progressBackgroundTint="@color/rbBackgroundColor"
app:mrb_progressTint="@color/rbProgressBackgroundColor" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clListingContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/white"
android:paddingStart="@dimen/ps_container_padding_start"
android:paddingTop="@dimen/ps_container_padding_top"
android:paddingEnd="@dimen/ps_container_padding_end"
android:paddingBottom="@dimen/ps_container_padding_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/clProfileInfoContainer"
app:layout_constraintBottom_toBottomOf="parent">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etShortBio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/ps_bio_min_height"
android:gravity="top"
android:padding="@dimen/ps_bio_padding"
android:text="Short Bio About Yourself"
android:inputType="textMultiLine"
android:clickable="false"
android:focusable="false"
android:enabled="false"
android:textColor="@color/labelColor"
android:background="@drawable/white_background"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvListing"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/ps_listing_margin_top"
android:paddingTop="@dimen/ps_listing_padding_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etShortBio"
tools:listitem="@layout/row_user_listing_item">
</androidx.recyclerview.widget.RecyclerView>
<com.github.ybq.android.spinkit.SpinKitView
android:id="@+id/skUserListingLoading"
style="@style/SpinKitView.Wave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:SpinKit_Color="@color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/rvListing" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
在我的活动中,我将其设置为禁用回收站视图滚动
private void setupListing() {
// rvMenuList should not be null
assert rvListing != null;
// initialize Recycler View Adapter
userListingAdapter = new UserListingAdapter(getContext());
// initialize GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), gridSize);
// initialize custom divider item decorator
SpacesItemDecoration itemDecorator = new SpacesItemDecoration(10, 10, 30);
rvListing.addItemDecoration(itemDecorator);
ViewCompat.setNestedScrollingEnabled(rvListing, false);
// set the adapter to the recycler view
rvListing.setAdapter(userListingAdapter);
// set the layout manager to the recycler view
rvListing.setLayoutManager(gridLayoutManager);
}
这是它的样子
现在,由于使用了这段代码ViewCompat.setNestedScrollingEnabled(rvListing, false);
,我无法滚动回收器视图,而且我也无法滚动全部内容来查看回收器视图的其他项目。
答案 0 :(得分:0)
这可能对您有用,请应用布局行为:
<android.support.v7.widget.RecyclerView
android:id="@+id/conversation"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
答案 1 :(得分:0)
代替使用此行:
ViewCompat.setNestedScrollingEnabled(rvListing, false);
使用以下行:
rvListing.setNestedScrollingEnabled(false);
像这样设置适配器后
private void setupListing() {
// rvMenuList should not be null
assert rvListing != null;
// initialize Recycler View Adapter
userListingAdapter = new UserListingAdapter(getContext());
// initialize GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), gridSize);
// initialize custom divider item decorator
SpacesItemDecoration itemDecorator = new SpacesItemDecoration(10, 10, 30);
rvListing.addItemDecoration(itemDecorator);
// set the adapter to the recycler view
rvListing.setAdapter(userListingAdapter);
// set the layout manager to the recycler view
rvListing.setLayoutManager(gridLayoutManager);
rvListing.setHasFixedSize(true);
rvListing.setNestedScrollingEnabled(false);
}
使用RecyclerView宽度
android:layout_height="wrap_content"
答案 2 :(得分:0)
最终得到了解决方案。 rvListing.setNestedScrollingEnabled(false);
实际上有效。问题是父母身高,我不小心将其设置为0dp
。这就是解决此问题的方法。
答案 3 :(得分:-1)
只需按如下所示重写GridLayoutManager,然后调用方法setCanScroll(false)。
public class CustomLayoutManager extends GridLayoutManager {
private boolean canScroll;
public CustomLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
/**
*
* @param canScroll true--enable scroll, false--disable scroll
*/
public void setCanScroll(boolean canScroll) {
this.canScroll = canScroll;
}
@Override
public boolean canScrollHorizontally() {
return this.canScroll;
}
@Override
public boolean canScrollVertically() {
return false;
}
}
答案 4 :(得分:-2)
要禁用RecyclerView
的滚动,您可以通过编程方式进行:
recyclerView.setNestedScrollingEnabled(false);
或通过xml
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
但是当您的RecyclerView
包含很多物品时要小心。会导致滞后
禁用RecyclerView
滚动是一种不好的做法,请将RecyclerView
与多种项目类型结合使用!