【问题标题】:The height of list-view in card-view is changing after scrolling卡片视图中列表视图的高度在滚动后发生变化
【发布时间】:2019-01-31 01:17:13
【问题描述】:

我正在使用包含列表视图的卡片视图。列表视图项目计数应根据用户的输入进行更改,因此我将列表视图高度设置为 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>

匹配项.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" />

【问题讨论】:

  • 能否附上 R.layout.match_item 的 xml 文件?
  • @VishalArora 如何附加 R.layout.match_item?
  • 就像你为cardview添加xml一样
  • 我添加了 match-item.xml

标签: android listview android-cardview


【解决方案1】:

尝试将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>

匹配项.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" />

【讨论】:

  • 当我将 layout_height 更改为 wrap-content 时,只会出现列表视图的第一项。
  • 好的。然后请按照@VishalArora 的建议附上您的 R.layout.match_item xml
  • 我添加了 match-item.xml
【解决方案2】:

我通过以编程方式更改列表视图大小解决了这个问题。首先,我在 card-view.xml 文件中将列表视图的高度设置为 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 文件。我分享这个解决方案是因为我在互联网上的搜索中看到其他人面临同样的问题。有不同解决方案的也可以回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    相关资源
    最近更新 更多