我创建了一个"幻灯片图片"在第一次活动中;它有五个图像。在我滑动最后一个图像(第五个)之后,它应该转到下一个活动,比如说 - activity_next.xml。
下面是我的幻灯片代码,即SlideActivity.java
public class SlideActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Illustrations.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
}
}
我没有在下一个活动中传递Intent的位置,即activity_next.xml
****根据您的建议更新代码****
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position==viewPager.getAdapter().getCount()){
Intent reg = new Intent(SlideActivity.this,activity_next.class);
startActivity(reg);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
@Override
public int getCount() {
return mImages.length+1;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Illustrations.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
if(position < getCount()-1)
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
**** activity_next.java代码低于****
public class activity_next extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
}
}
**** activity_next.xml代码如下****
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Nowhere Else" />
</LinearLayout>
答案 0 :(得分:2)
如果您想要滑动最后一张图片以转到下一个活动,例如&#34;解雇&#34;效果,你应该尝试这样的事情:
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
@Override
public int getCount() {
return mImages.length+1;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Illustrations.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
if(position < getCount()-1)
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
然后:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position)
{
if(position==viewPager.getAdapter().getCount()-1){
//start next Activity
}
}
});
}
答案 1 :(得分:1)
我没有把Intent传递到下一个活动的位置,即 activity_next.xml
要在ViewPager中的最后一个项目到达时启动活动,请将OnPageChangeListener
添加到ViewPager,然后在onPageSelected
开始下一个活动:
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position)
{
if(position==viewPager.getAdapter().getCount()){
//start next Activity here with activity_next.xml layout
}
}
....
});
答案 2 :(得分:1)
我清楚地知道,因为@Chol说我只是将计数设为-1,如下所示。
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if(position==viewPager.getAdapter().getCount()-1){
Intent reg = new
Intent(Illustrations.this,Register_Page.class);
startActivity(reg);
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}
非常感谢你们......:)
答案 3 :(得分:0)
位置是像slide_image [position]一样的数组的位置。因此,使用position,只需编写几个代码就可以在SliderAdapter页面创建Intent。
@覆盖 public Object instantiateItem(final ViewGroup container,final int position){
layoutInflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout, container, false);
LinearLayout layoutslide = view.findViewById(R.id.slider);
ImageView slideImageView =(ImageView)view.findViewById(R.id.slide_image);
TextView slideHeading=(TextView)view.findViewById(R.id.slide_heading);
TextView slideDescription=(TextView)view.findViewById(R.id.slide_desc);
slideImageView.setImageResource(slide_image[position]);
slideHeading.setText(slide_headings[position]);
slideDescription.setText(slide_description[position]);
slideImageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (position == 0) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
if (position == 1) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
if (position == 2) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
}
});