简体   繁体   中英

Where to declare RecyclerView, RecyclerView.Adapter and RecyclerView.LayoutManager?

So I have two examples of recycleView, tough in one they declare the aforementioned in the onCreate method, and in the other in the MainActivity. Is there any difference? Is one right or wrong? Guess it is not an android concept but a java one. Here is the code

package com.commonsware.jetpack.samplerj.recyclerview;

import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
  private final Random random = new Random();

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

    RecyclerView items = findViewById(R.id.items);
    ColorAdapter adapter = new ColorAdapter(getLayoutInflater());

    adapter.submitList(buildItems());
    items.setLayoutManager(new LinearLayoutManager(this));
    items.addItemDecoration(
      new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    items.setAdapter(adapter);
  }

  private List<Integer> buildItems() {
    ArrayList<Integer> result = new ArrayList<>(25);

    for (int i = 0; i < 25; i++) {
      result.add(random.nextInt());
    }

    return result;
  }
}

and the other example

public class MyActivity extends Activity {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);

        // use a linear layout manager
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        recyclerView.setAdapter(mAdapter);
    }
    // ...
}

From what I can understand from the question and the code, I see that both of the recyclerviews in the example are being called in the onCreate Methods of each one of the activities and the layout managers are also being assigned in the onCreate Method. So I don't think theres a difference there. Something that I can actually tell you is that neither of those examples are following the SOLID principles.

Try reading about the SOLID princples here: Medium about solid principles

Also, the second example doesn't seem to add a direction to the layout manager whereas the first is using an Item decorator to do so among other things.

I hope i got your question right!

They are just the different technique of variable declaration, the difference being the scope of those variable.
In the first example, variables such as items (a RecyclerView object), adapter (a ColorAdapter object) are local to onCreate() method and cannot be used outside that particular method. These variables are created at the beginning of the onCreate() method and destroyed at the end of the same method.

In the second example, they are declared with global private scope meaning "can be used anywhere in this class but not from outside the class". They are created at the constructor call of your MyActivity class and are destroyed at the onDestroy() call of your MyActivity .


Recommendation:
Always try to use more local variables and less global variables as much as possible. This would result in a memory-friendly code for the devices with limited resources such as an android phone(not valid these days though XD).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM