开发者

Problem in HorizontaScrollView and swiping the screen

In my code i m implementing swipe using the HorizontalScrollView for that i got a proper working code , its working fine to swipe left or right now my problem is i wand to show a dynamic page no. for that i added some extra code in that but its not working the code is below

public class PaginationLayout extends LinearLayout {

    private int mPageActive = 0;
    private HorizontalScrollView mScroll;
    private LinearLayout mBar;

    public PaginationLayout(Context context) {
        super(context);

        setOrientation(LinearLayout.VERTICAL);

        final GestureDetector mGestureDetector = new GestureDetector(new MySimpleOnGestureListener());

        mScroll = new HorizontalScrollView(context);
        mScroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        mScroll.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (mGestureDetector.onTouchEvent(event)) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        super.addView(mScroll);

    }

    @Override
    public void addView(View child) {
        mScroll.addView(child);
    }

    @Override
    protected void onSizeChanged(int arg0, int arg1, int arg2, int arg3) {
        super.onSizeChanged(arg0, arg1, arg2, arg3);
        View chield = mScroll.getChildAt(0);
        if (chield != null) {
            if (chield.getMeasuredWidth() > getWidth()) {
                mBar.setVisibility(LinearLayout.VISIBLE);
            } else {
                mBar.setVisibility(LinearLayout.INVISIBLE);
            }
        }
    }


    public void previous() {
        mPageActive = (mPageActive > 0) ? mPageActive - 1 : 0;
        mScroll.smoothScrollTo(mPageActive * mScroll.getWidth(), 0);
    }


    public void next() {
        int pageWidth = mScroll.getWidth();
        int nextPage = (mPageActive + 1) * pageWidth;
        if (nextPage - mScroll.getScrollX() <= pageWidth) {
            mScroll.smoothScrollTo(nextPage, 0);
            mPageActive++;
        } else {
            mScroll.smoothScrollTo(mScroll.getScrollX(), 0);
        }
    }


    private class MySimpleOnGestureListener extends SimpleOnGestureListener {

开发者_StackOverflow社区        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            if (e1 != null && e2 != null) {
                if (e1.getX() - e2.getX() > 0) {
                    // forward...
                    next();
                    return true;
                } else if (e2.getX() - e1.getX() > 0) {
                    // back...
                    previous();
                    return true;
                }
            }

            return false;
        }
    }
public void setPage(int noOfPageToScroll   ) {

    int pageWidth = mScroll.getWidth(); //problem is coming in this line its coming 0 always
    int nextPage = (noOfPageToScroll + 1) * pageWidth;

    Log.v("trace","Pagination.setPage() called with noOfPageToScroll = "+noOfPageToScroll+"and nextPage =  "+nextPage+ " and pageWidth = "+pageWidth);
    mScroll.smoothScrollTo(nextPage, 0);
}
}

//-------------inside the my activity i m using this java file

public void setUpViews(){

    try {
        TableLayout table = new TableLayout(getBaseContext());
        table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        table.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
        TableRow row = new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        table.addView(row);
        LayoutInflater inflater = (LayoutInflater)   getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

              //throught inflator i m adding multiple dynamic Views in row
        paginationLayout.addView(table);

        // set pagination layout
        setContentView(paginationLayout);

     //here i m calling want to go to specific page so i m calling 
                 paginationLayout.setPage(gotopageno );
             //but every time is showing the first page and of scroll view and the  log 06-24 16:38:39.156: VERBOSE/trace(894): setPage() called with noOfPageToScroll = gotopageno and nextPage =  0 and pageWidth = 0

// as much i understand the problem is that i m not getting the mscroll.width() == 0 it is called before draw view or may be some other reason

    } catch (Exception e) {
        Log.e("Error", "Problem in setUpViews() code 4 ");
        e.printStackTrace();
    }

}

//Please help me and ignore any grammer mistake. Thanks in advance


I solved this problem using the viewflipper as in HorrizontalScrollView the View size is becoming very big.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜