Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

RecyclerView 添加head view头部 和foot view尾部、设置水平horizontal时左侧布局和右侧布局

$
0
0

折腾一下午,遇到若干问题:如,设置水平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<String> getData(){
        List<String> dataList=new ArrayList<>();
        for (int i = 0; i <10; i++) {
            dataList.add("str"+i);
        }
        return  dataList;
    }


}

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.louisgeek.louisrecyclerviewtest.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/id_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

垂直的

list_header_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:padding="10dp"
              android:layout_margin="5dp"
              android:background="@color/colorAccent"
              android:gravity="center_horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="head str"
        android:padding="10dp"
        />

</LinearLayout>

list_footer_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:padding="10dp"
              android:layout_margin="5dp"
              android:background="@color/colorPrimary"
              android:gravity="center_horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="foot view"
        android:padding="10dp"
        />

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:padding="10dp"
              android:layout_margin="5dp"
              android:background="@android:color/darker_gray"
              android:gravity="center_horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <TextView
        android:id="@+id/id_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="normal str"
        android:padding="10dp"
        />
</LinearLayout>

水平的。。。

list_header_layout_4_horizontal.xml

<?xml version="1.0" encoding="utf-8"?>
<!--android:layout_width="wrap_content"-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:padding="10dp"
              android:layout_margin="5dp"
              android:background="@color/colorAccent"
              android:gravity="center_vertical"
              android:layout_width="wrap_content"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left str"
        android:padding="10dp"
        />

</LinearLayout>

list_footer_layout_4_horizontal.xml

<?xml version="1.0" encoding="utf-8"?>
<!--android:layout_width="wrap_content"-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:padding="10dp"
              android:layout_margin="5dp"
              android:background="@color/colorPrimary"
              android:gravity="center_vertical"
              android:layout_width="wrap_content"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right view"
        android:padding="10dp"
        />

</LinearLayout>

list_item_4_horizontal.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:padding="10dp"
              android:layout_margin="5dp"
              android:background="@android:color/darker_gray"
              android:gravity="center_horizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <TextView
        android:id="@+id/id_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="normal str"
        android:padding="10dp"
        />
</LinearLayout>
作者:RichieZhu 发表于2016/9/11 21:27:52 原文链接
阅读:76 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>