简体   繁体   English

RecyclerView 概念如何在 android 上工作?

[英]How RecyclerView concept works on android?

I have created a basic app using RecyclerView and CardView from get tutorials from websites.我使用 RecyclerView 和 CardView 从网站获取教程创建了一个基本应用程序。

App is working fine and I have some confusion.(I am showing my whole code here)应用程序运行良好,但我有些困惑。(我在这里展示了我的整个代码)

confusion is that how code works step by step.令人困惑的是代码如何逐步工作。 So please clear my concept on it.所以请明确我的概念。

Basic Structure of my App :我的应用程序的基本结构:

  1. I have created a row_data_layout xml file to bind on recycler_view .我创建了一个row_data_layout xml 文件来绑定recycler_view
  2. Created an Data class file (Here I have defined my variable that I used in App).创建了一个数据类文件(这里我定义了我在 App 中使用的变量)。
  3. Created an Adapter file (here I want to clear how it works step by step first which class gets called and why?).创建了一个适配器文件(在这里我想一步一步地弄清楚它是如何工作的,哪个类被调用以及为什么?)。
  4. Bind Data to RecyclerView on MainActivity file.将数据绑定到MainActivity文件上的RecyclerView

row_data_layout.xml file: row_data_layout.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/CardView"
    android:paddingBottom="16dp"
    android:layout_marginBottom="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</android.support.v7.widget.CardView>

Data Class File:数据类文件:

public class Data {
    public String Name;

    Data(String Name)
    {
        this.Name=Name;
    }
}

Data_Adapter Class file: Data_Adapter 类文件:

public class Data_Adapter extends RecyclerView.Adapter<Data_Adapter.View_holder> {
    List<Data> list = Collections.emptyList();
    Context context;

    public Data_Adapter(List<Data> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public Data_Adapter.View_holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_data_layout,parent,false);
        View_holder holder=new View_holder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(Data_Adapter.View_holder holder, int position) {
            holder.name.setText(list.get(position).Name);
    }

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

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

    public class View_holder extends RecyclerView.ViewHolder{
        CardView cv;
        TextView name;

        public View_holder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.CardView);
            name = (TextView) itemView.findViewById(R.id.txt_name);
        }
    }
}

MainActivity File:主活动文件:

public class MainActivity extends AppCompatActivity {

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

        List<Data> data = fill_data();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        Data_Adapter adapter = new Data_Adapter(data,getApplicationContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    public List<Data> fill_data()
    {
        List<Data> data = new ArrayList<>();
        data.add(new Data("Bred Pit"));
        data.add(new Data("Leonardo"));

        return data;
    }
}

Once you have a basic understanding of how a RecyclerView.Adapter works, it would make sense to take a deeper dive into the documentation .一旦您对 RecyclerView.Adapter 的工作原理有了基本的了解,就可以更深入地研究文档

What the adapter does is keep a pool of inflated views (this can be as many different types of ViewHolder as you would like) that it populates with the data you supply.适配器所做的是保持一个膨胀的视图池(这可以是您想要的任意多种不同类型的ViewHolder ),它会填充您提供的数据。 When the adapter does not have an empty view in the pool it creates a new one.当适配器在池中没有空视图时,它会创建一个新视图。

When a view is attached to the RecyclerView, it is removed from the pool, and when it is detached (scrolls beyond view, to some distance), it is added back to the pool of empty views--this is why it is important to reset everything when you populate your ViewHolders.当一个视图附加到 RecyclerView 时,它会从池中移除,当它分离时(滚动超出视图,到一定距离),它被添加回空视图池中——这就是为什么重要的是填充 ViewHolders 时重置所有内容。

The onCreateViewHolder() function is where a new, empty view (wrapped by a RecyclerView.ViewHolder) is created and added to the pool. onCreateViewHolder()函数用于创建一个新的空视图(由 RecyclerView.ViewHolder 包装)并将其添加到池中。

The onBindViewHolder() function gets a view from the empty pool and populates this view using the data you supplied to the adapter.\\ onBindViewHolder()函数从空池中获取一个视图,并使用您提供给适配器的数据填充此视图。\\

You can use the onViewRecycled() method to perform specific actions like setting an ImageView's bitmap to null (on detach) in order to reduce memory usage.您可以使用onViewRecycled()方法执行特定操作,例如将 ImageView 的位图设置为 null(分离时)以减少内存使用。

I don't normally override onAttachedToRecyclerView() , but if you need to do something specific when your adapter is associated with the RecyclerView, you would do it here.我通常不会覆盖onAttachedToRecyclerView() ,但是如果您需要在适配器与 RecyclerView 关联时执行某些特定操作,则可以在此处执行。

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

相关问题 Android Studio 自动完成功能如何在 RecyclerView 的 `app:layoutManager` 中工作? - How does the Android Studio autocomplete works in RecyclerView's `app:layoutManager`? 如何在Android中实现NSUserDefaults概念 - How to achieve NSUserDefaults concept in Android 如何在recyclerview gridlayout中实现水平和垂直链类型概念? - How to implement horizontal & vertical chain type concept in recyclerview gridlayout? Android Volley Recyclerview,它有时可以工作,但几乎根本不工作 - Android Volley Recyclerview, it works sometimes, but mostly not at all 带有 RecyclerView 的 Android ViewPager 在 BottomSheet 中无法正常工作 - Android ViewPager with RecyclerView works incorrectly inside BottomSheet 如何在Android中使用RecyclerView? - How to work with RecyclerView in Android? 如何在Android中实现事件和委托概念? - How to implement event and delegate concept in Android? 如何在Android应用程序中实现线程概念? - How to implement thread concept in Android application? 如何在 android 应用中应用母版页概念? - how to apply master page concept in android application? 如何添加圆角作为 Recyclerview 的背景,当滚动项目在 Android 中工作时 - How to add rounded corner as a background of Recyclerview that When scrolling items works in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM