Recyclerview读取片段重新加载的项目

时间:2016-05-03 01:01:36

标签: android android-layout android-recyclerview

我有一个在网格布局中使用recyclerview的应用程序。每次重新加载片段时,recyclerview都会将项目读入视图。这很难解释所以我在帖子中包含了图片。

适配器

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.PersonViewHolder> {
    private Context context;
    private ArrayList<NewsItem> items;

    public NewsAdapter(Context context, ArrayList<NewsItem> newsItems) {
        this.context = context;
        this.items = newsItems;
    }

    public class PersonViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.newsItemCard);
            personName = (TextView) itemView.findViewById(R.id.newsItemTitle);
            personPhoto = (ImageView) itemView.findViewById(R.id.newsItemImage);
        }
    }

    @Override
    public NewsAdapter.PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder holder, int position) {
        final NewsItem newsItem = items.get(position);
        holder.personName.setText(items.get(position).getTitle());
        holder.personPhoto.setImageResource(items.get(position).getImage());
        holder.cv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle args = new Bundle();
                args.putString("link",newsItem.getLink());
                RSSFragment rssFragment = new RSSFragment();
                rssFragment.setArguments(args);
                FragmentManager fragmentManager = ((FragmentActivity)context).getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_main,rssFragment).addToBackStack(null).commit();
            }
        });
    }

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

片段

package com.intellidev.faucetpro2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;


public class NewsFragment extends Fragment {
    private View v;
    private RecyclerView gridView;
    private ArrayList<NewsItem> items = new ArrayList<NewsItem>();
public NewsFragment() {
    // Required empty public constructor
}

private void initializeItems() {
    items.add(new NewsItem(R.mipmap.ic_launcher, "Let's Talk Bitcoin", "https://letstalkbitcoin.com/rss/feed/blog?limit=50&soundcloud-id=false&audio-url=false&sites=1&categories=7,8,12,13,14,15,16,17,18,19,20,21,27,30,35,34,38,39,58,59,53,60,70,47,77,42,48,50,41,46,26,11,25,28,36,37,44,45,54,55,56,61,62,68,69,57,22,32,24,29"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Crypto Coins News", "https://www.cryptocoinsnews.com/news/feed/"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin Magazine", "https://bitcoinmagazine.com/feed/"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoinist", "http://bitcoinist.net/feed/"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin.org", "https://bitcoin.org/en/rss/alerts.rss"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "The Bitcoin News", "http://thebitcoinnews.com/feed/"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin Core", "https://bitcoincore.org/en/rss.xml"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "News BTC", "http://www.newsbtc.com/feed/"));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_news, container, false);
    gridView = (RecyclerView) v.findViewById(R.id.newsRecyclerView);
    gridView.setHasFixedSize(true);
    GridLayoutManager glm = new GridLayoutManager(getContext(), 3);
    gridView.setLayoutManager(glm);
    NewsAdapter newsAdapter = new NewsAdapter(getContext(), items);
    gridView.setAdapter(newsAdapter);
    initializeItems();
    return v;
}
}

我第一次加载片段

enter image description here

第二次用户嘲笑片段

enter image description here

2 个答案:

答案 0 :(得分:1)

    private ArrayList<NewsItem> items;
    ...
    private void initializeItems() {
        items = new ArrayList<NewsItem>()
        items.add(new NewsItem(R.mipmap.ic_launcher, "Let's Talk Bitcoin", "https://letstalkbitcoin.com/rss/feed/blog?limit=50&soundcloud-id=false&audio-url=false&sites=1&categories=7,8,12,13,14,15,16,17,18,19,20,21,27,30,35,34,38,39,58,59,53,60,70,47,77,42,48,50,41,46,26,11,25,28,36,37,44,45,54,55,56,61,62,68,69,57,22,32,24,29"));
        items.add(new NewsItem(R.mipmap.ic_launcher, "Crypto Coins News", "https://www.cryptocoinsnews.com/news/feed/"));
        items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin Magazine", "https://bitcoinmagazine.com/feed/"));
        items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoinist", "http://bitcoinist.net/feed/"));
        items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin.org", "https://bitcoin.org/en/rss/alerts.rss"));
        items.add(new NewsItem(R.mipmap.ic_launcher, "The Bitcoin News", "http://thebitcoinnews.com/feed/"));
        items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin Core", "https://bitcoincore.org/en/rss.xml"));
        items.add(new NewsItem(R.mipmap.ic_launcher, "News BTC", "http://www.newsbtc.com/feed/"));
    }

答案 1 :(得分:0)

为了安全起见,不要全局声明列表,而是在createView中声明它并将其传递给初始化函数。这应该工作。

package com.intellidev.faucetpro2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;


public class NewsFragment extends Fragment {
    private View v;
    private RecyclerView gridView;
public NewsFragment() {
    // Required empty public constructor
}

private void initializeItems(List<NewsItems> items) {
    items.add(new NewsItem(R.mipmap.ic_launcher, "Let's Talk Bitcoin", "https://letstalkbitcoin.com/rss/feed/blog?limit=50&soundcloud-id=false&audio-url=false&sites=1&categories=7,8,12,13,14,15,16,17,18,19,20,21,27,30,35,34,38,39,58,59,53,60,70,47,77,42,48,50,41,46,26,11,25,28,36,37,44,45,54,55,56,61,62,68,69,57,22,32,24,29"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Crypto Coins News", "https://www.cryptocoinsnews.com/news/feed/"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin Magazine", "https://bitcoinmagazine.com/feed/"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoinist", "http://bitcoinist.net/feed/"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin.org", "https://bitcoin.org/en/rss/alerts.rss"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "The Bitcoin News", "http://thebitcoinnews.com/feed/"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "Bitcoin Core", "https://bitcoincore.org/en/rss.xml"));
    items.add(new NewsItem(R.mipmap.ic_launcher, "News BTC", "http://www.newsbtc.com/feed/"));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_news, container, false);
    gridView = (RecyclerView) v.findViewById(R.id.newsRecyclerView);
    gridView.setHasFixedSize(true);
    GridLayoutManager glm = new GridLayoutManager(getContext(), 3);
    gridView.setLayoutManager(glm);
    private ArrayList<NewsItem> items = new ArrayList<NewsItem>();
    initializeItems(items);
    NewsAdapter newsAdapter = new NewsAdapter(getContext(), items);
    gridView.setAdapter(newsAdapter);
    return v;
}
}