my name
my name

Reputation: 158

Android - How to manage position of viewpager in adapter?

I am developing an android app, in it i am displaying a ListView with a number of items. on Click of an item i am passing item name and ArrayList of all items .

Intent myIntent = new Intent(context, DetailActivity.class);
            myIntent.putExtra("name", name);
            myIntent.putStringArrayListExtra("arraylistofitems", arraylistofitems);
            context.startActivity(myIntent);

In DetailActivity, i want to show the clicked items from list and on scroll left or right show the remaining item from the list. I am using following code -

Intent mIntent = getIntent();
    name = mIntent.getStringExtra("name");
    names = mIntent.getStringArrayListExtra("arraylistofitems");
    viewPager = (ViewPager) findViewById(R.id.idofviewpager);
    adapter = new ViewPagerAdapter(context, arraylistofitems);
    viewPager.setAdapter(adapter);

and ViewPagerAdapter looks like following -

public ViewPagerAdapter(Context context,ArrayList<String> data) {
    this.context = context;
    this.data=data;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((MyLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

    // item views declaration

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.viewpager_item_id, container,
            false);

    //layout item initialization

    ((ViewPager) container).addView(itemView);

    ParseQuery<ParseObject> query = ParseQuery.getQuery("myclass");
    query.whereEqualTo("colName", data.get(position));
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> itemList, ParseException arg1) {
            for (int i = 0; i < itemList.size(); i++) {
                ParseObject po = itemList.get(i);

                //set the values from parse into views
            }
        }
    });


    return itemView;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((MyLayout) object);

}

But all data being shown from starting (0th Position to length of arraylist).

Question - How do i implement scroll effect with clicked item being opened and on scrolling left left items should be display or vice-versa?

Upvotes: 1

Views: 345

Answers (1)

Niranjan Singh Chouhan
Niranjan Singh Chouhan

Reputation: 148

You can do one thing , pass position of item which is being clicked and get it on your detail activity same as you are getting item name and array of list. and then call following line -

    viewPager.setCurrentItem(position);

So, now there is no need to manage position on adapter. adapter will take care of it.

Upvotes: 1

Related Questions