'horizontal'에 해당되는 글 1건

  1. 2013.05.28 HorizontalListView


===========================================================================================================

HorizontalListView.java


public class HorizontalListView extends LinearLayout {


    protected Context mContext;

    protected HorizontalScrollView mScrollView=null;

    protected LinearLayout mListLayout=null;

    protected int mSelectedPostion=0;


    public HorizontalListView(Context context) {

        super(context);

        mContext = context;

        initLayout();

    }


    public HorizontalListView(Context context, AttributeSet attrs) {

        super(context, attrs);

        mContext = context;

        initLayout();

    }


    public HorizontalListView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        mContext = context;

        initLayout();

    }

    private void initLayout(){

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.horizontal_listview, null);

        mScrollView = (HorizontalScrollView)layout.findViewById(R.id.horizontal_scrollview);

        mListLayout=(LinearLayout)layout.findViewById(R.id.horizontal_listview);

        super.addView(layout);

    }

    public void addView(View child){

        mListLayout.addView(child);

    }

    public void removeView(View child){

        mListLayout.removeView(child);

    }

    public void clear(){

        mListLayout.removeAllViews();

    }

    @Override

    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        super.onLayout(changed, l, t, r, b);

        if(mSelectedPostion > 0 && mSelectedPostion < mListLayout.getChildCount()){

            View v = mListLayout.getChildAt(mSelectedPostion);

            if(v == null){

                return;

            }


            mScrollView.computeScroll();

            int scrollWidth = mScrollView.getWidth();

            int layoutRight = mListLayout.getRight();

            int viewLeft = v.getLeft();

            final int scrollX;


            if(viewLeft+scrollWidth < layoutRight){

                scrollX = viewLeft;

            }else{

                scrollX = layoutRight - scrollWidth;

            }


            mScrollView.post(new Runnable() {

                @Override

                public void run() {

                    mScrollView.scrollTo(scrollX, mScrollView.getScrollY());

                }

            });


        }

    }


    public void setSelection(int position){

        if(position>=0 && position < mListLayout.getChildCount()){

            mSelectedPostion = position;

        }

    }

}

===========================================================================================================

layout/horizontal_listview.xml


<?xml version="1.0" encoding="utf-8"?>

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/horizontal_scrollview"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:scrollbars="none">"


    <LinearLayout

        android:id="@+id/horizontal_listview"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"/>


</HorizontalScrollView>



저작자 표시 비영리 변경 금지
신고
크리에이티브 커먼즈 라이선스
Creative Commons License

'programmer > android' 카테고리의 다른 글

Dialog softkeyboard  (0) 2013.06.10
apache 압축 라이브러리를 이용한 압축 클래스  (0) 2013.05.29
HorizontalListView  (0) 2013.05.28
onTouch와 onTouchEvent  (0) 2013.02.18
PendingIntent  (1) 2012.12.14
Toast 팝업  (0) 2012.12.13
Posted by ocsoosoo

댓글을 달아 주세요

이전버튼 1 이전버튼

티스토리 툴바