How RecyclerView concept works on android?

会有一股神秘感。 提交于 2019-12-03 09:48:09

问题


I have created an basic app using RecyclerView and CardView from get tutorials from websites.

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 create an row_data_layout xml file to bind on recycler_view.
  2. Created an Data class file(Here i defined my variable that i used in App)
  3. Created an Adapter file (here i want to clear how it goes work step by step first which class call and why ?).
  4. Bind Data to RecyclerView on MainActivity file

row_data_layout.xml file :

<?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 :

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;
    }
}

回答1:


Once you have a basic understanding of how a RecyclerView.Adapter works, it would make sense to take a deeper dive into the documentation.

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. 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.

The onCreateViewHolder() function is where a new, empty view (wrapped by a RecyclerView.ViewHolder) is created and added to the pool.

The onBindViewHolder() function gets a view from the empty pool and populates this view using the data you supplied to the adapter.\

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.

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.



来源:https://stackoverflow.com/questions/36042926/how-recyclerview-concept-works-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!