[英]Android ViewPager Delete Current Page and move to next
在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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.