Home > Java > javaTutorial > body text

How Android RecyclerView loads two layouts

高洛峰
Release: 2017-02-11 16:45:32
Original
1542 people have browsed it

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


}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!