-4

我必须为我的项目实现滑动方法。任何人都可以分享任何链接或示例/代码来左右滑动活动而不使用 Flipper 方法,因为它只在布局内部滑动,而不是完整的活动。

4

1 回答 1

2

这就是我目前对我的项目所做的事情:

public class CustomPagerAdapter extends PagerAdapter {

private final int NUM_PAGES;
private final String[] titles;
private final View[] views;

public CustomPagerAdapter(View[] views, String[] titles){
    super();
    this.NUM_PAGES = views.length;
    this.views = views;
    this.titles = titles;
}   

@Override
public int getCount(){
    return NUM_PAGES;       
}

@Override
public CharSequence getPageTitle(int position){
    return titles[position];
}

@Override
public Object instantiateItem(View collection, int position) {
    ((ViewPager) collection).addView(views[position]);
    return views[position];
}

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

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

}

您将要滑动的视图和视图的标题传递给构造函数。要使用此适配器,请添加:

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

            <android.support.v4.view.PagerTitleStrip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top" 
                android:textColor="@color/title_strip_text_color"
                android:textStyle="bold"
                android:background="@color/title_strip_background_color"/>
</android.support.v4.view.ViewPager>

到您的 xml 布局,然后在 Activity 的 OnCreate(或 Fragment 的 OnCreateView)中添加以下行:

ViewPager viewPager = (ViewPager) edit_hero_fragment_layout.findViewById(R.id.view_pager);
CustomPagerAdapter edit_hero_pager_adapter = new CustomPagerAdapter(views, titles);
viewPager.setAdapter(edit_hero_pager_adapter);  

如前所述,您不能滑动活动!

于 2012-08-25T13:11:58.497 回答