1. 程式人生 > >Android之RecyclerView多佈局

Android之RecyclerView多佈局

做一個專案的主頁面的時候,想要它呈現出來的效果,不單一,更豐富那就要使用多佈局來展現出來,那麼就要思考一個問題。他呈現的是多個佈局,怎麼才能展現出來不同的佈局?邏輯很簡單,通過設定幾個flag,來表示這些佈局當前顯示的是哪個佈局,接下來,和程式碼結合瞭解一下:

第一部分:有幾個佈局就寫幾個flag

public class MyRecycler extends RecyclerView.Adapter{

    private Context context;
    private HomeBean.ResultBean result;
    private LayoutInflater layoutInflater;

    public static final int banner = 0;//廣告
    public static final int chanel= 1;//頻道
    public static final int act=2;//活動
    public static final int seckill=3;//秒殺
    public static final int recommend=4;//推薦
    public static final int hot=5;//熱賣

    private int currenType= banner;//當前顯示

    public MyRecycler(Context context, HomeBean.ResultBean result) {
        this.context = context;
        this.result = result;
        layoutInflater=LayoutInflater.from(context);
    }

第二部分:繼承RecyclerViewAdapter重寫的方法:

1.onCreateViewHolder():判斷當前是第幾個佈局,返回當前的佈局(你的佈局的高度一定要是自定高度)

  @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==banner){
            return new bannerHolder(context,layoutInflater.inflate(R.layout.banner_item,null));
        }else if (viewType==chanel){
            return new MyViewHolder(context,layoutInflater.inflate(R.layout.channel_item,null));
        }else if (viewType==act){
            return new ActHolder(context,layoutInflater.inflate(R.layout.act_item,null));
        }else if (viewType==seckill){
            return new SeckillHolder(context,layoutInflater.inflate(R.layout.seckill_item,null));
        }else if (viewType==recommend){
            return new RecommendHolder(context,layoutInflater.inflate(R.layout.recommend_item,null));
        }else if (viewType==hot){
            return new HotHolder(context,layoutInflater.inflate(R.layout.hot_item,null));
        }
        return null;
    }

2. onBindViewHolder(),根據對應的佈局,傳對應的資料:

  @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position)==banner){
            bannerHolder bannerHolders= (bannerHolder) holder;
            List<HomeBean.ResultBean.BannerInfoBean> banner_info = result.getBanner_info();
            bannerHolders.setData(banner_info);
        }else if (getItemViewType(position)==chanel){
            MyViewHolder myViewHolder= (MyViewHolder) holder;
            List<HomeBean.ResultBean.ChannelInfoBean> channel_info= result.getChannel_info();
            myViewHolder.setData(channel_info);
        }else if (getItemViewType(position)==act){
            ActHolder actHolder= (ActHolder) holder;
            List<HomeBean.ResultBean.ActInfoBean> act_info = result.getAct_info();
            actHolder.setData(act_info);
        }else if (getItemViewType(position)==seckill){
            SeckillHolder seckillHolder= (SeckillHolder) holder;
            HomeBean.ResultBean.SeckillInfoBean seckill_info = result.getSeckill_info();
            seckillHolder.setData(seckill_info);
        }else if (getItemViewType(position)==recommend){
            RecommendHolder recommendHolder= (RecommendHolder) holder;
            List<HomeBean.ResultBean.RecommendInfoBean> recommend_info = result.getRecommend_info();
            recommendHolder.setData(recommend_info);
        }else if (getItemViewType(position)==hot){
            HotHolder hotHolder= (HotHolder) holder;
            List<HomeBean.ResultBean.HotInfoBean> hot_info = result.getHot_info();
            hotHolder.setData(hot_info);
        }
    }

3. getItemViewType()item型別

/**
     * item型別:
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case banner:
                currenType = banner;
                break;
            case chanel:
                currenType = chanel;
                break;
            case act:
                currenType = act;
                break;
            case seckill:
                currenType = seckill;
                break;
            case recommend:
                currenType = recommend;
                break;
            case hot:
                currenType = hot;
                break;
        }
        return currenType;
    }

4.getitemcount()這裡的個數,一定要滿足你要實現的佈局個數,不匹配就會報空指標的錯誤!

 /**
     * item的總個數:
     * @return
     */
    @Override
    public int getItemCount() {
        return 6;
    }

第二部分:進行適配:在這裡使用網格佈局,顯示成垂直一列:

  GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),1);
  rv.setLayoutManager(gridLayoutManager);

  MyRecycler recycler=new MyRecycler(getActivity(),result);
  rv.setAdapter(recycler);

第三部分:進行細節功能的操作:

1.設定一個小控制元件,當超過三個佈局自動隱藏:

 //設定跨度監聽事件:

gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
   @Override
   public int getSpanSize(int position) {
       if (position<=3){
           //隱藏:
           top.setVisibility(View.GONE);
       }else {
           //顯示:
           top.setVisibility(View.VISIBLE);
        }
       //只能返回1
       return 1;
       }
    });

2.以及置頂功能:

  //置頂監聽;
                image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rv.scrollToPosition(0);
                    }
                });

最終效果如下: