1

enter image description here里面一个DialogFragment的RecyclerView项宽度滚动

我已经附上DialogFragment的3个屏前收缩。在这个对话框中,我想显示一个RecyclerView。当对话框打开时,前两项的宽度会缩小。但是当第三项出现时有一点滚动后,它会以预期的宽度显示[查看屏幕截图1 & 2]。如果我继续从上到下滚动,那么我会看到通常显示的其余项目。然后我再次从下到上滚动,并且意外地注意到第一项和第二项也按预期显示。
[查看屏幕截图编号。 3]

我想分享我的代码。

从我的适配器类:

@Override 
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false); 
    return new CustomViewHolder(view); 
} 

item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:paddingTop="8dp" 
    android:paddingStart="16dp" 
    android:paddingEnd="16dp"> 

    <TextView 
     android:id="@+id/modalityTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toRightOf="@id/modalityTitle" 
     tools:text="X-Ray" 
     android:textSize="22sp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="4dp"/> 

    <ImageView 
     android:id="@+id/statusImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toTopOf="@id/modalityTextView" 
     app:layout_constraintBottom_toBottomOf="@id/modalityTextView" 
     app:layout_constraintRight_toRightOf="parent" 
     android:src="@drawable/ic_remove_circle_outline_black_24dp"/> 

    <TextView 
     android:id="@+id/contrastTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@id/modalityTextView" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:text="@string/contrast" 
     android:textStyle="bold" 
     android:layout_marginTop="4dp"/> 

    <TextView 
     android:id="@+id/contrastTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@id/modalityTextView" 
     app:layout_constraintLeft_toRightOf="@id/contrastTitle" 
     tools:text="Oral contrast" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="4dp"/> 


    <TextView 
     android:id="@+id/bodyPartTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@id/contrastTitle" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:text="@string/bodyPart" 
     android:textStyle="bold" 
     android:layout_marginTop="4dp"/> 

    <TextView 
     android:id="@+id/bodyPartTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@id/contrastTextView" 
     app:layout_constraintLeft_toRightOf="@id/bodyPartTitle" 
     tools:text="Chest" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="4dp"/> 

    <TextView 
     android:id="@+id/procedureTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@id/bodyPartTitle" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:text="@string/procedureDescription" 
     android:textStyle="bold" 
     android:layout_marginTop="4dp"/> 

    <TextView 
     android:id="@+id/procedureDescriptionTextView" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@id/bodyPartTitle" 
     app:layout_constraintLeft_toRightOf="@id/procedureTitle" 
     app:layout_constraintRight_toRightOf="parent" 
     android:text="This is a procedure description. It will be a long text" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="4dp"/> 

    <TextView 
     android:id="@+id/checkedInAtTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@id/procedureDescriptionTextView" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:text="@string/checked_in_at" 
     android:textStyle="bold" 
     android:layout_marginTop="4dp"/> 

    <TextView 
     android:id="@+id/checkedInAtTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@id/procedureDescriptionTextView" 
     app:layout_constraintLeft_toRightOf="@id/checkedInAtTitle" 
     tools:text="5:00 PM" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="4dp"/> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="2dp" 
     android:background="@color/grey" 
     app:layout_constraintTop_toBottomOf="@id/checkedInAtTextView" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginTop="8dp"/> 

</android.support.constraint.ConstraintLayout> 

DialogFragment XML:

<?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="400dp" 
    tools:context="com.alemhealth.ticketcapture.Features.CheckInListShow.StudyListDialog.CheckInDialogFragment"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="0dp" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/colorPrimary" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent"> 

     <TextView 
      android:id="@+id/toolbarTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/check_in_information" 
      android:textColor="@color/white" 
      android:textSize="18sp" /> 

    </android.support.v7.widget.Toolbar> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/studyListRecyclerView" 
     android:layout_width="0dp" 
     android:layout_height="250dp" 
     app:layout_constraintTop_toBottomOf="@id/toolbar" 
     app:layout_constraintBottom_toTopOf="@+id/closeButton" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginBottom="16dp"> 

    </android.support.v7.widget.RecyclerView> 

    <Button 
     android:id="@+id/closeButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@id/studyListRecyclerView" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:background="@drawable/custom_button" 
     android:textColor="@color/buttonTextColor" 
     android:text="@string/close" 
     android:layout_marginBottom="8dp"/> 


</android.support.constraint.ConstraintLayout> 

为DialogFragment自定义主题:

<style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog"> 
    <item name="android:windowMinWidthMajor">57%</item> 
    <item name="android:windowMinWidthMinor">57%</item> 
    <item name="android:textColor">@color/text_color</item> 
</style> 

DialogFragment的onCreateView:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_check_in_dialog, container, false); 

     StudyListForDialog studyListForDialog = (StudyListForDialog) getArguments().getSerializable("data"); 

     if(studyListForDialog!=null){ 
      toolbarTextView.setText(studyListForDialog.getPatientName() + " - " + studyListForDialog.getPatientAge() + " - " + studyListForDialog.getPatientGender()); 

      studyListRecyclerViewAdapter = new StudyListRecyclerViewAdapter(studyListForDialog, getActivity(), this); 
      recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false)); 
      recyclerView.setAdapter(studyListRecyclerViewAdapter); 
     }  

     return view; 
    } 

并以这种方式我打开我的DialogFragment:

Bundle bundle = new Bundle(); 
bundle.putSerializable("data", data); 

FragmentManager fragmentManager = ((Activity)view.getContext()).getFragmentManager(); 

CheckInDialogFragment checkInDialogFragment = new CheckInDialogFragment(); 
checkInDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle); 
checkInDialogFragment.setArguments(bundle); 
checkInDialogFragment.show(fragmentManager, "check-in"); 

请帮我解决这个问题奇怪的问题。

回答

2

可能是ConstaintLayout的错误。

我更新了我的DialogFragment XML根LinearLayout而不是ConstraintLayout。它解决了收缩的问题。

我的更新DialogFragment XML是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/colorPrimary"> 

     <TextView 
      android:id="@+id/toolbarTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/check_in_information" 
      android:textColor="@color/white" 
      android:textSize="18sp" /> 

    </android.support.v7.widget.Toolbar> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/studyListRecyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="250dp" 
     app:layout_constraintTop_toBottomOf="@id/toolbar" 
     app:layout_constraintBottom_toTopOf="@+id/closeButton" 
     android:layout_marginBottom="16dp"> 

    </android.support.v7.widget.RecyclerView> 

    <Button 
     android:id="@+id/closeButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/custom_button" 
     android:textColor="@color/buttonTextColor" 
     android:text="@string/close" 
     android:layout_marginBottom="8dp" 
     android:layout_gravity="center"/> 

</LinearLayout> 
相关问题