滚动后卡片视图中列表视图的高度发生变化

The height of list-view in card-view is changing after scrolling

我正在使用包含列表视图的卡片视图。列表视图项数应根据用户输入进行更改,因此我将列表视图高度设置为 wrap_content。在用户滚动之前一切正常,但滚动后列表视图或卡片视图的高度正在改变。

这是我的 xml 和卡片视图适配器代码块;

卡片视图适配器

public class RoundItemAdapter extends RecyclerView.Adapter<RoundItemAdapter.RoundItemViewHolder> {

private ArrayList<Round> mRounds;
private Context context;

public RoundItemAdapter(Context context, ArrayList<Round> rounds){
    mRounds = rounds;
    this.context = context;

}

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

@Override
public void onBindViewHolder(@NonNull RoundItemViewHolder roundItemViewHolder, int position) {
    Round currentItem = mRounds.get(position);

    roundItemViewHolder.mTextViewTitle.setText("Round: " + currentItem.number);

    MatchItemAdapter matchItemAdapter = new MatchItemAdapter(context, R.layout.match_item, currentItem.matches);
    roundItemViewHolder.mListViewRound.setAdapter(matchItemAdapter);
    matchItemAdapter.notifyDataSetChanged();


}

@Override
public int getItemCount() {
    return mRounds.size();
}



public static class RoundItemViewHolder extends RecyclerView.ViewHolder{

    public TextView mTextViewTitle;
    public ListView mListViewRound;
    public RoundItemViewHolder(View itemView){
        super(itemView);
        mTextViewTitle = itemView.findViewById(R.id.textView_rounds_title);
        mListViewRound = itemView.findViewById(R.id.listView_rounds);
      }
    }}

Xml 卡片视图

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="6dp"
android:layout_margin="4dp">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/textView_rounds_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title"
        android:textSize="16sp"
        android:background="@android:color/darker_gray"
        android:textColor="@android:color/white"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:paddingStart="4dp"
        android:paddingEnd="4dp"
        android:textStyle="bold"/>

    <ListView
        android:id="@+id/listView_rounds"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" />
</LinearLayout>

匹配-item.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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView_player_home_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:paddingLeft="8dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    android:text="@string/empty"
    android:textAlignment="viewEnd"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textView_vs"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView_player_away_name"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:drawableEnd="@drawable/ic_navigate_next_gray_24dp"
    android:drawableRight="@drawable/ic_navigate_next_gray_24dp"
    android:paddingLeft="8dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    android:text="@string/empty"
    android:textAlignment="viewStart"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/textView_player_home_name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/textView_vs"
    app:layout_constraintTop_toTopOf="@+id/textView_player_home_name" />

<TextView
    android:id="@+id/textView_vs"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:paddingLeft="8dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    android:text="vs"
    android:textAlignment="center"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/textView_player_home_name"
    app:layout_constraintEnd_toStartOf="@+id/textView_player_away_name"
    app:layout_constraintStart_toEndOf="@+id/textView_player_home_name"
    app:layout_constraintTop_toTopOf="@+id/textView_player_home_name" />

尝试将 CardViewConstraintLayoutlayout_height 约束从 match_parent 更改为 wrap_content

Xml 卡片视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="6dp"
android:layout_margin="4dp">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/textView_rounds_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title"
        android:textSize="16sp"
        android:background="@android:color/darker_gray"
        android:textColor="@android:color/white"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:paddingStart="4dp"
        android:paddingEnd="4dp"
        android:textStyle="bold"/>

    <ListView
        android:id="@+id/listView_rounds"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" />
</LinearLayout>

匹配-item.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"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/textView_player_home_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:paddingLeft="8dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    android:text="@string/empty"
    android:textAlignment="viewEnd"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textView_vs"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView_player_away_name"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:drawableEnd="@drawable/ic_navigate_next_gray_24dp"
    android:drawableRight="@drawable/ic_navigate_next_gray_24dp"
    android:paddingLeft="8dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    android:text="@string/empty"
    android:textAlignment="viewStart"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/textView_player_home_name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/textView_vs"
    app:layout_constraintTop_toTopOf="@+id/textView_player_home_name" />

<TextView
    android:id="@+id/textView_vs"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:paddingLeft="8dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    android:text="vs"
    android:textAlignment="center"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/textView_player_home_name"
    app:layout_constraintEnd_toStartOf="@+id/textView_player_away_name"
    app:layout_constraintStart_toEndOf="@+id/textView_player_home_name"
    app:layout_constraintTop_toTopOf="@+id/textView_player_home_name" />

我通过以编程方式更改列表视图大小解决了这个问题。首先我在card-view.xml文件中设置了listview的高度为55dp(列表视图中一行的高度为55dp)。在我更改 RoundItemAdapter class 中的 onBindViewHolder 方法之后。 onBindViewHolder方法修改后的样子

    @Override
public void onBindViewHolder(@NonNull RoundItemViewHolder roundItemViewHolder, int position) {
    Round currentItem = mRounds.get(position);

    roundItemViewHolder.mTextViewTitle.setText("Round: " + currentItem.number);

    MatchItemAdapter matchItemAdapter = new MatchItemAdapter(context, R.layout.match_item, currentItem.matches);
    roundItemViewHolder.mListViewRound.setAdapter(matchItemAdapter);
    matchItemAdapter.notifyDataSetChanged();

    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)roundItemViewHolder.mListViewRound.getLayoutParams();
    lp.height = lp.height * mRounds.get(position).matches.size();
    roundItemViewHolder.mListViewRound.setLayoutParams(lp);

}

我也尝试了很多修改XML文件来解决这个问题,但我没有。我相信应该有另一种方法可以通过修改 xml 文件来解决这个问题。我分享这个解决方案是因为我在互联网上的搜索中看到有其他人面临同样的问题。有不同解决方案的也可以回答。