1

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

    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>
4

2 回答 2

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

}
于 2012-10-15T06:56:13.630 回答
0

你可以看看这个网站http://vision-apps.blogspot.com/2013/06/solution-for-android-viewpager-inside.html

我希望它会帮助你。

于 2013-11-27T09:28:22.987 回答