Code example for RelativeLayout

Methods: getLayoutParams, setLayoutParams, setVisibility, startAnimation

0
view.measure(childWidthSpec, childHeightSpec);
    } 
     
    public void setScrollbarPosition(int margin) {
    	FrameLayout.LayoutParams layoutParam = (FrameLayout.LayoutParams) mScrollbar.getLayoutParams();
    	layoutParam.topMargin = margin;
    	mScrollbar.setLayoutParams(layoutParam);
    } 
     
    private void init(Context context) {
    	mScrollFlag = true;
        setWillNotDraw(false);
        setOnHierarchyChangeListener(this);
        mSectionFade = new SectionFade();
    } 
     
    public void onScrollStateChanged(AbsListView view, int scrollState) {}
	 
    public void onScroll(AbsListView view, int visibleIndex, int visibleCount, int totalCount) {
        if (totalCount - visibleCount > 0 && !mDragFlag) {
        	mScrollY = ((getHeight() - mScrollHeight) * visibleIndex) / (totalCount - visibleCount);
            setScrollbarPosition(mScrollY);
        } 
        mScrollFlag = true;
        if (visibleIndex == mVisibleTopIndex) {
            return; 
        } 
         
        mVisibleTopIndex = visibleIndex;        
        setSectionDisplay(visibleIndex);
        startSectionAnimation(true);         
        mHandler.removeCallbacks(mSectionFade);
        if (!mDragFlag) {
            mHandler.postDelayed(mSectionFade, 1500);
        } 
    } 
 
    public void onChildViewAdded(View parent, View child) {
 
    	if (child instanceof GridView) {
        	mGridView = (GridView)child;
        	mGridView.setOnScrollListener(this);
        	mGridView.setOnHierarchyChangeListener(this);
        } else if(child instanceof RelativeLayout) {
        	if(child.getId() == R.id.rLScrollbar) {
        		mScrollbar = (RelativeLayout)child;
        		setScrollbarSize(); 
        	} 
        } else if(child instanceof TextView){
        	if(child.getId() == R.id.tVSection)
        		mTextSection = (TextView)child;
        } 
    } 
 
    public void onChildViewRemoved(View parent, View child) {
        if (child == mGridView) {
        	mGridView = null;
            mListAdapter = null;
            mSections = null;
        } else if(child == mScrollbar) {
        	mScrollbar = null;
        } else if(child == mTextSection){
        	mTextSection = null;
        } 
    } 
     
    @Override 
    public boolean onTouchEvent(MotionEvent event) {
    	event.setLocation(event.getX(), event.getY() + mScrollHeight/2); // adjust Scroll bar position => Touch Y
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (event.getX() > getWidth() - mScrollWidth
                    && event.getY() >= mScrollY  + mScrollHeight/2
                    && event.getY() <= mScrollY + mScrollHeight + mScrollHeight/2) {
            	mDragFlag = true;
                cancelFling(); 
                return true; 
            } 
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (mDragFlag) {
            	mDragFlag = false;
                final Handler handler = mHandler;
                handler.removeCallbacks(mSectionFade);
                handler.postDelayed(mSectionFade, 1500);
                return true; 
            } 
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (mDragFlag) {
                final int viewHeight = getHeight();
                mScrollY = (int) event.getY() - mScrollHeight;
                if (mScrollY < 0) {
                	mScrollY = 0;
                } else if (mScrollY + mScrollHeight > viewHeight) {
                	mScrollY = viewHeight - mScrollHeight;
                } 
                setScrollbarPosition(mScrollY);
                if (mScrollFlag) {
                    scrollTo((float) mScrollY / (viewHeight - mScrollHeight));
                } 
                return true; 
            } 
        } 
         
        return super.onTouchEvent(event);
    } 
     
    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (mScrollVisibleFlag && ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (ev.getX() > getWidth() - mScrollWidth && ev.getY() >= mScrollY &&
                    ev.getY() <= mScrollY + mScrollHeight) {
            	mDragFlag = true;
                return true; 
            }             
        } 
        return super.onInterceptTouchEvent(ev);
    } 
 
    private void scrollTo(float position) {
        int count = mGridView.getCount();
        mScrollFlag = false;
        final Object[] sections = mSections;
        if (sections != null && sections.length > 1) {
            final int nSections = sections.length;
            int section = (int) (position * nSections);
            if (section >= nSections) {
                section = nSections - 1;
            } 
            final SectionIndexer baseAdapter = (SectionIndexer) mListAdapter;
            int index = baseAdapter.getPositionForSection(section);
            int nextIndex = count;
            int prevIndex = index;
            int prevSection = section;
            int nextSection = section + 1;
 
            if (section < nSections - 1) {
                nextIndex = baseAdapter.getPositionForSection(section + 1);
            } 
             
            if (nextIndex == index) {
                while (section > 0) {
                    section--;
                     prevIndex = baseAdapter.getPositionForSection(section);
                     if (prevIndex != index) {
                         prevSection = section;
                         break; 
                     } 
                } 
            } 
            int nextNextSection = nextSection + 1;
            while (nextNextSection < nSections &&
                    baseAdapter.getPositionForSection(nextNextSection) == nextIndex) {
                nextNextSection++;
                nextSection++;
            } 
            float fPrev = (float) prevSection / nSections;
            float fNext = (float) nextSection / nSections;
            index = prevIndex + (int) ((nextIndex - prevIndex) * (position - fPrev) / (fNext - fPrev));
            if (index > count - 1) index = count - 1;
             
            mGridView.setSelection(index);
        } else { 
            int index = (int) (position * count);
            mGridView.setSelection(index);
        } 
    } 
 
    private void cancelFling() { 
        MotionEvent cancelFling = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
        mGridView.onTouchEvent(cancelFling);
        cancelFling.recycle();
    } 
     
    /** 
     * Setting Section 
     * @param visibleItem Item position 
     */ 
    private void setSectionDisplay(int visibleItem) {
    	if(mGridView == null || mGridView.getCount() <= visibleItem) return;
    	try { 
    	    MediaItem item = (MediaItem)mGridView.getItemAtPosition(visibleItem);
    	    if (item == null) return;
        	String section = "";
        	if (Setting.INSTANCE.getCompareMode() == Setting.COMPARE_DATE_ASC || Setting.INSTANCE.getCompareMode() == Setting.COMPARE_DATE_DESC){ 
        		section = item.getDateTaken();
        		if(section == null) {
        			section = "1970.01.01";
        		}else{ 
    	    		Date date = new Date(Long.parseLong(section));            
    	            section = DateFormat.format("yyyy.MM.dd", date).toString();
        		} 
        	}else{ 
        		section = (item.getDisplayName().length() >= 1) ? item.getDisplayName().substring(0, 1) : ".";
        	} 
            mTextSection.setText(section);
        } catch(IndexOutOfBoundsException e) {
            mTextSection.setVisibility(GONE);
        } 
    } 
     
	private void startSectionAnimation(boolean fadeFlag) {
		Animation alpha;
		 
		if(fadeFlag) {
			if(mScrollVisibleFlag) return;
		 
			mScrollVisibleFlag = true;		
			alpha = new AlphaAnimation(0.0f, 1.0f);
			mScrollbar.setVisibility(VISIBLE);
			mTextSection.setVisibility(VISIBLE);	
		} else { 
			if(!mScrollVisibleFlag) return;
			 
			mScrollVisibleFlag = false;			
			alpha = new AlphaAnimation(1.0f, 0.0f);
		} 
		alpha.setDuration(500);
		alpha.setFillAfter(true);
 
		mScrollbar.startAnimation(alpha);
		mTextSection.startAnimation(alpha);
	} 
	 
	public class SectionFade implements Runnable {
		@Override