ashish2py

InstagramHeader android code

Apr 26th, 2012
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. package com.codeleaf.instaheader;
  2.  
  3. import java.util.List;
  4.  
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AbsListView;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15.  
  16. public class InstaHeaderActivity extends Activity implements AbsListView.OnScrollListener{
  17.  
  18.     ListView list;
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.         list = (ListView) findViewById(R.id.list);
  24.         list.setAdapter(new Adapter(this));        
  25.         list.setOnScrollListener(this);
  26.     }
  27.  
  28.     @Override
  29.     public void onScroll(AbsListView view, int firstVisibleItem,
  30.             int visibleItemCount, int totalItemCount) {    
  31.         //the listview has only few children (of course according to the height of each child) who are visible
  32.         for(int i=0; i < list.getChildCount(); i++){
  33.             View child = list.getChildAt(i);
  34.             ViewHolder holder = (ViewHolder) child.getTag();
  35.  
  36.             //if the view is the first item at the top we will do some processing
  37.             if(i == 0){            
  38.                 boolean isAtBottom = child.getHeight() <= holder.header.getBottom();
  39.                 int offset = holder.previousTop - child.getTop();
  40.                 if(!(isAtBottom && offset > 0)){                    
  41.                     holder.previousTop = child.getTop();
  42.                     holder.header.offsetTopAndBottom(offset);                  
  43.                     holder.header.invalidate();
  44.                 }
  45.             } //if the view is not the first item it "may" need some correction because of view re-use
  46.             else if (holder.header.getTop() != 0) {
  47.                 int offset = -1 * holder.header.getTop();
  48.                 holder.header.offsetTopAndBottom(offset);
  49.                 holder.previousTop = 0;
  50.                 holder.header.invalidate();
  51.             }
  52.         }
  53.     }
  54.  
  55.     @Override
  56.     public void onScrollStateChanged(AbsListView view, int scrollState) {}
  57.  
  58.     public class Adapter extends ArrayAdapter<String> {
  59.         public Adapter(Context context) {
  60.             super(context, R.layout.row, R.id.header);
  61.             for(int i=0; i < 50; i++){
  62.                 add(Integer.toString(i));
  63.             }
  64.         }
  65.  
  66.         @Override
  67.         public View getView(int position, View convertView, ViewGroup parent) {
  68.             if(convertView == null){
  69.                 convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
  70.                 ViewHolder holder = new ViewHolder();
  71.                 holder.header = (TextView) convertView.findViewById(R.id.header);
  72.                 convertView.setTag(holder);            
  73.             }
  74.             ViewHolder holder = (ViewHolder) convertView.getTag();
  75.             holder.header.setText(getItem(position));
  76.            
  77.             //callanother listview
  78.             ListView childList = (ListView) convertView.findViewById(R.id.childList);
  79.             ChildAdapter adapter = new ChildAdapter(InstaHeaderActivity.this);
  80.             childList.setAdapter(adapter);
  81.            
  82.             return convertView;
  83.         }
  84.     }
  85.  
  86.  
  87.     //child header adapter
  88.     public class ChildAdapter extends ArrayAdapter<String> {
  89.         public ChildAdapter(Context context) {
  90.             super(context, R.layout.child_header, R.id.childHeader);
  91.             for(int x=0; x< 6; x++){
  92.                 add(Integer.toString(x));
  93.             }
  94.         }
  95.  
  96.         @Override
  97.         public View getView(int position, View convertView, ViewGroup parent) {
  98.             if(convertView == null){
  99.                 convertView = LayoutInflater.from(getContext()).inflate(R.layout.child_header, parent, false);
  100.                 ViewHolder holder = new ViewHolder();
  101.                 holder.header = (TextView) convertView.findViewById(R.id.childHeader);
  102.                 convertView.setTag(holder);            
  103.             }
  104.             ViewHolder holder = (ViewHolder) convertView.getTag();
  105.             holder.header.setText(getItem(position));
  106.             return convertView;
  107.         }
  108.     }
  109.  
  110.  
  111.    
  112.     public class ViewHolder {
  113.         TextView header;
  114.         int previousTop = 0;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment