gpt4 book ai didi

java - 单击 ViewPager 中的特定页面后如何跳转到另一个 Activity ?

转载 作者:行者123 更新时间:2023-12-02 09:21:15 26 4
gpt4 key购买 nike

当我跳转到其他 Activity 时,我无法在 ViewPager 中转到下一个 Activity 我在第一个 Activity 中创建了一个“幻灯片图像”;其中有五个图像。滑动最后一个图像(第五张)后,它应该转到下一个 Activity ,say-activity_next.xml。

My RoomSelect class :

          mslidViewPager = (ViewPager) findViewById(R.id.slideViewPager);

sliderAdapter=new SliderAdapter(this);

mslidViewPager.setAdapter(sliderAdapter);

My slideractivity class

  public String[] slide_headings = {
"DELUX",
"SEMI DELUX",
"Dormitory"
};

public String[] s1= {
"Apply for Delux",
"Apply for Semi-Delux",
"Apply for Dormitory"

};


public Object instantiateItem(@NonNull ViewGroup container, final int position) {

layoutInflater =(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.slide_layout,container,false);



imageView.setImageResource(slide_images[position]);
slideHeading.setText(slide_headings[position]);
slideDes.setText(slide_descrpition [position]);
but.setText(s1 [position]);





view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(position==0) {
Toast.makeText(context, "delux", Toast.LENGTH_SHORT).show();

}
else if (position==1) {
Toast.makeText(context, "semi-delux", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Dormitory", Toast.LENGTH_SHORT).show();

}
}
});

container.addView(view);

return view;
}

最佳答案

在适配器中执行以下操作:

override fun instantiateItem(container: ViewGroup, position: Int): Any {
val inflater = LayoutInflater.from(context)
val itemView = context.layoutInflater.inflate(R.layout.row, container, false) as ViewGroup
val heading = itemView.findViewById(R.id.textView) as TextView
if (position < list.size)
heading.setText(list[position])
container.addView(itemView)

return itemView

}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as LinearLayout)
}

override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}

override fun getCount(): Int {
return list.size+1
}

内部 Activity :

 viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {

}

override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {

}

override fun onPageSelected(position: Int) {
if (position == list.size) {
startActivity(Intent(this@MainActivity, SecondActivity::class.java))
}

}

})

关于java - 单击 ViewPager 中的特定页面后如何跳转到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com