简体   繁体   中英

ClickListener in PagerAdapter fires on wrong position

I'm using this project (Android-Coverflow) in my app, which works as expected with one exception: when setting a View.OnClickListener on the single items in instantiateItem I do get wrong positions, ie:

  • the middle item returns the correct position.
  • the item on the right of the middle item displays the correct position (middle-item + 1)
  • the item to the left of the middle item displays the wrong position: the same as the item to the right.

So if I'm scrolling so far that item with index 3 is in the middle, I get

  • 3 for the middle item (correct)
  • 4 for the item to the right (correct)
  • 4 for the item to the left (wrong)

I add the ClickListener inside the instantiateItem method, so I would expect it to be correct...

What could I probably be missing here?

I uploaded the adapted project to Github: https://github.com/haemi/Android-Coverflow-Clicklistener-Issue - inside "transformer coverflow 2" the issue is visible. The according code is here: https://github.com/haemi/Android-Coverflow-Clicklistener-Issue/blob/master/app/src/main/java/me/crosswall/coverflow/demo/Normal2Activity.java#L63

Try this,

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        final TextView view = new TextView(Normal2Activity.this);
        view.setText("Item " + position);
        view.setGravity(Gravity.CENTER);
        view.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Normal2Activity.this, "position: " + position, Toast.LENGTH_SHORT).show();
            }
        });

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, final int position, Object object) {
        ((ViewPager) container).removeViewAt(position);
    }

Here is the full code.

public class Normal2Activity extends AppCompatActivity  {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_normal2);

    PagerContainer mContainer = (PagerContainer) findViewById(R.id.pager_container);

    final ViewPager pager = mContainer.getViewPager();

    PagerAdapter adapter = new MyPagerAdapter();
    pager.setAdapter(adapter);

    pager.setOffscreenPageLimit(adapter.getCount());

    pager.setClipChildren(false);


    boolean showRotate = getIntent().getBooleanExtra("showRotate",true);

    if(showRotate){
        new CoverFlow.Builder()
                .with(pager)
                .scale(0.3f)
                .pagerMargin(0f)
                .spaceSize(0f)
                .rotationY(25f)
                .build();
    }

}

private class MyPagerAdapter extends PagerAdapter {

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        final TextView view = new TextView(Normal2Activity.this);
        view.setText("Item " + position);
        view.setGravity(Gravity.CENTER);
        view.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Normal2Activity.this, "position: " + position, Toast.LENGTH_SHORT).show();
                System.out.println("position:::::::::::::" + position);
            }
        });

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, final int position, Object object) {
        ((ViewPager) container).removeViewAt(position);
    }

    @Override
    public int getCount() {
        return 15;
    }

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

}

This might not be useful for Op but for people having similar problem , The problem is caused when using a PageTransformer .I just Removed the line

viewPager.setPageTransformer(true,new MyViewPagerTransformation()); and all Click were working perfectly . I am not sure why PageTransformer is causing this issue , I will update it as soon I get more info .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM