onClick在ViewPager Android中不起作用

毛利克·索兰基(Maulik solanki)

我使用viewpager进行了幻灯片放映,并禁用了默认的滑动功能。相反,我想在单击image时更改幻灯片。所以我在viewpageradapter类中添加了一个代码,但是当我单击image时它什么也没做。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) {

}
}

谢谢 :)

毛利克·索兰基(Maulik solanki)

我通过创建单击该图像的方法来做到这一点,并且所有编码都在viewpager类中完成。方法如下:

    public void onClickSlideDown(View view) {

    Animation slideback;
    ImageView iconimage, whitebox;
    TextView titletext, title_2text, descriptiontext;
    titletext = (TextView)findViewById(R.id.title);
    title_2text = (TextView)findViewById(R.id.title_2);
    descriptiontext = (TextView)findViewById(R.id.description);
    iconimage = (ImageView)findViewById(R.id.icon);
    whitebox = (ImageView)findViewById(R.id.whitebox);
    slideback = AnimationUtils.loadAnimation(this, R.anim.whiteboxanimback);
    slideback.setAnimationListener(this);
    whitebox.startAnimation(slideback);
    iconimage.startAnimation(slideback);
    titletext.startAnimation(slideback);
    title_2text.startAnimation(slideback);
    descriptiontext.startAnimation(slideback);

    if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount()) {
        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
    } else {
        Intent i1 = new Intent(this, glass_3.class);
        startActivity(i1);
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章