请选择 进入手机版 | 继续访问电脑版

技术控

    今日:0| 主题:61300
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] EasyHeaderFooterAdapter

[复制链接]
寂寞好了 发表于 2016-10-1 16:48:01
126 0
EasyHeaderFooterAdapter

   Add a header and/or footer to your RecyclerView - the easy way.
   

EasyHeaderFooterAdapter

EasyHeaderFooterAdapter-1-技术控-download,existing,version,usually,without

   You can download the latest sample app here .
  Features

  
       
  • Completely hassle-free. You don't have to change your existing implementation.   
  • Support for LinearLayoutManager , GridLayoutManager and StaggeredGridLayoutManager .   
  • LayoutParams of your View s are honoured.   
  • The SpanSizeLookup of your GridLayoutManager continues to work without adjustments.   
  • Support for stable ids.  
  Include in your Project

   Add this to your root build.gradle (usually in the root of your project):
  1. repositories {
  2.     maven { url "https://jitpack.io" }
  3. }
复制代码
  And this to your module build.gradle (usually in the app directory):
  1. dependencies {
  2.     compile 'com.github.rubengees:EasyHeaderFooterAdapter:[email protected]'
  3. }
复制代码
If that doesn't work, look if there is a new version and the Readme was not updated yet.
  Usage

  There is almost zero configuration if you already have an adapter.
   You wrap your adapter in the EasyHeaderFooterAdapter and set it to the RecyclerView like this:
  1. YourAdapter adapter = new YourAdapter();
  2. EasyHeaderFooterAdapter easyHeaderFooterAdapter =
  3.                 new EasyHeaderFooterAdapter(adapter);
  4. //Always set the LayoutManager BEFORE the adapter.
  5. recyclerView.setLayoutManager(new SomeLayoutManager());
  6. recyclerView.setAdapter(easyHeaderFooterAdapter);
复制代码
  You can then set a header or footer through the setHeader and setFooter methods:
  1. headerFooterAdapter.setHeader(anyView);
  2. headerFooterAdapter.setFooter(anotherView);
复制代码
  Those can be removed with the removeHeader and removeFooter methods:
  1. headerFooterAdapter.removeHeader();
  2. headerFooterAdapter.removeFooter();
复制代码
And that's it! Easy right?
  There are a view things to look out for though.
  Positions

   If you are using the positions returned by the ViewHolder s getAdapterPosition method, you have to calculate the real position in your adapter. The method getRealPosition does exactly that:
  1. adapter.setCallback(new MainAdapter.MainAdapterCallback() {
  2.     @Override
  3.     public void onItemClick(int position) {
  4.         int positionInYourDataSet = headerFooterAdapter.getRealPosition(position);
  5.         // Do whatever you wanted to do with it.
  6.     }
  7. });
复制代码
  You can find more info on that in the sample .
  ViewTypes and Ids

  The following ViewTypes and IDs are used internally, so don't use them yourself:
  1. TYPE_HEADER = Integer.MIN_VALUE;
  2. TYPE_FOOTER = Integer.MIN_VALUE + 1;
  3. ID_HEADER = Long.MIN_VALUE;
  4. ID_FOOTER = Long.MIN_VALUE + 1;
复制代码
  Changing the LayoutManager at runtime
   As the EasyHeaderFooterAdapter needs to configure your LayoutManager , you have to reset the adapter to the RecyclerView if you want to change the LayoutManager :
  1. recycler.setLayoutManager(new YourNewLayoutManager());
  2. // Remember, always set if after the LayoutManager
  3. recycler.setAdapter(headerFooterAdapter);
复制代码
Further reading

   The sample features almost all use cases. Have a look here .
   You can find the JavaDoc here .
  Metrics

  Contributions and contributors

   A guide for contribution can be found here .
我要投稿

回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表