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, i.e.:
So if I'm scrolling so far that item with index 3 is in the middle, I get
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With