java - Recycler View 的程序流程

标签 java android android-recyclerview

我正在使用回收器 View 实现导航栏,通过引用网站上给出的代码:http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

在 MainActivity.java 中,调用以下几行。

mAdapter = new MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE);

mRecyclerView.setAdapter(mAdapter); 

mLayoutManager = new LinearLayoutManager(this);          

mRecyclerView.setLayoutManager(mLayoutManager);

我想知道 MyAdapter.java 中以什么顺序调用哪些函数? 这是 MyAdapter.java 的代码

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private static final int TYPE_HEADER = 0;  // Declaring Variable to Understand which View is being worked on
    // IF the view under inflation and population is header or Item
    private static final int TYPE_ITEM = 1;

    private String mNavTitles[]; // String Array to store the passed titles Value from MainActivity.java
    private int mIcons[];       // Int Array to store the passed icons resource value from MainActivity.java

    private String name;        //String Resource for header View Name
    private int profile;        //int Resource for header view profile picture
    private String email;       //String Resource for header view email


    MyAdapter(String Titles[], int Icons[], String Name, String Email, int Profile) { // MyAdapter Constructor with titles and icons parameter
        // titles, icons, name, email, profile pic are passed from the main activity as we
        mNavTitles = Titles;                //have seen earlier
        mIcons = Icons;
        name = Name;
        email = Email;
        profile = Profile;                     //here we assign those passed values to the values we declared here
        //in adapter


    }

    // Creating a ViewHolder which extends the RecyclerView View Holder
    // ViewHolder are used to to store the inflated views in order to recycle them

    public static class ViewHolder extends RecyclerView.ViewHolder {
        int Holderid;
        TextView textView;
        ImageView imageView;
        ImageView profile;
        TextView Name;
        TextView email;


        public ViewHolder(View itemView, int ViewType) {                 // Creating ViewHolder Constructor with View and viewType As a parameter
            super(itemView);


            // Here we set the appropriate view in accordance with the the view type as passed when the holder object is created

            if (ViewType == TYPE_ITEM) {
                textView = (TextView) itemView.findViewById(R.id.rowText); // Creating TextView object with the id of textView from item_row.xml
                imageView = (ImageView) itemView.findViewById(R.id.rowIcon);// Creating ImageView object with the id of ImageView from item_row.xml
                Holderid = 1;                                               // setting holder id as 1 as the object being populated are of type item row
            } else {
                Name = (TextView) itemView.findViewById(R.id.name);         // Creating Text View object from header.xml for name
                email = (TextView) itemView.findViewById(R.id.email);       // Creating Text View object from header.xml for email
                profile = (ImageView) itemView.findViewById(R.id.circleView);// Creating Image view object from header.xml for profile pic
                Holderid = 0;                                                // Setting holder id = 0 as the object being populated are of type header view
            }
        }


    }

    //Below first we ovverride the method onCreateViewHolder which is called when the ViewHolder is
    //Created, In this method we inflate the item_row.xml layout if the viewType is Type_ITEM or else we inflate header.xml
    // if the viewType is TYPE_HEADER
    // and pass it to the view holder

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false); //Inflating the layout

            ViewHolder vhItem = new ViewHolder(v, viewType); //Creating ViewHolder and passing the object of type view

            return vhItem; // Returning the created object

            //inflate your layout and pass it to view holder

        } else if (viewType == TYPE_HEADER) {

            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false); //Inflating the layout

            ViewHolder vhHeader = new ViewHolder(v, viewType); //Creating ViewHolder and passing the object of type view

            return vhHeader; //returning the object created


        }
        return null;

    }

    //Next we override a method which is called when the item in a row is needed to be displayed, here the int position
    // Tells us item at which position is being constructed to be displayed and the holder id of the holder object tell us
    // which view type is being created 1 for item row
    @Override
    public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
        if (holder.Holderid == 1) {                              // as the list view is going to be called after the header view so we decrement the
            // position by 1 and pass it to the holder while setting the text and image
            holder.textView.setText(mNavTitles[position - 1]); // Setting the Text with the array of our Titles
            holder.imageView.setImageResource(mIcons[position - 1]);// Settimg the image with array of our icons
        } else {

            holder.profile.setImageResource(profile);           // Similarly we set the resources for header view
            holder.Name.setText(name);
            holder.email.setText(email);
        }
    }

    // This method returns the number of items present in the list
    @Override
    public int getItemCount() {
        return mNavTitles.length + 1; // the number of items in the list will be +1 the titles including the header view.
    }


    // With the following method we check what type of view is being passed
    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position))
            return TYPE_HEADER;

        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

}

通过阅读注释,我了解了特定函数的作用,我想知道它们是按什么顺序调用的? 有人可以帮助我理解这一点吗? 谢谢。

最佳答案

适配器内部方法的调用顺序如下:

  1. RecyclerView 尝试从其回收(或废弃) View 持有者池中获取某个位置的 View 持有者。
  2. 执行此操作时,它会针对要填充的位置调用 getItemViewType
  3. 然后,它会尝试获取之前创建的与该项目 View 类型关联的 View 持有者。
  4. 如果 RecyclerView View 没有此项目类型的可用 View 持有者,它会调用 onCreateViewHolder 以便为此项目类型创建新的 View 持有者。
  5. 一旦获得 View 持有者(无论是通过创建新的,还是获取报废/回收的),它就会从适配器调用 onBindViewHolder 来为 View 持有者设置正确的数据。
  6. 在此期间,getItemCount 将用于确保 RecyclerView 不会尝试填充超出数据源限制的 View 持有者。<

关于java - Recycler View 的程序流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29365551/

相关文章:

android - onChildDraw 使项目闪烁

android - 如何将表示类对象的 RecyclerView 项与此类对象绑定(bind)? Kotlin

java - java中异常处理的输出不一致

java - 带有 PathMatcher 的 DirectoryStream 不返回任何路径

android - Android Studio无法呈现预览

android - 从解析中收到的堆栈通知

android - 通过观察 ViewModel 在 RecyclerView 中搜索 PagedList 的 LiveData

java - Java 左子树中最右边的节点

java - ARCore虚拟物体运动

android - 是否可以使用 Android 的 native 播放器支持隐藏式字幕