好的,所以我正在开发一个利用ViewPager来显示页面的Android应用程序。
在每个页面中,我都有一组按钮用于在页面之间导航(除了在页面之间滑动)。这些按钮用于"第一页"、"上一页"、"下一页"和"最后一页"。
我不知道该怎么做的是设计一种机制来启用按钮单击时的页面更改。
有人有什么想法吗?
ETA:为了更好地解释设置,按钮在每个页面的布局中声明,并与PagerAdapter中的其余布局一起膨胀。我的问题是我无法从PagerAdapter中引用ViewPager。或者至少,我想不出办法。
按钮:
Button yourButton = (Button)findViewById(R.id.button1);
yourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mViewPager.setCurrentItem(getItem(+1), true); //getItem(-1) for previous
}
});
功能:
private int getItem(int i) {
return mViewPager.getCurrentItem() + i;
}
yourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
yourViewPager.setCurrentItem(page, smoothScroll);
}
});
1) 制作布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/white"
android:weightSum="1">
<android.support.v4.view.ViewPager
android:id="@+id/images_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v4.view.ViewPager>
<ImageView
android:id="@+id/img_next"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/forward_white"
android:background="@color/colorPrimary"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
/>
<ImageView
android:id="@+id/img_previous"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/back_white"
android:background="@color/colorPrimary"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
</RelativeLayout>
2) 设置自定义适配器
CustomViewPagerAdapter custompageradpter;
ViewPager mViewPager;
mViewPager = (ViewPager)shareImagesDialouge.findViewById(R.id.images_pager);
custompageradpter = new CustomViewPagerAdapter(this);
mViewPager.setAdapter(custompageradpter);
public class CustomViewPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
int[] mResources = {
R.drawable.emoji_1,
R.drawable.emoji_2,
R.drawable.emoji_3,
R.drawable.emoji_4,
R.drawable.emoji_5,
R.drawable.emoji_6
};
public CustomViewPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
3) 对于下一个和上一个按钮
case R.id.img_previous:
mViewPager.setCurrentItem(getItemofviewpager(-1), true);
break;
case R.id.img_next:
mViewPager.setCurrentItem(getItemofviewpager(+1), true);
break;
4)使此功能
private int getItemofviewpager(int i) {
return mViewPager.getCurrentItem() + i;
}
对sweggersen和Alex Orlov的答案的防故障升级,
// For scrolling to next item
nextPageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(getNextPossibleItemIndex(1), true);
}
});
// For scrolling to previous item
previousPageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(getNextPossibleItemIndex(-1), true);
}
});
在活动/片段中添加getNextPossibleItemIndex()方法,
private int getNextPossibleItemIndex (int change) {
int currentIndex = viewPager.getCurrentItem();
int total = viewPager.getAdapter().getCount();
if (currIndex + change < 0) {
return 0;
}
return Math.abs((currentIndex + change) % total) ;
}
这样,您将能够更改当前项目,而不必担心 IndexOutOfBoundsException,正如 Heinrich 的评论所建议的那样。请注意,使用此方法,项目将像循环列表一样显示。
1 -> 2 -> 3 -> 1 -> 2 -> 3 -> 1
但是回过头来,它会停在第一个项目
1 <- 1 <- 2 <- 3
这样做:
backButton=(Button)findViewById(R.id.back_button);
nextButton=(Button)findViewById(R.id.next_button);
backButton.setVisibility(View.INVISIBLE);//you can use GONE or even disable it. It depends on your layout
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1, true);
}
});
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem()-1, true);
}
});
}
为了防止IndexOutOfBoundsException,您应该隐藏并显示两个按钮,如下所示:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int position) {
if(position==0) {
backButton.setVisibility(View.INVISIBLE);
}else {
backButton.setVisibility(View.VISIBLE);
}
if(position < viewPager.getAdapter().getCount()-1 ) {
nextButton.setVisibility(View.VISIBLE);
}else {
nextButton.setVisibility(View.INVISIBLE);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
我用这段代码完成了这个项目。
vPager = (ViewPager) findViewById(R.id.viewpager);
View tempView1 = inflater.inflate(R.layout.ani_dialog1, null);
firstView = (ImageView) tempView1.findViewById(R.id.ani_dialog_next);
views.add(tempView1);
firstView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
vPager.setCurrentItem(1, true);
}
});
更新代码
Button preButton = (Button)findViewById(R.id.pre_button);
Button nextButton = (Button)findViewById(R.id.next_button);
preButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem()-1, true);
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1, true);
}
});
首先创建一个接口
public interface OnPageChangedListener {
void OnPageChanged(int position); }
下一页 使用 OnPageChangedListener 界面实现您的活动并执行以下操作
private void setupViewPager(ViewPager viewPager){
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(new Fragment1(this));
viewPagerAdapter.addFragment(new Fragment2(this));
viewPagerAdapter.addFragment(new Fragment3(this));
viewPagerAdapter.addFragment(new Fragment4(this));
viewPagerAdapter.addFragment(new Fragment5(this));
viewPagerAdapter.addFragment(new Fragment6(this));
viewPager.setAdapter(viewPagerAdapter);
}
@Override
public void OnPageChanged(int position) {
viewPager.setCurrentItem(position);
}
在您的片段中创建一个构造函数,使用
private OnPageChangedListener listener;
public Fragment1(OnPageChangedListener listener1) {
this.listener = listener1;
}
查看寻呼机适配器
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
}
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}}
希望这对你有帮助
public void onClick(View v) {
int currentItem = mViewPager.getCurrentItem();
mViewPager.setCurrentItem(currentItem+1); //(currentItem-1)
}
只有这个代码在按钮点击: @Override
getcurrentitem
是viewpager
中的实际项目,则下一项是currentItem + 1