RecyclerView 新增head view頭部 和foot view尾部、設定水平horizontal時左側佈局和右側佈局
阿新 • • 發佈:2019-01-28
折騰一下午,遇到若干問題:如,設定水平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>