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

时间:2019-01-28 15:30:48

标签: android listview android-cardview

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

Before Scroll After Scroll

这是我的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>

Match-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" />

2 个答案:

答案 0 :(得分:0)

尝试将layout_heightCardView的{​​{1}}约束从ConstraintLayout更改为match_parent

用于卡片视图的Xml:

wrap_content

Match-item.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>

答案 1 :(得分:0)

我通过以编程方式更改listview的大小解决了这个问题。首先,在card-view.xml文件中将listview的高度设置为55dp(列表视图中一行的高度为55dp)。在更改RoundItemAdapter类中的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文件即可解决此问题。之所以共享此解决方案,是因为我发现在Internet上进行搜索时还有其他人面临同样的问题。那些拥有不同解决方案的人也可以回答。