如何在Android的单个视图中组合水平寻呼机和垂直寻呼机?

时间:2012-10-12 13:29:07

标签: android android-viewpager

我不确定如何在一个视图中集成这两个寻呼机。我想做的是,图像应该能够垂直和水平滑动。此屏幕将在屏幕中显示页面指示符。

    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>

2 个答案:

答案 0 :(得分:0)

所以,这是我的代码:

主要布局的xml中的viewpager:

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

查看寻呼机的代码:

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);

和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;
}

}

答案 1 :(得分:0)

您可以查看此网页http://vision-apps.blogspot.com/2013/06/solution-for-android-viewpager-inside.html

我希望它会对你有所帮助。