2012-10-12 50 views
1

我不確定如何在單個視圖中集成這兩個傳呼機。我想要做的是,這些圖像應該能夠在垂直和水平方向上滑動。這個屏幕在屏幕上會有一個頁面指示器。如何在android的單個視圖中結合水平尋呼機和垂直尋呼機?

scrollView = (ScrollView) findViewById(R.id.scroll_view); 
    contentView = (ViewGroup) findViewById(R.id.content); 

    scrollView.setOnTouchListener(new ScrollPager(scrollView, contentView)); 
    scrollView.post(new Runnable() 
    { 
      public void run() 
      { 
        scrollView.scrollTo(0, contentView.getPaddingTop()); 
      } 
    });   

    final PagerControl control = (PagerControl) findViewById(R.id.control); 
    final HorizontalPager pager = (HorizontalPager) findViewById(R.id.pager); 
    control.setNumPages(pager.getChildCount()); 

    pager.addOnScrollListener(new HorizontalPager.OnScrollListener() { 
     public void onScroll(int scrollX) { 
      //Log.d("TestActivity", "scrollX=" + scrollX); 
      float scale = (float) (pager.getPageWidth() * pager.getChildCount())/ (float) control.getWidth(); 
      control.setPosition((int) (scrollX/scale)); 

     } 

     public void onViewScrollFinished(int currentPage) { 
      //Log.d("TestActivity", "viewIndex=" + currentPage); 
      control.setCurrentPage(currentPage); 
     } 
    }); 

和XML是

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:example="http://schemas.android.com/apk/res/com.example.page" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
> 
<com.example.page.HorizontalPager 
    android:id="@+id/pager" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    example:pageWidth="250dip" 
    > 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scroll_view" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<LinearLayout 
    android:id="@+id/content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingTop="200dip" 
    android:paddingBottom="200dip"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:background="#0a0" 
     android:text="Text 1" 
     android:focusable="true" 
     /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:background="#00a" 
     android:text="Text 2" 
     android:focusable="true" 
     /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Text 3" 
     /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:background="#a00" 
     android:text="Text 4" 
     android:focusable="true" 
     /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:background="#0aa" 
     android:text="Text 5" 
     android:focusable="true" 
     /> 
</ScrollView> 
</com.example.page.HorizontalPager> 
<com.example.page.PagerControl 
    android:id="@+id/control" 
    android:layout_width="fill_parent" 
    android:layout_height="4dip" 
    android:layout_margin="8dip" 
    example:roundRectRadius="2dip" 
    /> 
</LinearLayout> 
+0

您嘗試過'ViewPager' +'ScrollView' – user1049280

+0

是的,但沒有工作 – sabeer

+0

這是什麼問題? – user1049280

回答

0

所以,這裏是我的代碼:

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 
List<View> pages = new ArrayList<View>(); 
// add here all views that you need 
pages.add(view_1); 
pages.add(view_2); 
... 
pages.add(view_N); 
/////////// 
MyPageAdapter pageAdapter = new MyPageAdapter(pages); 
viewPager.setAdapter(pageAdapter); 
viewPager.setCurrentItem(0); 

<android.support.v4.view.ViewPager 
    android:id="@+id/view_pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
/> 

的觀點尋呼機代碼:在主要佈局的XML

viewpager

和MyPageAdapter:

public class MyPageAdapter extends PagerAdapter{ 

List<View> pages = null; 

public MyPageAdapter(List<View> pages){ 
    this.pages = pages; 
} 

@Override 
public Object instantiateItem(View collection, int position){ 
    View v = pages.get(position); 
    ((ViewPager) collection).addView(v, 0); 
    return v; 
} 

@Override 
public void destroyItem(View collection, int position, Object view){ 
    ((ViewPager) collection).removeView((View) view); 
} 

@Override 
public int getCount(){ 
    return pages.size(); 
} 

@Override 
public boolean isViewFromObject(View view, Object object){ 
    return view.equals(object); 
} 

@Override 
public void finishUpdate(View arg0){ 
} 

@Override 
public void restoreState(Parcelable arg0, ClassLoader arg1){ 
} 

@Override 
public Parcelable saveState(){ 
    return null; 
} 

@Override 
public void startUpdate(View arg0){ 
} 

@Override 
public int getItemPosition(Object object) { 
    return POSITION_NONE; 
} 

}