RecyclerView加载不同的Layout

有时候一个界面的RecyclerView可能要显示好几种的不同的布局,学习了,记录一下
下面是同一个RecyclerView加载两种不同布局的写法

主要加载的xml文件

activity_main.xml




    
    


RecyclerView中加载的第一种布局xml:top.xml
只显示一个TextView



    
    


RecyclerView中加载的第二种xml:Container.xml
显示一个ImageView和一个TextView




    
    

创建一个接口,并让两个实体类实现它

接口Type.java

package com.example.mulrecyclerview;

public interface Type {
     
    int getItemType();
}

第一个布局的实体类Condition.java

package com.example.mulrecyclerview;

public class Condition implements Type{
     
    private String title;

    public Condition(String title) {
     
        this.title = title;
    }

    public String getTitle() {
     
        return title;
    }

    @Override
    public int getItemType() {
     
        return R.layout.top;
    }
}

第二个布局的实体类Category.java

package com.example.mulrecyclerview;

public class Category implements Type{
     
    private int iconID;
    private String name;

    public Category(int iconID, String name) {
     
        this.iconID = iconID;
        this.name = name;
    }

    public int getIconID() {
     
        return iconID;
    }

    public String getName() {
     
        return name;
    }

    @Override
    public int getItemType() {
     
        return R.layout.container;
    }
}

创建适配器Adapter

package com.example.mulrecyclerview;

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.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
     
    private Context mContext;
    private List<Type> mDataList;
    public static final int TOP_TYPE=R.layout.top;
    public static final int CONTAINER_TYPE=R.layout.container;
    public MyAdapter(Context mContext,List<Type> mDataList){
     
        this.mContext = mContext;
        this.mDataList = mDataList;
    }

    @Override
    public int getItemViewType(int position) {
     
        return mDataList.get(position).getItemType();
    }

    public static class TopViewHolder extends RecyclerView.ViewHolder{
     
        private TextView top_title;
        public TopViewHolder(View view){
     
            super(view);
            top_title = view.findViewById(R.id.top_title);
        }
    }
    public static class ContainerViewHolder extends RecyclerView.ViewHolder{
     
        private ImageView container_image;
        private TextView contaniner_title;
        public ContainerViewHolder(View view){
     
            super(view);
            container_image = view.findViewById(R.id.container_image);
            contaniner_title = view.findViewById(R.id.container_title);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
     
        if(viewType==TOP_TYPE){
     
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.top,viewGroup,false);
            return new TopViewHolder(view);
        }else if(viewType==CONTAINER_TYPE){
     
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.container,viewGroup,false);
            return new ContainerViewHolder(view);
        }
        return  null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
     
        int type = getItemViewType(position);
        if(type==TOP_TYPE){
     
            Condition condition =(Condition) mDataList.get(position);
            TopViewHolder topViewHolder = (TopViewHolder)holder;
            topViewHolder.top_title.setText(condition.getTitle());
        }else if(type==CONTAINER_TYPE){
     
            Category category = (Category)mDataList.get(position);
            ContainerViewHolder containerViewHolder = (ContainerViewHolder)holder;
            containerViewHolder.container_image.setImageResource(category.getIconID());
            containerViewHolder.contaniner_title.setText(category.getName());
        }
    }

    @Override
    public int getItemCount() {
     
        return mDataList.size();
    }
}

主活动中初始化数据和添加布局

package com.example.mulrecyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
     
    List<Type> mDataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        MyAdapter myAdapter = new MyAdapter(this,mDataList);
        recyclerView.setAdapter(myAdapter);
    }
    public void init(){
     
        for(int i=0;i<50;i++){
     
            if(i<25){
     
                mDataList.add(new Condition("i am your yo"));
            }else{
     
                mDataList.add(new Category(R.drawable.ic_launcher_background,"yo yo yo"));
            }
        }
    }
}

你可能感兴趣的:(Android开发,Android,RecyclerView,多种layout)