簡體   English   中英

如何在Recycler View中停止重復項目?

[英]How to stop repetition of items in Recycler View?

我創建了一個帶有片段的三個選項卡式應用程序,每當我在片段之間切換時,這些項目就會不斷重復

我試圖將hassatbleimageId設置為true,但沒有任何效果

適配器代碼

public class PlaceAdapter extends 
RecyclerView.Adapter<PlaceAdapter.PlaceViewHolder> {


OnPlaceListener mListener;
Context mContext;
List<Place> mData;

public static class PlaceViewHolder extends RecyclerView.ViewHolder{

    public TextView mTextView;
    public ImageView mImageView;

    public PlaceViewHolder(@NonNull View itemView) {
        super ( itemView );
        mTextView = itemView.findViewById ( R.id.ImageViewText );
        mImageView = itemView.findViewById ( R.id.ImageView );

    }
}


public PlaceAdapter(Context mContext, List<Place> mData, OnPlaceListener 
listener){

    this.mContext = mContext;
    this.mData = mData;
    this.mListener = listener;
    setHasStableIds ( true );

}

@NonNull
@Override
public PlaceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
viewType) {
     View v = LayoutInflater.from ( mContext ).inflate ( 
R.layout.list_item, parent, false );
    PlaceViewHolder pvh = new PlaceViewHolder ( v );

    return pvh;
}

@Override
public void onBindViewHolder(@NonNull PlaceViewHolder holder, final int 
position) {

    holder.itemView.setOnClickListener(new View.OnClickListener () {
        @Override
        public void onClick(final View v) {
            mListener.onPlaceClick(position);
        }
    });
    holder.mTextView.setText ( mData.get ( position ).getPlaceName () );
    holder.mImageView.setImageResource ( mData.get ( position 
).getImageResourceId () );

}

@Override
public int getItemCount() {
    return mData.size ();
}


public interface OnPlaceListener{

    void onPlaceClick(int position);
}

}

這是碎片之一

public class MonumentsFragment extends Fragment implements 
PlaceAdapter.OnPlaceListener {

private RecyclerView mRecyclerView;
final List<Place> places = new ArrayList<> ();

public MonumentsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     View rootView = inflater.inflate( R.layout.places_list, container, 
false);

    Place place1 = new Place(R.string.monument1, 
R.drawable.windsor_castle, R.string.monument1S, R.string.monument1I);
    places.add(place1);

    Place place2 = new Place(R.string.monument2, 
R.drawable.trafalgar_square,R.string.monument2S, R.string.monument2I);
    places.add(place2);

    Place place3 = new Place(R.string.monument3, 
R.drawable.buckingham_palace,R.string.monument3S, R.string.monument3I);
    places.add(place3);

    Place place4 = new Place(R.string.monument4, 
R.drawable.bank_of_england,R.string.monument4S, R.string.monument4I);
    places.add(place4);

    Place place5 = new Place(R.string.monument5, 
R.drawable.kensington_palace,R.string.monument5S, R.string.monument5I);
    places.add(place5);

    Place place6 = new Place(R.string.monument6, 
R.drawable.london_wall,R.string.monument6S, R.string.monument6I);
    places.add(place6);


    mRecyclerView = (RecyclerView) rootView.findViewById ( 
R.id.recycler_view );
    PlaceAdapter placeAdapter = new PlaceAdapter ( getContext (), 
places,this );

    mRecyclerView.setLayoutManager ( new LinearLayoutManager ( getActivity 
() ) );
    mRecyclerView.setAdapter ( placeAdapter );

    return rootView;


}

@Override
public void onPlaceClick(int position) {
    Intent intent = new Intent( MonumentsFragment.this.getActivity () 
,SelectedPlaceActivity.class );
    intent.putExtra("Place Item", places.get(position));
    startActivity ( intent );
}





}

自訂物件

public class Place implements Parcelable {


/** String resource ID for the name of the place */
private int mPlaceID;

private int mImageResourceId;

private int mNearestStation;

private int mSiteInfo;

public Place(int placeID, int imageResourceId, int NearestStation, int 
SiteInfo) {
    mPlaceID = placeID;
    mImageResourceId = imageResourceId;
    mNearestStation = NearestStation;
    mSiteInfo = SiteInfo;
}

protected Place(Parcel in) {
    mImageResourceId = in.readInt();
    mPlaceID = in.readInt ();
    mNearestStation = in.readInt ();
    mSiteInfo = in.readInt ();

}

public static final Creator<Place> CREATOR = new Creator<Place>() {
    @Override
    public Place createFromParcel(Parcel in) {
        return new Place (in);
    }

    @Override
    public Place[] newArray(int size) {
        return new Place[size];
    }
};

/**
 * Get the string resource ID for the name of the place.
 */
public int getPlaceName() {
    return mPlaceID;
}

/**
 * Return the image resource ID of the place.
 */
public int getImageResourceId() {
    return mImageResourceId;
}

public int getNearestStation() {
    return mNearestStation;
}

public int getSiteInfo() {
    return mSiteInfo;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(mImageResourceId);
    parcel.writeInt(mPlaceID);
    parcel.writeInt ( mNearestStation );
    parcel.writeInt ( mSiteInfo );
}

}

每當我切換然后再次返回該片段時,這些項目就會重復。

放置places.clear(); 在place1之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM