我想在ViewPager中添加项目,并允许用户根据需要添加或删除视图。在视图寻呼机中添加或删除项目时,滚动问题就会出现。当在0索引处添加项目时,不应向下滚动查看寻呼机。
这是我的代码
private class StoryAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, final int position) {
// TODO Auto-generated method stub
Item item = lists.get(position);
LayoutInflater inflater = getActivity().getLayoutInflater();
final View viewItem = inflater.inflate(R.layout.custom_story, container, false);
TextView title = viewItem.findViewById(R.id.titlestory);
TextView storytext = viewItem.findViewById(R.id.storytext);
ImageView storyImg = viewItem.findViewById(R.id.storyimg);
title.setTypeface(C.getBlackToBlackFont(getActivity()));
storytext.setTypeface(C.getRobotoRegularFont(getActivity()));
title.setText(item.storyTitle);
storytext.setText(item.storyDescription);
PicassoCache.getPicassoInstance(getActivity()).load(item.storyImage).placeholder(R.drawable.dummyimage).error(R.drawable.dummyimage).fit().centerCrop().into(storyImg);
((ViewPager) container).addView(viewItem);
return viewItem;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lists.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((View) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
}
XML:
<me.kaelaela.verticalviewpager.VerticalViewPager
android:id="@+id/vertical_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/hd"
/>