好的,所以我正在开发一个利用ViewPager来显示页面的Android应用程序。
在每个页面中,我有一组用于在页面之间导航的按钮(除了在页面之间滑动)。这些按钮用于“第一页”,“上一页”,“下一页”和“最后一页”。
我无法弄清楚该怎么做是设计一种机制,以便在按钮点击时启用页面更改。
有人有什么想法吗?
ETA:为了更好地解释设置,按钮在每个页面的布局中声明,并在PagerAdapter中用其余布局充气。我的问题是我无法在PagerAdapter中引用ViewPager。或者至少,我想不出办法。
答案 0 :(得分:94)
按钮:
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;
}
希望这会有所帮助:)
答案 1 :(得分:44)
yourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
yourViewPager.setCurrentItem(page, smoothScroll);
}
});
答案 2 :(得分:3)
我用这段代码完成了这个项目。
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);
}
});
答案 3 :(得分:3)
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;
}
答案 4 :(得分:2)
防病毒升级到sweggersen's和Alex Orlov's,
// 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);
}
});
在Activity / Fragment中添加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) ;
}
这样,您就可以更改currentItem而无需担心IndexOutOfBoundsException as suggested by Heinrich's comment。请注意,使用此方法,项目将显示为圆形列表。
1 -> 2 -> 3 -> 1 -> 2 -> 3 -> 1
但回过头来,它会停在第一项
1 <- 1 <- 2 <- 3
答案 5 :(得分:1)
更新代码
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);
}
});
答案 6 :(得分:0)
这样做:
from pynput.keyboard import Key, Controller
def game():
keyboard = Controller()
if key.press():
s = Sound()
s.read("Cymatics - Guitar Chord Loop 2 - 128 BPM Dmaj")
s.play()
print "Welcome to soundboard"
print "You will see a list of sound names, just write the name of it"
print "If you want to stop it type stop"
print "Type start"
start = input()
if start.lower() == "start":
while True:
game()
else:
print "Ups, try again.."
要防止IndexOutOfBoundsException,您应该隐藏并显示两个按钮,如下所示:
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);
}
});
}
答案 7 :(得分:0)
首先创建一个界面
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;
}
ViewPagerAdapter
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);
}}
希望这对您有帮助
答案 8 :(得分:-4)
public void onClick(View v) {
int currentItem = mViewPager.getCurrentItem();
mViewPager.setCurrentItem(currentItem+1); //(currentItem-1)
}
按钮单击中只有此代码:
@Override
getcurrentitem
是viewpager
中的实际项目,下一项是currentItem + 1