How Android RecyclerView loads two layouts
When RecyclerView has a pull-down refresh, sometimes the list reflected in the design drawing has a header layout. This can be achieved by loading multiple sets of layouts through the Adapter. Here we take loading two layouts as an example.
Let’s take a look at the Adapter code first:
import android.annotation.SuppressLint; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import com.alvin.R; import java.util.ArrayList; import java.util.List; public class MainListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private List<Data> list; private ArrayList<ItemEntity> jdList; private Context context; public MainListAdapter(List<Data> list,ArrayList<ItemEntity> jdList, Context context) { this.list = list; this.jdList = jdList; this.context = context; } @Override public int getItemViewType(int position) { return list.get(position).getShowType(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType==1){ View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_header_main_list,parent,false); return new HeaderHolder(view); }else{ View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_main,parent,false); return new ViewHolderItem(view); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if(holder instanceof HeaderHolder){ final HeaderHolder headerHolder = (HeaderHolder) holder; //TODO 实现相关逻辑 }else if(holder instanceof ViewHolderItem){ } } @Override public int getItemCount() { return list.size(); } private class HeaderHolder extends RecyclerView.ViewHolder{ HeaderHolder(View itemView) { super(itemView); } } private class ViewHolderItem extends RecyclerView.ViewHolder{ ViewHolderItem(View itemView) { super(itemView); } } }
getItemViewType() returns the current layout type of the list. When the Adapter loads the layout, Alignment is judged, that is, the corresponding judgment logic is implemented in the onBindViewHolder() method.
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more related articles on how Android RecyclerView loads two layouts, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
