Android Open Source - AndroidSectionHeaders Section List View






From Project

Back to project page AndroidSectionHeaders.

License

The source code is released under:

Apache License

If you think the Android project AndroidSectionHeaders listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.marczych.androidsectionheaders;
// w w w.  java  2  s .c  o m
import android.content.Context;

import android.util.AttributeSet;

import android.view.LayoutInflater;
import android.view.View;

import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.FrameLayout;

public class SectionListView extends FrameLayout implements
 AbsListView.OnScrollListener {
   private ListView mListView;
   private SectionHeadersAdapter mAdapter;
   private View mPinnedHeader;
   private int mHeaderPosition = -1;

   public SectionListView(Context context) {
      super(context);
      init(context);
   }

   public SectionListView(Context context, AttributeSet attrs) {
      super(context, attrs);
      init(context);
   }

   public SectionListView(Context context, AttributeSet attrs, int def) {
      super(context, attrs, def);
      init(context);
   }

   private void init(Context context) {
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(
       Context.LAYOUT_INFLATER_SERVICE);

      inflater.inflate(R.layout.section_list_view, this, true);

      mListView = (ListView)findViewById(R.id.list_view);
   }

   public ListView getListView() {
      return mListView;
   }

   public void setAdapter(SectionHeadersAdapter adapter) {
      mAdapter = adapter;
      mListView.setAdapter(mAdapter);
      mListView.setOnScrollListener(this);
   }

   public void onScroll(AbsListView view, int firstVisibleItem,
    int visibleItemCount, int totalItemCount) {
      int headerPos;

      if ((headerPos = mAdapter.getHeaderPosition(firstVisibleItem)) !=
       mHeaderPosition) {
         mHeaderPosition = headerPos;
         injectPinnedHeader(mAdapter.getView(mHeaderPosition, mPinnedHeader,
          this));
      }

      int nextHeader = mAdapter.getNextHeaderPosition(firstVisibleItem);
      int nextHeaderChild = nextHeader - firstVisibleItem;
      int top;
      View nextView = mListView.getChildAt(nextHeaderChild);


      // Pin the header at the top if the next header is not in view or the
      // next header is "pushing" the current header up
      if (nextView == null || nextView.getTop() > mPinnedHeader.getHeight()) {
         top = mPinnedHeader.getHeight();
      } else {
         top = nextView.getTop();
      }

      mPinnedHeader.layout(mPinnedHeader.getLeft(),
                           top - mPinnedHeader.getHeight(),
                           mPinnedHeader.getRight(),
                           top);
   }

   public void onScrollStateChanged(AbsListView view, int scrollState) {
   }

   private void injectPinnedHeader(View header) {
      // Adapter isn't using the convertView so we must remove the previous one
      if (mPinnedHeader != header) {
         if (mPinnedHeader != null) {
            removeView(mPinnedHeader);
         }

         FrameLayout.LayoutParams layoutParams =
          new FrameLayout.LayoutParams(
          FrameLayout.LayoutParams.FILL_PARENT,
          FrameLayout.LayoutParams.WRAP_CONTENT);

         addView(header, layoutParams);
      }

      mPinnedHeader = header;
   }
}




Java Source Code List

com.marczych.androidsectionheaders.SectionHeadersAdapter.java
com.marczych.androidsectionheaders.SectionListView.java
com.marczych.androidsectionheaders.Section.java
com.marczych.androidsectionheaders.sample.SampleSectionAdapter.java
com.marczych.androidsectionheaders.sample.SectionHeadersSampleActivity.java