2017-04-23 167 views
0
 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方法中的位置是否等於0,但仍然爲什麼它被創建多次?

我希望我明確提出了我的問題。

+0

在'onBindViewHolder'加上'else'聲明,recyclerView如果你使用'如果'你必須把'else'語句。 –

+0

我不認爲我需要在這裏的其他聲明。不要認爲這是造成問題的原因。如果陳述應該只有一次,即當位置爲1時,但在每創建5次cardview後它才變爲真。 –

+0

http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works –

回答

2

holder.setIsRecyclable(false);

添加在公共無效onBindViewHolder這條線(ArticleViewHolder架,INT位置){}

我希望這將有助於你