gpt4 book ai didi

java - Android ViewPager 删除当前页面并移至下一页

转载 作者:行者123 更新时间:2023-12-02 12:22:26 25 4
gpt4 key购买 nike

在 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);

关于java - Android ViewPager 删除当前页面并移至下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45666326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com