当我第一次下载列表时,一切正常,但滚动后第二个和第三个元素之间的边距增加(列表大小为10个元素)。我发现这个代码可以做到,但我不明白为什么以及如何解决它(我需要在最后一个元素的末尾有边距):
我的适配器:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//...
// margin for the last element
if (position == getItemCount() - 1) {
Resources r = activity.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MARGIN_END_LAS_ELEMENT, r.getDisplayMetrics());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, (int) px, 0);
holder.itemView.setLayoutParams(layoutParams);
}
//...
}
的xml:
<android.support.v7.widget.RecyclerView
android:id="@+id/rvHoriz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
item xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llRoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="ContentDescription">
<RelativeLayout
android:layout_width="@dimen/video_longrid_film_image_w"
android:layout_height="@dimen/video_longrid_film_image_h"
android:layout_marginLeft="@dimen/video_longrid_film_image_margin"
android:layout_marginStart="@dimen/video_longrid_film_image_margin"
android:layout_marginTop="@dimen/video_longrid_film_image_margin_top">
<ImageView
android:id="@+id/ivPoster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_longrid_film_placeholder"/>
<ImageView
android:layout_width="@dimen/video_longrid_fav_icon_size"
android:layout_height="@dimen/video_longrid_fav_icon_size"
android:id="@+id/ivFavIcon"
android:src="@drawable/favorite"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="gone"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivParent"
android:layout_centerInParent="true"
android:src="@drawable/lock_poster"
android:visibility="gone"/>
</RelativeLayout>
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/video_longrid_film_image_margin_bottom"
android:layout_marginLeft="@dimen/video_longrid_film_image_margin"
android:layout_marginStart="@dimen/video_longrid_film_image_margin"
android:ellipsize="marquee"
android:includeFontPadding="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="0"
android:maxLines="1"
android:textColor="@color/video_longrid_title_color"
android:textSize="@dimen/video_longrid_film_text"/>
<TextView
android:id="@+id/tvGenre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:layout_marginTop="@dimen/video_longrid_film_title_margin_bottom"
android:layout_marginLeft="@dimen/video_longrid_film_image_margin"
android:layout_marginStart="@dimen/video_longrid_film_image_margin"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="0"
android:maxLines="1"
android:textColor="@color/video_longrid_genre_color"
android:textSize="@dimen/video_longrid_film_text"/>
</LinearLayout>
答案 0 :(得分:0)
您需要在条件语句中添加 else ,如下所示:
// Set margin for the last element
if (position == getItemCount() - 1) {
Resources r = activity.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MARGIN_END_LAS_ELEMENT, r.getDisplayMetrics());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, (int) px, 0);
holder.itemView.setLayoutParams(layoutParams);
} else {
// Set the margin for non-last element here
...
}
这是因为滚动时会重复使用视图,因此对视图所做的任何更改在重复使用时都会显示,除非您重置它。