我在我的应用中使用了this project (Android-Coverflow),其工作正常,只有一个例外:在View.OnClickListener
中的单个项目上设置instantiateItem
时,我确实得到错误的位置,即:
因此,如果我滚动到目前为止,索引为3的项目位于中间,我会得到
我在ClickListener
方法中添加instantiateItem
,所以我希望它是正确的...
我可能会在这里失踪什么?
我将改编后的项目上传到Github:https://github.com/haemi/Android-Coverflow-Clicklistener-Issue - 在“变压器封面流2”里面,问题是可见的。相应的代码在这里:https://github.com/haemi/Android-Coverflow-Clicklistener-Issue/blob/master/app/src/main/java/me/crosswall/coverflow/demo/Normal2Activity.java#L63
答案 0 :(得分:2)
试试这个,
@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);
}
这是完整的代码。
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);
}
}
}
答案 1 :(得分:0)
这可能对Op没有帮助,但对于有类似问题的人来说,问题是使用PageTransformer
时引起的。我刚刚删除了行
viewPager.setPageTransformer(true,new MyViewPagerTransformation());
和所有Click都运行良好。我不确定为什么PageTransformer
会导致此问题,我将在获得更多信息后立即对其进行更新。