获取当前位置寻呼机适配器onClickListener

时间:2018-03-05 10:13:42

标签: android android-viewpager

我的寻呼机适配器出了问题。我在布局inflater中有ImageViewTextView。我的适配器将显示图像的一些图像和描述。我希望向寻呼机实施onClick,当我点击图片时,TextView会显示/隐藏。

在我的代码段下面:

private class MyAdapter extends PagerAdapter implements ViewPager.OnClickListener {
    private ArrayList<Bitmap> images;
    private LayoutInflater inflater;
    private Context context;
    private ViewPager viewPager;

    private TextView textViewDescription;
    private ImageView imageViewDescription;

    public MyAdapter(Context context, ArrayList<Bitmap> images, ViewPager viewPager) {
        this.context = context;
        this.images = images;
        this.viewPager = viewPager;
        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, final int position) {
        LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View page = inflater.inflate(R.layout.item_image_preview, null);

        ImageView myImage = (ImageView) page.findViewById(R.id.imageView_preview);
        TextView textView= (TextView) page.findViewById(R.id.textView_image_description);

        textViewDescription = textView;
        imageViewDescription = myImage;

        imageDescription = arraydescription.get(position);

        textView.setText(arraydescription.get(position));
        myImage.setImageBitmap(images.get(position));

        page.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                if (isDescriptionVisible) {
                    textViewDescription.setVisibility(View.GONE);
                } else {
                    textViewDescription.setVisibility(View.VISIBLE);
                }
                isDescriptionVisible = !isDescriptionVisible;

                Log.i("TAG", "This page was clicked: " + position);
            }
        });

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

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

    @Override
    public void onClick(View v) {
        if (isDescriptionVisible) {
            textViewDescription.setVisibility(View.GONE);
        } else {
            textViewDescription.setVisibility(View.VISIBLE);
        }
        isDescriptionVisible = !isDescriptionVisible;
    }
}

问题是:如果位置2的寻呼机,我单击图像,位置2的TextView将显示/隐藏。但当我滑入位置1并单击图像时,显示/隐藏的TextView仍处于位置2.它只是我可以处理的一个位置。我看过a question about this example,但它仍无效。

1 个答案:

答案 0 :(得分:1)

您需要保存每个页面的状态。请尝试以下代码。我已将状态设置为Tag。

 public Object instantiateItem(ViewGroup view, final int position) {
    LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View page = inflater.inflate(R.layout.item_image_preview, null);
    final ImageView myImage = (ImageView) page.findViewById(R.id.imageView_preview);
    final TextView textView= (TextView) page.findViewById(R.id.textView_image_description);
    imageDescription = arraydescription.get(position);
    textView.setText(arraydescription.get(position));
    myImage.setImageBitmap(images.get(position));
    page.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            boolean isVisible=textView.getTag()==null?true:(boolean)textView.getTag();
            if (isVisible) {
                textView.setVisibility(View.GONE);
            } else {
                textView.setVisibility(View.VISIBLE);
            }
            textView.setTag(!isVisible);
            Log.i("TAG", "This page was clicked: " + position);
        }
    });
    ((ViewPager) view).addView(page, 0);
    return page;
}