gpt4 book ai didi

java - onClick 在 vi​​ewpager android 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:09 25 4
gpt4 key购买 nike

我使用 viewpager 制作了幻灯片并禁用了默认的滑动功能。相反,我想在单击图像时更改幻灯片。因此,我在 viewpageradapter 类中添加了一段代码,但当我单击图像时,它不执行任何操作。ViewPagerAdapter 类是:

package com.dekton.dektonapp;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewPagerAdapter extends PagerAdapter implements Animation.AnimationListener {

//Declare variables
Context context;
int[] background;
int[] icon;
String[] title;
String[] title_2;
String[] description;
LayoutInflater inflater;

public ViewPagerAdapter(Context context, int[] background, int[] icon, String[] title, String[] title_2, String[] description) {
this.context = context;
this.background = background;
this.icon = icon;
this.title = title;
this.title_2 = title_2;
this.description = description;
}

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

@Override
public int getCount() {
return background.length;
}

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

//Declare variables
ImageView iconimage, whitebox, bgimage, slidedownimage;
TextView titletext, title_2text, descriptiontext;
final ViewPager viewPager;
Animation translate, slide;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.slidescreen_item, container, false);

//Locate textviews in slidescreen_item.xml
titletext = (TextView) itemView.findViewById(R.id.title);
title_2text = (TextView) itemView.findViewById(R.id.title_2);
descriptiontext = (TextView) itemView.findViewById(R.id.description);

//Capture position and set to textviews
titletext.setText(this.title[position]);
title_2text.setText(this.title_2[position]);
descriptiontext.setText(this.description[position]);

//Locate imageview in slidescreen_item.xml
iconimage = (ImageView) itemView.findViewById(R.id.icon);
whitebox = (ImageView) itemView.findViewById(R.id.whitebox);
bgimage = (ImageView) itemView.findViewById(R.id.bgimage);

//Capture position and set to the Imageview
bgimage.setBackgroundResource(this.background[position]);
iconimage.setImageResource(this.icon[position]);
//Apply font
Typeface BryantProBold = Typeface.createFromAsset(this.context.getAssets(), "BryantProBold.otf");
Typeface PFHighwaySansProRegular = Typeface.createFromAsset(this.context.getAssets(), "PFHighwaySansProRegular.ttf");
titletext.setTypeface(PFHighwaySansProRegular);
title_2text.setTypeface(PFHighwaySansProRegular);
descriptiontext.setTypeface(BryantProBold);

//Apply animations
translate = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.ABSOLUTE, -48f, TranslateAnimation.ABSOLUTE, 150f);
translate.setDuration(5000);
translate.setRepeatCount(-1);
translate.setRepeatMode(Animation.REVERSE);
translate.setInterpolator(new LinearInterpolator());
bgimage.setAnimation(translate);
slide = AnimationUtils.loadAnimation(this.context, R.anim.whiteboxanim);
slide.setAnimationListener(this);
whitebox.startAnimation(slide);
iconimage.startAnimation(slide);
titletext.startAnimation(slide);
title_2text.startAnimation(slide);
descriptiontext.startAnimation(slide);

viewPager = (ViewPager) itemView.findViewById(R.id.pager);

slidedownimage = (ImageView) itemView.findViewById(R.id.slidedownimage);
slidedownimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (viewPager.getCurrentItem() < viewPager.getChildCount()) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
} else {
viewPager.setCurrentItem(0);
}
}
});
((ViewPager) container).addView(itemView);
return itemView;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//Remove slidescreen_item.xml from viewpager
((ViewPager) container).removeView((RelativeLayout) object);
}

@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}
}

谢谢:)

最佳答案

<ImageView 
...
android:clickable="true"
/>

检查一下。如果你想在 ImageView 上设置 onClick Listener,你应该将 clickable 设置为 true。希望这可以帮助。 :)

关于java - onClick 在 vi​​ewpager android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30821728/

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