onclick用于viewpager中的图像

bxgwgixi  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(304)

我有一个图像滑块的帮助下作出的 ViewPager. 我想表现出不同 toast 在加载的不同图像中单击时的消息 ViewPager ,我该怎么做?
显示图像滑块的“我的活动”:

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() 但没用。

v64noz0r

v64noz0r1#

据我所知,你需要显示吐司时,点击图片的每一页 ViewPager 在这种情况下,一个简单的方法是添加 onClickListener 给你的 ImageView 在适配器的内部 instantiateItem 方法。因此,您应该用以下内容更改/替换您的方法:

@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;
}
qxsslcnc

qxsslcnc2#

试试这个:

@Override
public Object instantiateItem(View collection, final int pos) { //have to make final so we can see it inside of onClick()
    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View page = inflater.inflate(R.layout.YOUR_PAGE, null);

    page.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            //this will log the page number that was click
            Log.i("TAG", "This page was clicked: " + pos);
        }
    });

    ((ViewPager) collection).addView(page, 0);
    return page;
}

希望这能奏效!

u4dcyp6a

u4dcyp6a3#

适配器类中的make image click接口

public class MyAdapter extends PagerAdapter {

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

public interface MyClassImage{
    void imageClick(String id);

}

public ViewPagerAdapter(Context context, ArrayList<Integer> images,MyClassImage myClassImage) {
    this.context = context;
    this.images=images;
    this.myClassImage=myClassImage;

    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.z_slide_pager_item, view, false);
    ImageView myImage = (ImageView) myImageLayout.findViewById(R.id.image);
    myImage.setImageResource(images.get(position));
    view.addView(myImageLayout, 0);
    final int id=images.get(position);
    final String imgId=String.valueOf(id);

    myImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myClassImage.imageClick(imgId);
        }
    });
    return myImageLayout;
}

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

并在mainactivity类中实现它

public class MainActivity extends AppCompatActivity implements MyAdapter.MyClassImage {
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,this));
    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);
}

@Override
public void imageClick(String id) {

    Toast.makeText(this, "page "+id, Toast.LENGTH_SHORT).show();
}

希望对你有帮助。
谢谢。

相关问题