gpt4 book ai didi

java - ViewPager 中的 onClick 图像

转载 作者:行者123 更新时间:2023-12-01 19:30:27 24 4
gpt4 key购买 nike

我在 ViewPager. 的帮助下制作了一个图像 slider 我想展现不同的toastViewPager 加载的不同图像中单击时出现消息,我该怎么做?

这是我的 Activity ,其中显示图像 slider :

public class MainActivity extends AppCompatActivity{
public static final String MAIN = "OTHER";
private static ViewPager mPager;
private static int currentPage = 0;
private static final Integer[] XMEN=
{R.drawable.benz2, R.drawable.benz2,
R.drawable.benz2};
private ArrayList<Integer> XMENArray = new ArrayList<Integer>();
@Override
protected void onCreate(...);
init();
}
private void init() {
for(int i=0;i<XMEN.length;i++)
XMENArray.add(XMEN[i]);

mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new com.example.nepali_test.MyAdapter(MainActivity.this,XMENArray));
CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
indicator.setViewPager(mPager);
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == XMEN.length) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 3000, 3000);
}

这是 viewpager 的适配器:

public class MyAdapter extends PagerAdapter {

private ArrayList<Integer> images;
private LayoutInflater inflater;
private Context context;

public MyAdapter(Context context, ArrayList<Integer> images) {
this.context = context;
this.images=images;
inflater = LayoutInflater.from(context);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}

@Override
public int getCount() {
return images.size();
}

@Override
public Object instantiateItem(ViewGroup view, int position) {
View myImageLayout = inflater.inflate(R.layout.slide, view, false);
ImageView myImage = (ImageView) myImageLayout
.findViewById(R.id.image);
myImage.setImageResource(images.get(position));
view.addView(myImageLayout, 0);
return myImageLayout;
}

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

我尝试通过添加来制作

if (currentPage==1){
Toast.makeText(this, "page 1", Toast.LENGTH_SHORT).show();
}

init()但没有成功。

最佳答案

据我了解,您需要在单击 ViewPager 的每个页面中的图像时显示 toast,在这种情况下,一个简单的方法是添加 onClickListener到适配器的 instantiateItem 方法内的 ImageView 。所以你应该用这个改变/替换你的方法:

@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup view, final int position) {
View myImageLayout = inflater.inflate(R.layout.slide, view, false);
ImageView myImage = (ImageView) myImageLayout
.findViewById(R.id.image);
myImage.setImageResource(images.get(position));
myImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context.getApplicationContext(), "page "+position, Toast.LENGTH_SHORT).show();
}
});
view.addView(myImageLayout, 0);
return myImageLayout;
}

关于java - ViewPager 中的 onClick 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59894602/

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