PinnedSectionListView使用技巧

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;
     }


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

@Override
	public boolean isItemViewTypePinned(int viewType) {
		// TODO Auto-generated method stub
		if(viewType==1){
			return true;
		}else
			
		return false;
	}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章