Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> RecyclerView 添加head view頭部 和foot view尾部、設置水平horizontal時左側布局和右側布局

RecyclerView 添加head view頭部 和foot view尾部、設置水平horizontal時左側布局和右側布局

編輯:關於Android編程

折騰一下午,遇到若干問題:如,設置水平HORIZONTAL時,占用多個跨度(類似單元格的合並)

終於都好用了。。。

簡陋圖發6張。
LinearLayoutManager.VERTICAL
這裡寫圖片描述
LinearLayoutManager.HORIZONTAL
這裡寫圖片描述
GridLayoutManager.VERTICAL
這裡寫圖片描述
GridLayoutManager.HORIZONTAL
這裡寫圖片描述
StaggeredGridLayoutManager.VERTICAL
這裡寫圖片描述
StaggeredGridLayoutManager.HORIZONTAL
這裡寫圖片描述

其中一個尾部
這裡寫圖片描述

demo結構
這裡寫圖片描述

MainActivity

package com.louisgeek.louisrecyclerviewtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

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

public class MainActivity extends AppCompatActivity {

    private int list_Header_LayoutID=R.layout.list_header_layout;
    private int list_Footer_LayoutID=R.layout.list_footer_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView idrv = (RecyclerView) findViewById(R.id.id_rv);

        idrv.setLayoutManager(new LinearLayoutManager(this));
       /*idrv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
         idrv.setLayoutManager(new GridLayoutManager(this,3));
        idrv.setLayoutManager(new GridLayoutManager(this,3,GridLayoutManager.HORIZONTAL,false));
        idrv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
       idrv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL));*/

        MyRecyclerViewAdapter myAdapter= new MyRecyclerViewAdapter(getData());

        /**單獨為HORIZONTAL 設置左側的布局*/
        RecyclerView.LayoutManager layoutManager=idrv.getLayoutManager();
        if (layoutManager instanceof StaggeredGridLayoutManager){
            StaggeredGridLayoutManager staggeredGridLayoutManager= (StaggeredGridLayoutManager) layoutManager;
                list_Header_LayoutID=staggeredGridLayoutManager.getOrientation()==StaggeredGridLayoutManager.HORIZONTAL
                        ?R.layout.list_header_layout_4_horizontal :list_Header_LayoutID;
              list_Footer_LayoutID=staggeredGridLayoutManager.getOrientation()==StaggeredGridLayoutManager.HORIZONTAL
                      ?R.layout.list_footer_layout_4_horizontal :list_Footer_LayoutID;
        }else if (layoutManager instanceof GridLayoutManager){
            GridLayoutManager gridLayoutManager= (GridLayoutManager) layoutManager;
            list_Header_LayoutID=gridLayoutManager.getOrientation()==GridLayoutManager.HORIZONTAL
                    ?R.layout.list_header_layout_4_horizontal :list_Header_LayoutID;
            list_Footer_LayoutID=gridLayoutManager.getOrientation()==GridLayoutManager.HORIZONTAL
                    ?R.layout.list_footer_layout_4_horizontal :list_Footer_LayoutID;
        }else if (layoutManager instanceof LinearLayoutManager){
            LinearLayoutManager linearLayoutManager= (LinearLayoutManager) layoutManager;
            list_Header_LayoutID=linearLayoutManager.getOrientation()==LinearLayoutManager.HORIZONTAL
                    ?R.layout.list_header_layout_4_horizontal :list_Header_LayoutID;
            list_Footer_LayoutID=linearLayoutManager.getOrientation()==LinearLayoutManager.HORIZONTAL
                    ?R.layout.list_footer_layout_4_horizontal :list_Footer_LayoutID;
        }
        /**注意  root view 為recycleview*/
        View headView = LayoutInflater.from(this).inflate(list_Header_LayoutID,idrv,false);
        View footView = LayoutInflater.from(this).inflate(list_Footer_LayoutID,idrv,false);
         myAdapter.setHeaderView(headView);
        myAdapter.setFooterView(footView);
        idrv.setHasFixedSize(true);
        idrv.setItemAnimator(new DefaultItemAnimator());
        idrv.setAdapter(myAdapter);
    }


    public List getData(){
        List dataList=new ArrayList<>();
        for (int i = 0; i <10; i++) {
            dataList.add("str"+i);
        }
        return  dataList;
    }


}

activity_main.xml就是一個RelativeLayout加一個RecyclerView



    

垂直的

list_header_layout.xml



    

list_footer_layout.xml



    

list_item.xml



    

水平的。。。

list_header_layout_4_horizontal.xml




    

list_footer_layout_4_horizontal.xml




    

list_item_4_horizontal.xml



    
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved