This is the individual item's layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#DDE6EC"
tools:context="com.example.personal.newsfeeder.MainActivity">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/section_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:id="@+id/card_view"
android:layout_gravity="center"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="10dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/avatar_section"
android:layout_width="match_parent"
android:layout_height="72dp"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/circular_image"
android:id="@+id/avatar_image"
/>
<TextView
android:id="@+id/avatar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/avatar_image"
android:layout_marginLeft="4dp"
tools:text="Title"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#616161"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/avatar_name"
android:layout_toRightOf="@id/avatar_image"
android:layout_marginLeft="4dp"
tools:text="Subhead"
android:id="@+id/avatar_subhead"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#616161"/>
</RelativeLayout>
<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:src="@drawable/placeholder"
android:id="@+id/image_view"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="Keep Moving"
android:paddingLeft="16dp"
android:textAppearance="?android:textAppearanceMedium"
android:textAllCaps="true"
android:textStyle="bold"
android:id="@+id/the_title"
/>f
<TextView
android:id="@+id/the_three_lines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:padding="16dp"
tools:text="@string/description"
android:ellipsize="end"
android:lineSpacingMultiplier="1.2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/heart_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart"
android:padding="8dp"
/>
<ImageView
android:id="@+id/bookmark_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bookmark"
android:padding="8dp"
/>
</LinearLayout>
</LinearLayout>
enter code here
</android.support.v7.widget.CardView>
</LinearLayout>
This is the recycler's Adapter code
@Override
public void onBindViewHolder(ArticleViewHolder holder, int position) {
holder.mAvatarView.setText(mArticles.get(position).getmAvatarInitial());
holder.mAvatarNameView.setText(mArticles.get(position).getmAvatarName());
holder.mAvatarSubView.setText(mArticles.get(position).getmAvatarSub());
mRowIndex++;
if (position == 0) {
holder.horizontalList.setLayoutManager(new LinearLayoutManager(holder.context, LinearLayoutManager.HORIZONTAL, false));
holder.horizontalAdapter = new SectionRVAdapter(holder.context);
holder.horizontalList.setAdapter(holder.horizontalAdapter);
holder.horizontalAdapter.setDataset(holder.sections);
}
String imageUrl = mArticles.get(position).getmImageURL();
Picasso.with(mContext)
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error_img)
.into(holder.mImageView);
holder.mTheTitleView.setText(mArticles.get(position).getmTheTitle());
holder.mTheThreeLinesView.setText(mArticles.get(position).getmTheThreeLines());
}
现在我想在垂直回收者视图上看一个水平回收者视图。但目前水平回收者视图在每5次卡片视图后重复出现。
我正在检查我的onBindViewHolder方法中的position是否等于0,但仍然为什么会多次创建它?
我希望我明白我的问题。
答案 0 :(得分:2)
holder.setIsRecyclable(假);
在public void onBindViewHolder中添加此行(ArticleViewHolder holder,int position){}
我希望这会对你有所帮助