Android Listview Custom Section Header

前端 未结 5 1428
挽巷
挽巷 2020-12-24 10:04

How can i implement custom section or header of ListView like Instagram app in android.

http://prsarahevans.com/wp-content/uploads/2011/06/photo.PNG

When scr

5条回答
  •  生来不讨喜
    2020-12-24 10:26

    I've improved Siyamed's question a little bit so that the header is not gone when the view is redrawn for example if a bitmap in the listview item is changed.

    Instead of using coordinates relative to the last position I use coordinates relative to the top of the view and use padding instead of offsets.

    @Override
    public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    
        for(int i=0; i < list.getChildCount(); i++){
            View child = list.getChildAt(i);
            ViewHolder holder = (ViewHolder) child.getTag();
    
            if(i == 0){
                try {
                    boolean isAtBottom = child.getHeight() <= holder.movingHeader.getBottom();
                    if(!(isAtBottom)){
                        if (child.getTop() >= 0){}
                        else if (child.getHeight() - movingHeader.getHeight() - 1 > -child.getTop())
                        {
                            holder.movingHeader.setPadding(0, -child.getTop(), 0, 0);
                            holder.movingHeader.invalidate();
                        }
                        else {
                            holder.movingHeader.setPadding(0, child.getHeight() - movingHeader.getHeight(), 0, 0);
                            holder.movingHeader.invalidate();
                        }
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            else if (holder.movingHeader.getPaddingTop() != 0)
            {
                holder.movingHeader.setPadding(0, 0, 0, 0);
                holder.movingHeader.invalidate();
            }
        }
    }
    

提交回复
热议问题