PinnedSectionListView使用技巧

来源:互联网 发布:下载默默聊天软件 编辑:程序博客网 时间:2024/06/04 00:40

githab上一款很好用的分组的listview滑动中固定标题的实现

https://github.com/beworker/pinned-section-listview

查看demo,知道使用pinned-section-listview,仅仅需要将封装好的PinnedSectionListView拷贝至自己的项目中

<com.hb.views.PinnedSectionListView      android:id="@android:id/list"      android:layout_width="match_parent"      android:layout_height="wrap_content"       />

除此之外还需要添加一部分代码在adapter中:

 // Our adapter class implements 'PinnedSectionListAdapter' interface  class MyPinnedSectionListAdapter extends BaseAdapter           implements PinnedSectionListAdapter {      ...      // We implement this method to return 'true' for all view types we want to pin      @Override      public boolean isItemViewTypePinned(int viewType) {          return viewType == <type to be pinned>;      }  }

isItemViewTypePinned  这个方法的作用是确定哪个item是title,当item为需要固定的title时,返回true。
那么如何确定item是否是title?需要重写另外两个构造方法:
所给的demo中是这样确定的:
 @Override public int getViewTypeCount() {            return 2;        }        @Override public int getItemViewType(int position) {            return getItem(position).type;        }        @Override        public boolean isItemViewTypePinned(int viewType) {            return viewType == Item.SECTION;        }

getViewTypeCount()有几种类型的item
<pre name="code" class="java" style="color: rgb(148, 82, 119); font-size: 14px; font-weight: bold; line-height: 20px;">getItemViewType(int position)返回item的类型,与<span style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace;">isItemViewTypePinned(int viewType)对应</span>
再写一种demo:
 @Override  public int getViewTypeCount() {         return 2;     }


@Overridepublic int getItemViewType(int position) {// TODO Auto-generated method stubItem item=items.get(position);if(item.isSection()){return 1;}else{return 0;}}

@Overridepublic boolean isItemViewTypePinned(int viewType) {// TODO Auto-generated method stubif(viewType==1){return true;}elsereturn false;}



0 0