简体   繁体   English

RecyclerView、RecyclerView.Adapter和RecyclerView.LayoutManager在哪里声明?

[英]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.所以我有两个recycleView 的例子,一个在onCreate 方法中声明上述内容,另一个在MainActivity 中声明。 Is there any difference?有什么区别吗? Is one right or wrong?一个是对还是错? Guess it is not an android concept but a java one.猜猜这不是 android 概念,而是 java 概念。 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.从我从问题和代码中可以理解的情况来看,我看到示例中的两个 recyclerviews 都在每个活动的 onCreate 方法中被调用,并且布局管理器也在 onCreate 方法中被分配。 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.我实际上可以告诉你的是,这些示例都没有遵循 SOLID 原则。

Try reading about the SOLID princples here: Medium about solid principles尝试在此处阅读有关 SOLID 原则的信息: Medium about solid principle

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.此外,第二个示例似乎没有向布局管理器添加方向,而第一个示例使用 Item 装饰器来执行此操作。

I hope i got your question right!我希望你的问题是正确的!

They are just the different technique of variable declaration, the difference being the scope of those variable.它们只是变量声明的不同技术,不同之处在于这些变量的scope
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.在第一个示例中,诸如itemsRecyclerView对象)、 adapterColorAdapter对象)等变量是onCreate()方法的本地变量,不能在该特定方法之外使用。 These variables are created at the beginning of the onCreate() method and destroyed at the end of the same method.这些变量在onCreate()方法的开头创建并在同一方法的结尾销毁

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".在第二个示例中,它们使用全局私有 scope声明,意思是“可以在此 class 的任何地方使用,但不能从类外使用”。 They are created at the constructor call of your MyActivity class and are destroyed at the onDestroy() call of your MyActivity .它们是在MyActivity class的构造函数调用中创建的,并在MyActivityonDestroy()调用中被销毁


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).这将为资源有限的设备(例如 android 电话)生成内存友好的代码(尽管 XD 目前无效)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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