編輯:關於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 ListgetData(){ 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
最近在研究AMS代碼遇到一個問題,在函數startActivityUncheckedLocked中 Slog.d("DDY", "!!
第二十二章、享元模式 享元模式是結構型設計模式之一,是對對象池的一種實現。就像它的名字一樣,共享對象,避免重復的創建。我們常用的String 就是使用了共享模式,所以St
最近使用到Recylerview完成拖動排序,側滑刪除,在此記錄一下。需要使用到:ItemTouchHelper.Callback這個類。效果圖:在有Recy
一、概述本文之前,先提一下關於上篇博文的100多萬訪問量請無視,博文被刷,我也很郁悶,本來想把那個文章放到草稿箱,結果放不進去,還把日期弄更新了,實屬無奈。