2013-03-22 5 views
6

내부에 몇 가지 조각 클래스가있는 fragmentPagerAdapter viewpager이 있습니다. 따라서이 viewpager은 왼쪽과 오른쪽으로 스 와이프를 사용하여 가로로 스크롤합니다. 문제는 각 페이지에서 내 값이 페이지 크기를 초과하고 세로로 스크롤해야한다는 것입니다. 하지만 수직 스크롤을 사용할 수는 없습니다. 내가 가지고있는 모든 scrollview을 삭제했으며 코드를 작동 상태로 만들었습니다. 여기 뷰 페이지의 세로 스크롤

내 코드입니다 : (카테고리에 가로로 스크롤 수직 응용 프로그램을보고 나는 안드로이드 앱 스토어 같은 것을 할 수 있습니다.) :

MyFragmentPagerAdapter :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:isScrollContainer="true"> 

      <android.support.v4.view.PagerTitleStrip 
       android:id="@+id/pager_title_strip" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="top" 
       android:background="#33b5e5" 
       android:paddingBottom="4dp" 
       android:paddingTop="4dp" 
       android:textColor="#fff" /> 
     </android.support.v4.view.ViewPager> 
</RelativeLayout> 

조각의 활동이 필요 수직 스크롤 : 여기

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="10dp" 
     android:text="Last fill-up spent :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="15dp" 
     android:text="Month total spent :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/currentMoneySpentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView2" 
     android:layout_marginLeft="24dp" 
     android:layout_toRightOf="@+id/textView1" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/monthTotalSpentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_alignLeft="@+id/currentMoneySpentText" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView2" 
     android:layout_below="@+id/textView2" 
     android:layout_marginTop="15dp" 
     android:text="Year total spent :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView3" 
     android:layout_below="@+id/textView3" 
     android:layout_marginTop="15dp" 
     android:text="Since 1st addition :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/yearTotalSpentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView4" 
     android:layout_alignLeft="@+id/monthTotalSpentText" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/firstTotalSpentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView4" 
     android:layout_alignLeft="@+id/yearTotalSpentText" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView6" 
     android:layout_below="@+id/textView6" 
     android:layout_marginTop="1dp" 
     android:text="since last addition :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView7" 
     android:layout_below="@+id/textView7" 
     android:layout_marginTop="15dp" 
     android:text="Distance covered" 
     android:textSize="15dp" /> 
</RelativeLayout> 

는 관련이 내 조각 클래스의 처음 몇 줄은 다음과 같습니다

public class MyFragmentDetails extends Fragment { 

private RelativeLayout ll; 
private FragmentActivity fa; 
DBAdapter db; 

// MyFragmentFuel fFuels; 
private String carSelected, m, y, lowestDistance, highestDistance; 

BroadcastReceiver receiver; 
int highestDay = 0; 
int lowestDay = 31; 

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
} 

@SuppressWarnings("static-access") 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    fa = super.getActivity(); 
    super.onCreate(savedInstanceState); 
    ll = (RelativeLayout) inflater.inflate(
      R.layout.activity_add_fuel_details, container, false); 
+0

의이 클래스의

http://stackoverflow.com/questions/8381697/ viewpager-inside-a-scrollview-does-not-scroll-correclty 이것을 사용해 보시겠습니까? – dmnlk

+0

필자는이 코드를 보았지만 어디에서 사용해야하는지 잘 모릅니다. –

+0

당신은 MyFragmentPagerAdapter 레이아웃을 inflate하는 Fragment 클래스의 onStart() 메소드에서이 코드를 사용합니다. – dmnlk

답변

0

NestedScrollView에 내용을 포장하십시오.

scrollView.setOnTouchListener(new View.OnTouchListener() { 
     int dragthreshold = 30; 
     int downX = 0; 
     int downY = 0; 

     @Override 
     public boolean onTouch(View view, MotionEvent event) { 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        downX = (int) event.getRawX(); 
        downY = (int) event.getRawY(); 
        break; 

       case MotionEvent.ACTION_MOVE: 
        int distanceX = Math.abs((int) event.getRawX() - downX); 
        int distanceY = Math.abs((int) event.getRawY() - downY); 

        if (distanceY > distanceX) { 
         view.getParent().requestDisallowInterceptTouchEvent(true); 
        } else if (distanceX > distanceY && distanceX > dragthreshold) { 
         view.getParent().requestDisallowInterceptTouchEvent(false); 
        } 
        break; 

       case MotionEvent.ACTION_UP: 
        view.getParent().requestDisallowInterceptTouchEvent(false); 
        break; 
      } 
      return false; 
     } 
    }); 
1
Try putting your content in scroll view like this: 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="10dp" 
     android:text="Last fill-up spent :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="15dp" 
     android:text="Month total spent :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/currentMoneySpentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView2" 
     android:layout_marginLeft="24dp" 
     android:layout_toRightOf="@+id/textView1" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/monthTotalSpentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_alignLeft="@+id/currentMoneySpentText" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView2" 
     android:layout_below="@+id/textView2" 
     android:layout_marginTop="15dp" 
     android:text="Year total spent :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView3" 
     android:layout_below="@+id/textView3" 
     android:layout_marginTop="15dp" 
     android:text="Since 1st addition :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/yearTotalSpentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView4" 
     android:layout_alignLeft="@+id/monthTotalSpentText" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/firstTotalSpentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView4" 
     android:layout_alignLeft="@+id/yearTotalSpentText" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView6" 
     android:layout_below="@+id/textView6" 
     android:layout_marginTop="1dp" 
     android:text="since last addition :" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView7" 
     android:layout_below="@+id/textView7" 
     android:layout_marginTop="15dp" 
     android:text="Distance covered" 
     android:textSize="15dp" /> 
</LinearLayout> 
</ScrollView> 

를 그 후에 원하는 경우 : 스크롤은 아직 (내 경우에는하지 않았다) 작동하지 않는 경우, 수직 스크롤하는 경우 그 부모에 requestDIsallowInterceptTouchEvent(true)를 호출하는 OnTouchListener 추가 시도 당신의 스크롤에 특정 이벤트를 수행 한 후 사용

이 위에 해당하는 안드로이드 버전 멜로와는 다음과 같습니다

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       mScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() { 
        @Override 
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 
         if (view.getTop() == scrollY) { 
          Log.e(TAG, "onScrollChange: top"); 
         } else { 

         } 
        } 
       }); 
      } 

낮은 버전의 사용자가 사용할 수 있습니다 :

final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener = new 
        ViewTreeObserver.OnScrollChangedListener() { 

         @Override 
         public void onScrollChanged() { 
          int scrollY = mScrollView.getScrollY(); // For ScrollView 
          int scrollX = mScrollView.getScrollX(); // For HorizontalScrollView 
          // DO SOMETHING WITH THE SCROLL COORDINATES 
          if (view.getTop() == scrollY) { 

           Log.e(TAG, "ViewTreeObserver onScrollChange: top"); 
          } 
         } 
        }; 
      mScrollView.setOnTouchListener(new View.OnTouchListener() { 
       private ViewTreeObserver observer; 

       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        if (observer == null) { 
         observer = mScrollView.getViewTreeObserver(); 
         observer.addOnScrollChangedListener(onScrollChangedListener); 
        } 
        else if (!observer.isAlive()) { 
         observer.removeOnScrollChangedListener(onScrollChangedListener); 
         observer = mScrollView.getViewTreeObserver(); 
         observer.addOnScrollChangedListener(onScrollChangedListener); 
        } 

        return false; 
       } 
      }); 
+0

ScrollView는 직접 자식을 두 명 이상 허용하지 않습니다! – Sadegh

+0

맞습니다. 편집에 자녀가 빠졌습니다. –

0

넣어 프로젝트에 클래스를 만듭니다이

public class VerticalViewPager extends ViewPager { 

public VerticalViewPager(Context context) { 
    super(context); 
    init(); 
} 

public VerticalViewPager(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

private void init() { 
    // The majority of the magic happens here 
    setPageTransformer(true, new VerticalPageTransformer()); 
    // The easiest way to get rid of the overscroll drawing that happens on the left and right 
    setOverScrollMode(OVER_SCROLL_NEVER); 
} 

private class VerticalPageTransformer implements ViewPager.PageTransformer { 

    @Override 
    public void transformPage(View view, float position) { 

     if (position < -1) { // [-Infinity,-1) 
      // This page is way off-screen to the left. 
      view.setAlpha(0); 

     } else if (position <= 1) { // [-1,1] 
      view.setAlpha(1); 

      // Counteract the default slide transition 
      view.setTranslationX(view.getWidth() * -position); 

      //set Y position to swipe in from top 
      float yPosition = position * view.getHeight(); 
      view.setTranslationY(yPosition); 

     } else { // (1,+Infinity] 
      // This page is way off-screen to the right. 
      view.setAlpha(0); 
     } 
    } 
} 

/** 
* Swaps the X and Y coordinates of your touch event. 
*/ 
private MotionEvent swapXY(MotionEvent ev) { 
    float width = getWidth(); 
    float height = getHeight(); 

    float newX = (ev.getY()/height) * width; 
    float newY = (ev.getX()/width) * height; 

    ev.setLocation(newX, newY); 

    return ev; 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev){ 
    boolean intercepted = super.onInterceptTouchEvent(swapXY(ev)); 
    swapXY(ev); // return touch coordinates to original reference frame for any child views 
    return intercepted; 
} 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    return super.onTouchEvent(swapXY(ev)); 
} 

} XML 호출 대신 android.support.v4.view.ViewPager

관련 문제