一项活动中的三个RecyclerView

时间:2016-08-01 19:10:46

标签: android-studio memory android-recyclerview

我需要有三行图片,一行在另一行之下。可滚动的每个RecyclerView独立地位于每一行中。我想出了将RecyclerView s,一个放在另一个下面,以及三个适配器RecyclerView的想法,它看起来非常好。我唯一的问题是它似乎消耗了大量内存并且滚动速度很慢。我认为代码没问题,RecyclerView也正常工作,但也许我错过了什么或做错了什么。这是我设置适配器的活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  MobileAds.initialize(getApplicationContext(), "ca-app-pub-XXXXX");
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.articles);
    RecyclerView recyclerView2 = (RecyclerView) findViewById(R.id.articles2);
    RecyclerView recyclerView3 = (RecyclerView) findViewById(R.id.articles3);

    recyclerView.setHasFixedSize(true);
    recyclerView2.setHasFixedSize(true);
    recyclerView3.setHasFixedSize(true);

    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    recyclerView2.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
   recyclerView3.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

    ArrayList<Article> articles = new ArrayList<>();
    for (int i = 0; i < 30; ++i)
        articles.add(new Article());

    ArrayList<Article2> articles2 = new ArrayList<>();
    for (int i = 0; i < 30; ++i)
        articles2.add(new Article2());

    ArrayList<Article3> articles3 = new ArrayList<>();
    for (int i = 0; i < 30; ++i)
        articles3.add(new Article3());

    recyclerView.setAdapter(new MyAdapter(articles, recyclerView));
    recyclerView2.setAdapter(new MyAdapter2(articles2, recyclerView2));
    recyclerView3.setAdapter(new MyAdapter3(articles3, recyclerView3));
}

有关如何解决内存消耗问题的任何建议?

1 个答案:

答案 0 :(得分:1)

有一些方法可以增加应用程序的内存,使其能够在堆中存储更多内存。由于图像以及需要将三个不同的适配器设置为包含图像的三个不同RecyclerView的事实,您的应用程序可能运行缓慢。通常,设置图​​像可能会占用大量资源,考虑到它们有三行。您可以尝试使用较少的图像,但这可能会破坏应用程序的整个目的。我建议您查看managing your application's memory并查看可以优化它的位置。