[英]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 :我的应用程序的基本结构:
row_data_layout
xml file to bind on recycler_view
.我创建了一个row_data_layout
xml 文件来绑定recycler_view
。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.