Reputation: 91
I am a beginner in android programming. I am using button onclick to add new page to the viewpager using the codes below. However, the viewpager return wrong position value when instantiate item on swipe activity.
Swipe to the right
Correct position: 1, 2, 3 (page 2, 3 and 4)
Return wrong position instead: 2, 3, 4
Swipe to the left
Correct position: 3, 2, 1 (page 4, 3 and 2)
Return wrong position instead: 2, 1, 0
I want to get position value from instantiateItem() to get view from a specific position from the "views" array list to be displayed on the viewpager. The wrong position causes the wrong view to be displayed. Hope someone can help me!!! Thanks in advance.
Add Button:
findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.image_text_1, null);
// the array list
views.add(itemView);
pagerAdapter.notifyDataSetChanged();
}
});
ViewPager:
private class SlideAdapter extends PagerAdapter {
ArrayList<View> views = new ArrayList<View>();
@Override
public int getItemPosition (Object object)
{
int index = views.indexOf((View)object);
if(views.contains((View)object)){
return index;
}else{
return POSITION_NONE;
}
}
@Override
public Object instantiateItem (ViewGroup container, final int position)
{
//for example, when I swipe to the right from page 1 to page 2, the int position return value of 2 instead of 1.
View v = views.get(position);
imagePage = (ImageView) v.findViewById(R.id.imgSlide);
imagePage.setOnLongClickListener(null);
imagePage.setLongClickable(true);
imagePage.setOnLongClickListener(btnImageChoose);
((ViewPager)container).addView(v, 0);
return v;
}
@Override
public void destroyItem (ViewGroup container, int position, Object object)
{
container.removeView (views.get(position));
}
@Override
public int getCount ()
{
return views.size();
}
@Override
public boolean isViewFromObject (View view, Object object)
{
return view == object;
}
public int addView (View v)
{
return addView (v, views.size());
}
public int addView (View v, int position)
{
views.add (position, v);
return position;
}
public int removeView (ViewPager pager, View v)
{
return removeView (pager, views.indexOf (v));
}
public int removeView (ViewPager pager, int position)
{
pager.setAdapter (null);
views.remove (position);
pager.setAdapter (this);
return position;
}
public View getView (int position)
{
return views.get (position);
}
}
Upvotes: 1
Views: 6838
Reputation: 91
It seems like not the instantiateItem() problem. I get my current view from setPrimaryItem().
private View mCurrentView;
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
mCurrentView = (View)object;
}
Upvotes: 8
Reputation: 173
You don't need to use views array list:
class SamplePagerAdapter extends PagerAdapter {
// for example
List<String> str = new ArrayList<String>();
public SamplePagerAdapter(List<String> str){
this.str = str;
}
private void addPage(String item) {
str.add(item);
notifyDataSetChanged();
}
@Override
public int getCount() {
return str.size();
}
@Override
public View instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(container.getContext());
View v = inflater.inflate(R.layout.image_text_1, container, false);
// Your code. (don't use addView);
return v;
}
}
and in onClick method:
public void OnClick(View v){
adapter.addPage("New page")
}
Upvotes: 0