Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

android - RecyclerView items duplicate and constantly changing

What's Happening:

The list (RecyclerView) is mixing up the data when I scroll.

I.E when I scroll back up after scrolling down, some of the list items are repeated, not displaying proper content.

package jamesnguyen.newzyv2.UI_update;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import jamesnguyen.newzyv2.R;
import jamesnguyen.newzyv2.RSS_Processcors.RssItem;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.FeedViewHolder> {

    private static List<RssItem> items = null;
    private static Context context;

    public RVAdapter(Context context, List<RssItem> items) {
        this.items = items;
        this.context = context;
    }

    public static List<RssItem> getItems() {
        return items;
    }

    @Override
    public RVAdapter.FeedViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new FeedViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position) {
        FeedViewHolder.getTitle().setText(items.get(position).getTitle());
        FeedViewHolder.getPubDate().setText(items.get(position).getPubDate());
        //FeedViewHolder.getDescription().setText(items.get(position).getDescription());
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

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

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public static class FeedViewHolder extends RecyclerView.ViewHolder {
        private static CardView cv;
        private static TextView title;
        private static TextView pubDate;
        private static TextView description;

        FeedViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            title = (TextView) itemView.findViewById(R.id.title);
            pubDate = (TextView) itemView.findViewById(R.id.pubDate);
            //description = (TextView) itemView.findViewById(R.id.description);
        }

        public static TextView getTitle() {
            return title;
        }

        public static TextView getPubDate() {
            return pubDate;
        }

        public static TextView getDescription() {
            return description;
        }
    }
}

I believe maybe some works have to be done with the recycling process of RecyclerView but nothing i tried seesm to work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

done .

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
   return position;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[2] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

57.0k users

...