在 ViewPager 中,当用户单击删除按钮时,我试图删除当前页面并移至下一页。当我尝试下面的代码时,它移动到下一页但删除了最后一页。请帮助我。
@Override
public Object instantiateItem(ViewGroup container, final int IdxVal)
{
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Glide.with(context)
.load(imageListAryVar.get(IdxVal))
.fitCenter()
.into(imageView);
linearLayout.addView(imageView);
container.addView(linearLayout);
return linearLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((LinearLayout) object);
}
void delCurrentPageFnc()
{
int delIdxVar = viewPager.getCurrentItem();
if(imageListAryVar.size() > 1)
{
if (delIdxVar == imageListAryVar.size() - 1)
viewPager.setCurrentItem(delIdxVar - 1);
else
viewPager.setCurrentItem(delIdxVar + 1);
imageListAryVar.remove(delIdxVar);
notifyDataSetChanged();
}
else finish();
}
经过几次尝试,我通过添加 getItemPosition 并设置 return POSITION_NONE 解决了这个问题;
如果你不添加这个notifyDataSetChanged(); 方法行不通。
@Override
public int getItemPosition(Object object)
{
return POSITION_NONE;
}
void delCurrentPageFnc()
{
int delIdxVar = viewPager.getCurrentItem();
if(imageListAryVar.size() > 1)
{
imageListAryVar.remove(delIdxVar);
notifyDataSetChanged();
}
else finish();
}
设置 getItemPosition 后这是不必要的,因此删除
if (delIdxVar == imageListAryVar.size() - 1)
viewPager.setCurrentItem(delIdxVar - 1);
else
viewPager.setCurrentItem(delIdxVar + 1);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句