CodePrimate
CodePrimate

Reputation: 6666

How do I create a Horizontal Scroll View with ViewPager functionality?

I've tried figuring out a way to solve this problem by using the viewpager but I've been unable to do so entirely.

Here is the tasks I have been assigned to solve:

enter image description here

Is there any way of solving this on the Android platform? Is there a way to simply define one layout that is larger than the phones dimension and somehow be able to page between the visible part of the view and the non-visible part?

Upvotes: 1

Views: 1802

Answers (1)

Krishna Shrestha
Krishna Shrestha

Reputation: 1662

you should work on onInterceptTouchEvent. when you click on the horizontalScrollview you should return false in onInterceptTouchEvent else return true;

Here is, How i did

1)make CustomViewpager extending ViewPager 2)override onInterceptTouchEvent like this

 @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
     if (childId > 0) {
         View scroll = findViewById(childId);
         if (scroll != null) {
             Rect rect = new Rect();
             scroll.getHitRect(rect);
             if (rect.contains((int) event.getX(), (int) event.getY())) {
                 return false;
             }
         }
     }
     return true;
}
private int childId;
public void setChildId(int id) {
    this.childId = id;
}

3)use customViewpager instead of viewpager in xml like this

<com.packagename.CustomViewpager .....

4) and set childId from activity like this

CustomViewpager cv=(CustomViewpager)findViewById(R.id.id_of_customviewpager);
cv.setChildId(R.id.id_of_customviewpager);

5)done

Logic is when you touch HorizontalScrollview give the touch to child other wise give touch to parent

Upvotes: 1

Related Questions