我在DialogFragment中有一个RecyclerView。对话框片段和recyclerview都显示,但是当recyclerview超出视图容量时,它不会滚动。
?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:tag="FoodMenuTag"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.android.cop1803.MainActivity">
<View
android:id="@+id/divider3"
android:layout_width="1184dp"
android:layout_height="1dp"
android:layout_marginTop="@dimen/MarginTop_PDF_dividers"
android:layout_marginBottom="@dimen/MarginTop_PDF_dividers"
android:background="?android:attr/listDivider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/kCal"
tools:layout_editor_absoluteX="8dp" />
<com.example.android.cop1803.MyRecyclerView
android:id="@+id/recycler_menuview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constraintTop_toBottomOf="@id/divider3"
/>
这是我的对话框片段:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.recyclerview_menu, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_menuview);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle args = getArguments();
if(args != null) {
cartList = args.getParcelableArrayList("key");
MenuDialogFragmentAdapter adapter = new
MenuDialogFragmentAdapter(this.getActivity(), cartList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
}
@Override
public void onResume() {
// Get existing layout params for the window
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
// Assign window properties to fill the parent
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
// Call super onResume after sizing
super.onResume();}
非常感谢您提供帮助以使其正常工作。
谢谢
吉姆
答案 0 :(得分:0)
事实证明,我并没有考虑回收者视图的整个高度。因此它实际上正在工作,但是高度将其扩展到了底部边界之外。一旦我对高度进行了一些调整。这是所使用的XML。
<com.example.android.cop1803.MyRecyclerView
android:id="@+id/recycler_menuview"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:scrollbars="vertical"
app:layout_constraintVertical_bias="0.01"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider3" />
我不喜欢将高度设为固定值,因此很可能会通过代码进行更改。 谢谢, 吉姆
答案 1 :(得分:0)
尝试将嵌套滚动添加到recyclerview中,看看其是否有效。
recyclerView.setNestedScrollingEnabled(true);