emen
emen

Reputation: 6308

ViewPager SetCurrentItem does not set the position after it is clicked

I'm using TouchImageView to load images in full screen and have zoom/pinch capability.

The images are pulled from URL via a web service. The response is in JSON. At this part, I'm using Volley+GSON to obtain the response and use a custom adapter to populate the Pager.

Initially, the images are shown in a listview. When a user click an item, it will go full screen showing the chosen item.

However, in my case, this doesn't happen. Whatever position the user chose, it will still show the image at index 0.

Here is how I do it:

In this adapter, a user will click an item and it will pass the position to another activity.

public class ItemPhotoAdapter extends BaseAdapter {

    private ArrayList<ItemImageModel> arrItemImage;

    @Override
    public ItemImageModel getItem(int i) {
        return arrItemImage.get(i);
    }

    ...

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        lf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(view == null){
            vh = new ViewHolder();
            view = lf.inflate(R.layout.row_photo_grid, null);
            vh.item_image = (ImageView) view.findViewById(R.id.img_item);

            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }


        ItemImageModel iim = arrItemImage.get(i);

        Picasso.with(context) //
                .load(iim.getResized()) //
                .placeholder(R.drawable.placeholder) //
                .error(R.drawable.error)
                .into(vh.item_image);

        vh.item_image.setOnClickListener(new OnImageClickListener(i));

        return view;
    }

    ...

    private class OnImageClickListener implements View.OnClickListener {
        int _position;

        public OnImageClickListener(int position) {
            this._position = position;
        }

        @Override
        public void onClick(View view) {
            Intent i = new Intent(context, PhotoGalleryActivity.class);
            i.putExtra("position", _position);
            context.startActivity(i);

        }
    }
}

In PhotoGalleryActivity, will load all the images back, but it doesn't start with the item the user chose.

public class PhotoGalleryActivity extends FragmentActivity {

    private ArrayList<ItemImageModel> arrItemImages;

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_gallery);

        ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
        pb = (ProgressBar) findViewById(R.id.loading);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        arrItemImages = new ArrayList<ItemImageModel>();

        Intent i = getIntent();
        final int position = i.getIntExtra("position", 1);
        user_id = i.getStringExtra("user_id");
        item_id = i.getStringExtra("item_id");

//        Log.d(TAG, "Image position: " + position);

        mAdapter = new PhotoGalleryAdapter(arrItemImages, getApplicationContext(), this);

        mViewPager.setAdapter(mAdapter);

        mViewPager.setCurrentItem(position);

        // ViewPagerIndicator
        CirclePageIndicator titleIndicator = (CirclePageIndicator)findViewById(R.id.titles);
        titleIndicator.setViewPager(mViewPager);

        loadPhotos();
    }

    ...

    private void loadPhotos() {
        mRequestQueue = Volley.newRequestQueue(this);

        String url = Constants.ITEM_DETAILS;

        GsonRequest<ItemDetailContainer> myReq = new GsonRequest<ItemDetailContainer>(
                Request.Method.GET, url, ItemDetailContainer.class,
                createMyReqSuccessListener(), createMyReqErrorListener());

        mRequestQueue.add(myReq);
    }

    ...
}

Upvotes: 3

Views: 18254

Answers (2)

Stephen Liu
Stephen Liu

Reputation: 141

If you want to retain the tab index through screen rotation, you can save the value into a member variable first:

if (savedInstanceState != null) {
    mLastTabIndex = savedInstanceState.getInt(KEY_TAB_INDEX, 0);
} else {
    mLastTabIndex = -1;
}

And only after the tabs data have been loaded into tab adapter, would you check the member variable and set current tab index if present:

mPostTypes = result.getTypes();

mAdapter.notifyDataSetChanged();
if (mLastTabIndex > 0) {
    mViewPager.setCurrentItem(mLastTabIndex);
}

Upvotes: 0

emen
emen

Reputation: 6308

I've found the solution.

To use SetCurrentItem correctly, in this case, I have to put it after all the images is loaded. Eg. after notifysetdatachanged.

In Volley response listener function,

private Response.Listener<ItemDetailContainer> createMyReqSuccessListener() {
    return new Response.Listener<ItemDetailContainer>() {
        @Override
        public void onResponse(ItemDetailContainer response) {
            try {
                ...

                mAdapter.notifyDataSetChanged();
                mViewPager.setCurrentItem(position); // this
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    };
}

Upvotes: 5

Related Questions